Click here to Skip to main content
15,884,980 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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::

C#
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::

C#
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::


SQL
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
Posted
Updated 8-Jan-15 3:42am
v3

1 solution

Um...The only thing you use the array of bytes for is to get it's length:
C#
byte[] photo = GetPhoto(imageloc);
... 
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);

And an integer value isn't going to work as a byte[]...

Plus, if you are trying to load the same data to several images, send it as one parameter, and pass that multiple times - it's a lot more efficient, especially when images get quite large...
 
Share this answer
 
Comments
shaliniraji 8-Jan-15 6:35am    
Now which line I've to modify?
OriginalGriff 8-Jan-15 6:39am    
All of them below "..." above?
shaliniraji 8-Jan-15 7:30am    
I don't know how to modify...could u please explain it elaborately?????
OriginalGriff 8-Jan-15 7:40am    
No...because it's your code.
You wrote it, you produced the DB, and you know what you are trying to achieve.
I didn't, I don't know your DB or how you are planning to use it, and I don't know (apart from a small code fragment) what you are trying to do.

Why can't you modify your own code?
shaliniraji 8-Jan-15 8:01am    
I just want to store all the information along with images into my DB....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900