mithun kumar

mithun kumar

  • NA
  • 21
  • 3.6k

i am trying to insert the data from the form to database

Sep 30 2016 3:49 AM
Here i am trying the code to to insert the data from form to database sql server. it should store the data once i click on submit button.
 
here is the code for submit button i have tried
 
private void button2_Click(object sender, EventArgs e)
{
string cs = @"Data Source=.;Database=master;Integrated Security=True";
if(textBox1.Text=="")
{
MessageBox.Show("All fields are manditory to Enter");
return;
}
try
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("insert into invoice values ("+textBox3.Text+","+textBox4.Text+","+textBox5.Text+","+textBox6.Text+",", con);
cmd.Parameters.AddWithValue("@invoice", textBox1.Text);
cmd.Parameters.AddWithValue("@prepareby", textBox2.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
//If count is equal to 1, than show frmMain form
if (count > 0)
{
}
else
{
MessageBox.Show("Submitted Succesful");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
 
 
 
 

Answers (2)