Hi everyone,
I've created 1 windows app in that app i've created Registration form including image fields.After filling all the details in that form when i click on save button i'm runtime error(Failed to convert parameter value from a Bitmap to a Byte[])..Can anyone tell me how to resolve this issue..I'm try to resolve this from since last 4days.I posted the question on so many forums but no use atleast i came to bytes forum..I hope I'll get the solution from this forum.
This is the code which I've written under button method::
private void btnaddinfo_Click(object sender, EventArgs e)
{
string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;
using (SqlConnection cn = new SqlConnection(scn))
{
using (SqlCommand cmd = new SqlCommand("SP_Info", cn))
{
try
{
byte[] photo = GetPhoto(imageloc);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@Hp_Number", SqlDbType.NVarChar, 50);
SqlParameter p2 = new SqlParameter("@Customer_Name", SqlDbType.VarChar, 50);
SqlParameter p3 = new SqlParameter("@Customer_Contact_Number", SqlDbType.NVarChar, 15);
SqlParameter p4 = new SqlParameter("@Guarantor_Name", SqlDbType.VarChar, 50);
SqlParameter p5 = new SqlParameter("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15);
SqlParameter p6 = new SqlParameter("@Hp_Date", SqlDbType.Date);
SqlParameter p7 = new SqlParameter("@Customer_Address", SqlDbType.NVarChar, Max);
SqlParameter p8 = new SqlParameter("@Vehicle", SqlDbType.VarChar, 50);
SqlParameter p9 = new SqlParameter("@Vehicle_Model", SqlDbType.VarChar, 50);
SqlParameter p10 = new SqlParameter("@Vehicle_Number", SqlDbType.NVarChar, 50);
SqlParameter p11 = new SqlParameter("@Chasis_Number", SqlDbType.NVarChar, 50);
SqlParameter p12 = new SqlParameter("@Engine_Number", SqlDbType.NVarChar, 50);
SqlParameter p13 = new SqlParameter("@FC_Date", SqlDbType.Date);
SqlParameter p14 = new SqlParameter("@Insurance_Date", SqlDbType.Date);
SqlParameter p15 = new SqlParameter("@Insurance_Amount", SqlDbType.Int);
SqlParameter p16 = new SqlParameter("@Paid_Amount", SqlDbType.Int);
SqlParameter p17 = new SqlParameter("@Vehicle_Pic",SqlDbType.Image,photo. Length);
SqlParameter p18 = new SqlParameter("@Customer_Pic ", SqlDbType.Image, photo.Length);
SqlParameter p19 = new SqlParameter("@Guarantor_Pic ", SqlDbType.Image, photo.Length);
SqlParameter p20 = new SqlParameter("@Documents_Pic", SqlDbType.Image, photo.Length);
SqlParameter p21 = new SqlParameter("@Insurance_Pic", SqlDbType.Image, photo.Length);
cmd.Parameters.Add(p1).Value = tbhpnum.Text;
cmd.Parameters.Add(p2).Value = tbcusnam.Text;
cmd.Parameters.Add(p3).Value=tbcusmblno.Text;
cmd.Parameters.Add(p4).Value = tbguanam.Text;
cmd.Parameters.Add(p5).Value = tbguamblno.Text;
cmd.Parameters.Add(p6).Value = DateTime.Parse(tbhpdat.Text);
cmd.Parameters.Add(p7).Value = tbcusadd.Text;
cmd.Parameters.Add(p8).Value = tbveh.SelectedItem.ToString();
cmd.Parameters.Add(p9).Value = tbvehmod.SelectedItem.ToString();
cmd.Parameters.Add(p10).Value = tbvehnum.Text;
cmd.Parameters.Add(p11).Value = tbchanum.Text;
cmd.Parameters.Add(p12).Value = tbengnum.Text;
cmd.Parameters.Add(p13).Value = DateTime.Parse(tbfcdat.Text);
cmd.Parameters.Add(p14).Value = DateTime.Parse(tbinsdat.Text);
cmd.Parameters.Add(p15).Value = Convert.ToInt32(tbinsamt.Text);
cmd.Parameters.Add(p16).Value = Convert.ToInt32(tbpaiamt.Text);
cmd.Parameters.Add(p17).Value = boxvehpic;
cmd.Parameters.Add(p18).Value = boxcuspic;
cmd.Parameters.Add(p19).Value = boxguapic;
cmd.Parameters.Add(p20).Value=boxdocpic;
cmd.Parameters.Add(p21).Value = boxinspic;
if (cn.State != ConnectionState.Open)
cn.Open();
int count = cmd.ExecuteNonQuery();
if (count == 1)
{
MessageBox.Show(count.ToString() + "Record(s) Saved.");
}
else
{
MessageBox.Show("Please correct the error which you have entered and then click on addinformation button");
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (cn.State == ConnectionState.Open)
cn.Close();
}
}
}
}
External Byte[] method code::
public static byte[] GetPhoto(string imageloc)
{
byte[] photo= null;
FileStream stream = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
photo= reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
return photo;
}
[^]&<>
Stored Procedure::
create PROC SP_Info
(
@Hp_Number nvarchar(50),
@Customer_Name varchar(50),
@Customer_Contact_Number nvarchar(15),
@Guarantor_Name varchar(50),
@Guarantor_Contact_Number nvarchar(15),
@Hp_Date date,
@Customer_Address nvarchar(MAX),
@Vehicle varchar(50),
@Vehicle_Model varchar(50),
@Vehicle_Number nvarchar(50),
@Chasis_Number nvarchar(50),
@Engine_Number nvarchar(50),
@FC_Date date,
@Insurance_Date date,
@Insurance_Amount int,
@Paid_Amount int,
@Vehicle_Pic image,
@Customer_Pic image,
@Guarantor_Pic image,
@Documents_Pic image,
@Insurance_Pic image
)
AS
BEGIN
Insert into Info values(@Hp_Number,
@Customer_Name,
@Customer_Contact_Number,
@Guarantor_Name,
@Guarantor_Contact_Number,
@Hp_Date,
@Customer_Address,
@Vehicle,
@Vehicle_Model,
@Vehicle_Number,
@Chasis_Number,
@Engine_Number,
@FC_Date,
@Insurance_Date,
@Insurance_Amount,
@Paid_Amount,
@Vehicle_Pic,
@Customer_Pic,
@Guarantor_Pic,
@Documents_Pic,
@Insurance_Pic)
END