Hi,
I am selecting value from one combobox and want to use its value to update the second combobox. but the error is that the second combobox is not showing any value. plz help me.

namespace ShopSoftware
{
    public partial class frmSelling : Form
    {
        public frmSelling()
        {
            InitializeComponent();
        }
        clsConnection clsConn = new clsConnection();
        SqlCommand sqlCmd = new SqlCommand();
        SqlCommand sqlCmd1 = new SqlCommand();
        SqlCommand sqlCmd2 = new SqlCommand();
        SqlCommand sqlCmd3 = new SqlCommand();


        private void frmSelling_Load(object sender, EventArgs e)
        {


            BindData1();
        }





        public void BindData1()
        {
            sqlCmd.Connection = clsConn.conn;
            sqlCmd.CommandText = "select cPName from Product  ";
            sqlCmd.CommandType = CommandType.Text;
            //sqlConn.Open();
            clsConn.GetConnection(); //to open and close sql connection i have made class for it.

            SqlDataAdapter sqlAdpt = new SqlDataAdapter();
            sqlAdpt.SelectCommand = sqlCmd;
            sqlAdpt.SelectCommand.ExecuteNonQuery();

            DataSet ds = new DataSet();
            sqlAdpt.Fill(ds);
            sqlCmd.ExecuteNonQuery();
            clsConn.GetConnection();

            cbPName.DisplayMember = "cPName";
            cbPName.ValueMember = "iSrNo";
            cbPName.DataSource = ds.Tables[0];

            cbPName.Enabled = true;

        }



        private void cbBName_ValueMemberChanged(object sender, EventArgs e)
        {
            clsConn.GetConnection();
            sqlCmd2.CommandText = "select iBno from Product where cPName='" + cbPName.SelectedText + "'";
            sqlCmd2.CommandType = CommandType.Text;
            sqlCmd2.Connection = clsConn.conn;
            SqlDataAdapter sqladpt = new SqlDataAdapter();
            sqladpt.SelectCommand = sqlCmd2;

            int j = 0;
            DataTable dt = new DataTable();
            sqladpt.Fill(dt);

            SqlDataReader reader = sqlCmd2.ExecuteReader();
            while (reader.Read())
            {

                if (!string.IsNullOrEmpty(reader["iBno"].ToString()))
                {
                    j = Convert.ToInt32(reader["iBno"].ToString());

                }

            }

            reader.Close();

            sqlCmd.Connection = clsConn.conn;
            sqlCmd.CommandText = "select cBrandName from brandName where  iBno =" + j ;
            sqlCmd.CommandType = CommandType.Text;


            cbBName.Items.Clear();

            SqlDataReader dr = sqlCmd.ExecuteReader();
            while (dr.Read())
            {               
                cbBName.Items.Add( dr["cBrandName"]);

            }

            clsConn.GetConnection();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }




    }
}

Dani AI

Generated

Short answer: the combo on the left is bound only to cPName while the code expects an iSrNo value, the selection handler is the wrong event and it uses SelectedText (which will usually be empty), and the code calls ExecuteNonQuery for SELECTs and mixes adapters/readers in a confusing way. Those facts explain why the second combobox never gets populated.

What to fix (step-by-step):

  • Make the products query return both the display and key columns (for example iSrNo and cPName), then set DisplayMember and ValueMember only after filling the DataTable.
  • Do not use SelectedText to get the selected item — use SelectedValue (or cast SelectedItem to a DataRowView and read the column). SelectedText is for in-place text selection and will often be empty.
  • Handle a selection event that fires on user change (SelectionChangeCommitted or SelectedIndexChanged), not ValueMemberChanged.
  • Use a parameterized query (or a join) to fetch brands for the selected product key; avoid string concatenation. Prefer setting the second combobox DataSource to the brand DataTable (and set its DisplayMember/ValueMember) rather than repeatedly calling Items.Add.
  • Remove unnecessary ExecuteNonQuery calls for SELECTs and adopt using blocks to open/close commands/readers/connection reliably.

Debugging tips (as suggested): set a breakpoint inside the selection event and inspect cbPName.SelectedValue, cbPName.Text, the constructed SQL and the adapter/DataTable row count. cbBName being empty does not prevent stepping through — the watch window or Debug.WriteLine/MessageBox will show variable values. ’s pattern (fill a second datasource based on the first combobox’s selected value) is correct in principle; apply the fixes above (correct columns, correct event, parameterized query) and the second combobox will populate.

Recommended Answers

All 4 Replies

If you debug your code, what is the value of j in the line that reads sqlCmd.CommandText = "select cBrandName from brandName where iBno = " + j;? When stepping through your code, also check that the line cbBName.Items.Add(dr["cBrandName"]); is actually being called (ie does your SQL query actually return any results?)

i can not debug it. because the cbBName has no value. Without that how can i debug it?

i can not debug it. because the cbBName has no value. Without that how can i debug it?

hye !

what I understand from your question is that you have comboA and comboB and when you select the comboA then records of comboB will filter ? am i right ? .
Try to do something like this .

//Let suppose we have two comboboxes , 1- cmbSelector 2-cmbFilter 

sqlconnection con = new sqlconnection("your connection string");
con.Open();
sqldataadapter da = new sqldataadapter("select TypeId,TypeName from tbl",con);
Datatable dt = new Datatable();
da.fill(dt);
cmbSelector.datasource = dt;
cmbSelector.DisplayMember = "TypeName";
cmbSelector.ValueMember = "TypeId";
con.Close();

//now here is the method which will populate the cmbFilter

private void GetFilter(int TypeId)
{
    sqlconnection con = new sqlconnection("your connection string");
    con.Open();
    sqldataadapter da = new sqldataadapter("select ProductId,ProductName from tbl2 where TypeId=" + TypeId,con);
    Datatable dt = new Datatable();
    da.fill(dt);
    cmbFilter.datasource = dt;
    cmbFilter.DisplayMember = "ProductName";
    cmbFilter.ValueMember = "ProductId";
    con.Close();
}

// now call above method on the selected index change event of cmbSelector.
//Like this

GetFilter(convert.toint16(cmbSelector.SelectedValue));


hope this will help you
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.