2
Answers

Whats wrong in my code????

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 (2)