C# .NET - Plz Slove This Problem - Asked By suresh kotte on 05-Sep-11 01:08 AM

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");

      }
Anoop S replied to suresh kotte on 05-Sep-11 01:34 AM
Explain your question little more,
you have some values in combobox, you need to display data in datagrid based on this selection- > is it right?
suresh kotte replied to Anoop S on 05-Sep-11 01:36 AM
yes
Anoop S replied to suresh kotte on 05-Sep-11 01:46 AM
Refer this code

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{
string strQuery = "SELECT * FROM TableName WHERE ColumnName='" + comboBox1.SelectedValue + "' ";
FillData(strQuery );
}

private void FillData(string strQuery)

{

SqlConnection cn = new SqlConnection("CONNECTION STRING");
cn.Open();
string strQuery = strQuery;
SqlDataAdapter da = new SqlDataAdapter(strQuery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

}
Sharad Chandra replied to suresh kotte on 05-Sep-11 01:54 AM
try this and let me know
private void Form1_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        comboBox1.Items.Add("MCA");
        comboBox1.Items.Add("MBA");
        comboBox1.Items.Add("M-Tech");
        comboBox1.Items.Add("B-Tech");

        BindData("");
      }
    }
    void BindData(String subQuery)
    {
      SqlConnection cn = new SqlConnection("Database=suresh;server=SURESH\\SQLEXPRESS;integrated security=true");
      cn.Open();
      SqlDataAdapter da = new SqlDataAdapter("Select * from sub " + subQuery, cn);
      ds = new DataSet();
      da.Fill(ds, "sub");
      datagridview1.DataSource = ds.Tables[0];
    }
    
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      String subQuery = "where COLUMN_NAME="+comboBox1.SelectedValue;
      BindData(subQuery);
    }
Lata replied to Sharad Chandra on 05-Sep-11 02:17 AM
hi just remove the is postback condition from the above reply you ll get the result