使用c#将数据库中的新值加载到组合框中 [英] Loading new values from database into combo box using c#

查看:102
本文介绍了使用c#将数据库中的新值加载到组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows应用程序,我有两个表单第一个表单,其中包含用于从数据库检索数据的组合框,第二个表单包含用于将数据保存在数据库中的文本框。我已经创建了第一种形式的方法,它执行检索数据并加载到组合框中的工作,然后我在第二种形式中调用相同的方法,其中我将数据从文本框保存到数据库。我写的方法如下: -

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:-

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 !!!

推荐答案

每次从数据库加载新数据之前清除数据集。以及你的组合框。

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


嗨亲爱的,



查看此解决方案。



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


这篇关于使用c#将数据库中的新值加载到组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆