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();
        }




    }
}

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.