Answered by:
What's wrong in my code???

Question
-
Hi Everyone..
Can any one tell me whats wrong in my code...I would like to store images into my database,but I'm not able to do that.
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[] img = null;
FileStream fs = new FileStream(imageloc, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
img = br.ReadBytes((int)fs.Length);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Hp_Number", SqlDbType.NVarChar, 50).Value = tbhpnum.Text; ;
cmd.Parameters.Add("@Customer_Name", SqlDbType.VarChar, 50).Value = tbcusnam.Text;
cmd.Parameters.Add("@Customer_Contact_Number", SqlDbType.NVarChar, 15).Value = tbcusmblno.Text;
cmd.Parameters.Add("@Guarantor_Name", SqlDbType.VarChar, 50).Value = tbguanam.Text;
cmd.Parameters.Add("@Guarantor_Contact_Number", SqlDbType.NVarChar, 15).Value = tbguamblno.Text;
cmd.Parameters.Add("@Hp_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbhpdat.Text);
cmd.Parameters.Add("@Customer_Address", SqlDbType.NVarChar, Max).Value = tbcusadd.Text;
cmd.Parameters.Add("@Vehicle", SqlDbType.VarChar, 50).Value = tbveh.SelectedItem.ToString();
cmd.Parameters.Add("@Vehicle_Model", SqlDbType.VarChar, 50).Value = tbvehmod.SelectedItem.ToString();
cmd.Parameters.Add("@Vehicle_Number", SqlDbType.NVarChar, 50).Value = tbvehnum.Text;
cmd.Parameters.Add("@Chasis_Number", SqlDbType.NVarChar, 50).Value = tbchanum.Text;
cmd.Parameters.Add("@Engine_Number", SqlDbType.NVarChar, 50).Value = tbengnum.Text;
cmd.Parameters.Add("@FC_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbfcdat.Text);
cmd.Parameters.Add("@Insurance_Date", SqlDbType.DateTime).Value = DateTime.Parse(tbinsdat.Text);
cmd.Parameters.Add("@Insurance_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbinsamt.Text);
cmd.Parameters.Add("@Paid_Amount", SqlDbType.Int).Value = Convert.ToInt32(tbpaiamt.Text);
cmd.Parameters.Add("@Vehicle_Pic", SqlDbType.Image).Value = boxvehpic;
cmd.Parameters.Add("@Customer_Pic", SqlDbType.Image).Value = boxcuspic;
cmd.Parameters.Add("@Guarantor_Pic", SqlDbType.Image).Value = boxguapic;
cmd.Parameters.Add("@Documents_Pic", SqlDbType.Image).Value = boxdocpic;
cmd.Parameters.Add("@Insurance_Pic", SqlDbType.Image).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();
}
}
}
}
Thanks & Regards RAJENDRAN M
Answers
-
I resolved this error of my self.......
Thanks & Regards RAJENDRAN M
- Proposed as answer by Cor Ligthert Saturday, January 10, 2015 2:38 PM
- Marked as answer by Caillen Tuesday, January 20, 2015 2:33 AM
All replies
-
-
You have this:
byte[] img = null; FileStream fs = new FileStream(imageloc, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); img = br.ReadBytes((int)fs.Length);
But you are adding these images:
cmd.Parameters.Add("@Vehicle_Pic", SqlDbType.Image).Value = boxvehpic; cmd.Parameters.Add("@Customer_Pic", SqlDbType.Image).Value = boxcuspic; cmd.Parameters.Add("@Guarantor_Pic", SqlDbType.Image).Value = boxguapic; cmd.Parameters.Add("@Documents_Pic", SqlDbType.Image).Value = boxdocpic; cmd.Parameters.Add("@Insurance_Pic", SqlDbType.Image).Value = boxinspic;
I don't know where those "...pic" variables are defined, but you're not adding the "img" variable that you just created.
~~Bonnie DeWitt [C# MVP]
-
Start with using the parameter AddWithValue instead of that old 2003 way.
Success
Cor -
I've created 5 pictureboxes for my understanding purpose I've given the names for that in properties,
1.vehicel image picturbox =boxvehpic
2.customer image picturbox=boxcuspic
3.guarantor image picturbox=boxguapic
4.documents image picturbox=boxdocpic
5.Insurance image picturebox=boxinspic
Thanks & Regards RAJENDRAN M
-
-
I resolved this error of my self.......
Thanks & Regards RAJENDRAN M
- Proposed as answer by Cor Ligthert Saturday, January 10, 2015 2:38 PM
- Marked as answer by Caillen Tuesday, January 20, 2015 2:33 AM
-
We appreciate that if you can share your solution here, it'll help others who have similar questions.
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -