简体   繁体   English

从C#组合框中将ID插入SQL

[英]Insert ID into SQL from C# Combo box

I have a dropdown(combo) box in a C# program I'm writing. 我正在编写的C#程序中有一个下拉列表(combo)框。 -It pulls through its data from a table in my database: -它从数据库中的表中提取数据:

 Application_Id | Application
 ---------------|--------------
       1        |Name1
       2        |Name2
       3        |Name3

The code for the dropdown box looks like this: 下拉框的代码如下所示:

SqlConnection conn = new SqlConnection(SqlCon.DBCON);
conn.Open();
SqlCommand sc = new SqlCommand("select * from Application", conn);
SqlDataReader reader;

reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("ApplicationId", typeof(int));
dt.Columns.Add("Application_Name", typeof(string));
dt.Load(reader);

App.ValueMember = "ApplicationId";
App.DisplayMember = "Application_Name";
App.DataSource = dt;

conn.Close();

Basically, the user selects the name of the application and presses an 'Insert' button. 基本上,用户选择应用程序的名称并按下“插入”按钮。 I want this to then post the relating ID of that application to another table in my database. 然后,我希望将此应用程序的相关ID发布到数据库中的另一个表。 (Accounts table). (帐户表)。 -I did have this setup as a primary-foreign key relationship but to test I removed the relationship. -我确实将此设置设置为主键-外键关系,但是为了测试,我删除了该关系。

My code for inserting into the database is this: 我插入数据库的代码是这样的:

SqlConnection conn = new SqlConnection(SqlCon.DBCON);
conn.Open();
using (SqlCommand AQuery = conn.CreateCommand())
{
   AQuery.CommandText = "INSERT INTO Accounts(AccountName,Application_Id,num_Users) VALUES(@param1,@param2,@param3)";

   AQuery.Parameters.Add(new SqlParameter("@param1", account.Text));
   AQuery.Parameters.Add(new SqlParameter("@param2", App.ValueMember));
   AQuery.Parameters.Add(new SqlParameter("@param3", users.Text));

   AQuery.ExecuteNonQuery();
}

My issue, is that I'm getting an exception occur when I press Insert. 我的问题是,按“插入”时出现异常。 (Everything Compiles) (一切都编译)

The exception states: 异常指出:

Error Conversion failed when converting the nvarchar value 'ApplicationId' to data type int. 将nvarchar值'ApplicationId'转换为数据类型int时,错误转换失败。

-I have tried: CAST CONVERT -我尝试过:CAST CONVERT

Converting the valuemember to int in the app before the insert statement, still the same error. 在插入语句之前将值成员转换为应用程序中的int,仍然是相同的错误。

What am I doing wrong? 我究竟做错了什么? -Its not trying to convert the actual string 'ValueMember' to int is it?? -它不是试图将实际的字符串'ValueMember'转换为int吗?

The parameter should be initialized with 该参数应使用

AQuery.Parameters.Add(new SqlParameter("@param2", Convert.ToInt32(App.SelectedValue)));

Your code use the ValueMember property that is the name of the column ( a string) instead of the value. 您的代码使用ValueMember属性(而不是值)作为列名(字符串)。 However a bit of error checking should be added to be sure that something is selected in the combobox 但是,应添加一些错误检查,以确保在组合框中选择了某些内容

if(app.SelectedValue == null)
{
     MessageBox.Show("Select an app");
     return;
}

As a side note, when reading the values for the combobox, I suggest to use just the fields required to fill the combobox instead of loading all the fields and then manually building a datatable with only two columns 附带说明,在读取组合框的值时,我建议仅使用填充组合框所需的字段,而不是加载所有字段,然后手动构建只有两列的数据表

using(SqlConnection conn = new SqlConnection(SqlCon.DBCON))
using(SqlCommand sc = new SqlCommand(@"select ApplicationId, Application_Name 
                                       from Application", conn))
{
     conn.Open();
     using(SqlDataReader reader = sc.ExecuteReader())
     {
          DataTable dt = new DataTable();
          dt.Load(reader);
          .....

I think you need SelectedValue to retrieve the selected value from the combo box, so: 我认为您需要SelectedValue才能从组合框中检索选定的值,因此:

App.SelectedValue

SelectedValue Property: Gets or sets the value of the member property specified by the ValueMember property. SelectedValue属性:获取或设置ValueMember属性指定的成员属性的值。

You can set a breakpoint on retrieving the value to check what is returned to make sure that it is a number. 您可以在获取该值时设置一个断点,以检查返回的内容以确保它是一个数字。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM