Insert ID into SQL from C# Combo box

微笑、不失礼 提交于 2021-01-29 20:45:17

问题


I have a dropdown(combo) box in a C# program I'm writing. -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. (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.

-I have tried: CAST CONVERT

Converting the valuemember to int in the app before the insert statement, still the same error.

What am I doing wrong? -Its not trying to convert the actual string 'ValueMember' to int is it??


回答1:


I think you need SelectedValue to retrieve the selected value from the combo box, so:

App.SelectedValue

SelectedValue Property: Gets or sets the value of the member property specified by the ValueMember property.

You can set a breakpoint on retrieving the value to check what is returned to make sure that it is a number.




回答2:


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. 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);
          .....


来源:https://stackoverflow.com/questions/24293920/insert-id-into-sql-from-c-sharp-combo-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!