Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am working on windows application, I have two forms first form which contains combobox to retrieve data from database and second form which contains textbox to save data in database. I have created a method in first form which does the work of retrieving the data and load in combobox and then I called the same method in second form where I save the data from textbox to database. The method I wrote is as follows:-
C#
public void Bank_Name()
        {
            //Connection String
            SqlConnection cone = new SqlConnection(cnstd);
            cone.Open();
            SqlDataAdapter adpi = new SqlDataAdapter("Select distinct Cust_Name from Customer", cone);
            DataSet ds = new DataSet();
            adpi.Fill(ds);
            cmbCust_Name.DataSource = ds.Tables[0];
            cmbCust_Name.DisplayMember = "Cust_Name";
            cone.Close();
        }

In debug mode when I run the project the when I save the value from textbox, it can be seen in dataset, but the combobox is not getting the new value even though it is loaded in dataset.

Thanks in Advance !!!
Posted
Comments
vivektiwari97701 5-Dec-12 1:05am    
debug ur code and find exactly where is the problem...
Sergey Alexandrovich Kryukov 5-Dec-12 1:05am    
Where is the combo box in your code? Always show all relevant declarations...
--SA
Ank_ush 5-Dec-12 1:12am    
cmbCust_Name is the name of combobox
Ank_ush 5-Dec-12 1:15am    
In debug mode when I place the mouse on adpi.fill(ds), i.e. adadpter.fill(Dataset) the newly added value is loaded, but in combobox, it is not shown.
vivektiwari97701 5-Dec-12 1:25am    
just try to use DataTable in place of DataTable and set ValueMember property of Combo Box..

DataTable ds = new DataTable();
adpi.Fill(ds);
cmbCust_Name.DataSource = ds;
cmbCust_Name.DisplayMember = "Cust_Name";
cmbCust_Name.ValueMember ="Cust_Name"
cone.Close();
}

make sure after binding combo box u r not doing any clear operation.and ur calling this method in right event...

clear your dataset every time before it loads new data from database. and also your combobox.
public void Bank_Name()
        {   
            cmbCust.items.clear();
            DataSet ds = new DataSet();
            Ds.Clear();
            //Connection String
            SqlConnection cone = new SqlConnection(cnstd);
            cone.Open();
            SqlDataAdapter adpi = new SqlDataAdapter("Select distinct Cust_Name from Customer", cone);           
            adpi.Fill(ds);
            cmbCust_Name.DataSource = ds.Tables[0];
            cmbCust_Name.DisplayMember = "Cust_Name";
            cone.Close();
        }
 
Share this answer
 
Comments
Ank_ush 5-Dec-12 1:37am    
Not Working...
hi dear,

check this solution.

SqlConnection cone = new SqlConnection(cnstd);
string SQL = "Select distinct Cust_Name from Customer";
SqlCommand cmd = new SqlCommand(SQL, cone);            
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
  comboBox1.DataSource = dt;
  comboBox1.DisplayMember = "Cust_Name";
  comboBox1.ValueMember = "Cust_Name";               
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900