Jayita Roy

Jayita Roy

  • NA
  • 22
  • 3.5k

Data not storing in SQL from C# form

Jun 17 2015 2:53 AM
/***I have a table in SQL database with (Id,Visitor_Name,Organisation_Name,Phone_No,No_Person,To_whom,Purpose,Color_Code,Day_In,Time_In,Day_Out,Time_Out,Image)
Now I want to insert values of (Id,Visitor_Name,Organisation_Name,Phone_No,No_Person,To_whom,Purpose,Color_Code,Day_In,Time_In, Image) into database.
I wrote this code in C# below, it's working fine but not storing any data to SQL database. Please help.***/
 

private void button_Pass_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection("Data Source=JAYI-PC\\SQLEXPRESS;Initial Catalog=db-ub;Integrated Security=True");
con1.Open();

SqlCommand cmd1 = new SqlCommand("Insert into Visitors(Id,Visitor_Name,Organisation_Name,Phone_No,No_Person,To_whom,Purpose,Color_Code,Day_In,Time_In,Image) values (@id,@vname,@org,@phn,@nop,@whom,@prps,@ccd,@dI,@tI,@img)");
cmd1.Parameters.AddWithValue("@id",textBox_Id.Text);
cmd1.Parameters.AddWithValue("@vname",textBox_NameV.Text);
cmd1.Parameters.AddWithValue("@org",textBox_Org.Text);
cmd1.Parameters.AddWithValue("@phn",textBox_Phn.Text);
cmd1.Parameters.AddWithValue("@nop", numericUpDown_Person.Value.ToString());
cmd1.Parameters.AddWithValue("@whom",comboBox_TOWHOM.SelectedItem.ToString());
string prps;
if(radioButton_OFFICIAL.Checked)
{
prps = "Official";
cmd1.Parameters.AddWithValue("@prps",prps);
}
else if(radioButton_PERSONAL.Checked)
{
prps = "Personal";
cmd1.Parameters.AddWithValue("@prps",prps);
}
cmd1.Parameters.AddWithValue("@ccd",comboBox_color.SelectedItem.ToString());
cmd1.Parameters.AddWithValue("@dI",textBox_Dayin.Text);
cmd1.Parameters.AddWithValue("@tI",textBox_timeR.Text);
Image img = pictureBox_UB.Image;
byte[] arr;
ImageConverter converter = new ImageConverter();
arr = (byte[])converter.ConvertTo(img, typeof(byte[]));
cmd1.Parameters.AddWithValue("@img",img);
MessageBox.Show("Record has been inserted");
con1.Close();

Answers (11)