Hello,
In combobox item selected, that item save in database table and display the datagridview. How it is possible? I try this but not working.bellow this code...
Code:
SqlConnection cn;
SqlCommand cmd;
DataSet ds;
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("MCA");
comboBox1.Items.Add("MBA");
comboBox1.Items.Add("M-Tech");
comboBox1.Items.Add("B-Tech");
//Sql connection
SqlConnection cn = new SqlConnection("Database=suresh;server=SURESH\\SQLEXPRESS;integrated security=true");
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from sub", cn);
ds = new DataSet();
da.Fill(ds, "sub");
datagridview1.DataSource = ds.Tables[0];
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
cmd = new SqlCommand("insert into sub values('" + comboBox1.SelectedItem + "')", cn);
if (cn.State == ConnectionState.Closed) { cn.Open(); }
cmd.ExecuteNonQuery();
cn.Close();
SqlDataAdapter da = new SqlDataAdapter("Select * from sub", cn);
ds = new DataSet();
da.Fill(ds, "sub");
}