简体   繁体   English

将记录插入数据库C#之后,如何立即在组合框中显示主键?

[英]How to display primary key in a combobox immediately after insert records to the database c#?

I'm developing a windows form application to insert datagridview values to sqldatabase.I would like to know how to display inserted value's primary key(GRN_No) in a combobox immediately after insert.I tried this for display GRN_No when I load form as well as after insert command. 我正在开发一个Windows窗体应用程序以将datagridview值插入sqldatabase.I想知道如何在插入后立即在组合框中显示插入值的主键(GRN_No)。我在加载窗体以及在插入命令之后。

void fillGRNcombo()
    {
        DynamicConnection con = new DynamicConnection();
        con.mysqlconnection();
        con.sqlquery("select GRN_No from TBL_GRN");
        con.dataread();
        while (con.datareader.Read())
        {
            cmbGRN.Items.Add((int)con.datareader["GRN_No"]);
        }
    }

But after insertation values duplicated in combobox like(1,2,1,2,3).How to solve this? 但是插入值重复出现在组合框((1,2,1,2,3))中后,如何解决呢?

In your case your combo box persisting earlier data, that is the reason you can see repeated record in your combo box. 在您的情况下,组合框保留了较早的数据,这就是您可以在组合框中看到重复记录的原因。

Instead of adding same data again and again, just clear combo box before adding new primary keys or new records to it. 不必一遍又一遍地添加相同的数据,只需清除组合框,然后再向其添加新的主键或新记录。

  cmbGRN.Items.Clear();
  while (con.datareader.Read())
        {
            cmbGRN.Items.Add((int)con.datareader["GRN_No"]);
        }

Simply Clear ComboBox before Reading the DataReader 在读取DataReader之前只需清除ComboBox

  cmbGRN.Items.Clear();    
  while (con.datareader.Read())
  {
       cmbGRN.Items.Add((int)con.datareader["GRN_No"]);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ms访问数据库中插入带主键的记录c# - How to insert a record with a primary key in ms access database c# 插入C#后如何获取主键 - How to get the primary key after insert in C# 如何获取插入数据库中的序列/触发器主键并将其插入到我的类参数ID C#中 - How to get the sequence/trigger primary key that is inserted into the database and insert it into my class parameter id C# 如何使用LINQ和C#插入记录并返回该记录的主键 - How to insert a record with LINQ and C# and return the Primary Key of that record 如何将我在组合框中选择的内容插入到C#中的ACCESS数据库中 - How to insert what I selected in combobox into an ACCESS database in C# 如何在C#的列表框中显示除主键以外的所有列 - How to display all columns except the primary key in a listbox in c# 如何显示来自 c# 数据库的 combobox 中的特定数据? - How to display specific data in combobox coming from database in c#? 如何使用没有主键的数据库使用DataSet(C#和MySQL)进行更新 - How to update using DataSet (C# and MySQL) with the database with no primary key 如何在数据库C#中查找表的主键 - How to find primary key(s) of the table in database C# 如何在C#中使用一个命令将条目插入SQL Server数据库并返回包含主键标识的已插入列? - How do I insert an entry into a SQL Server database and return inserted columns including primary key identity with one command in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM