简体   繁体   中英

Inserting value from a combobox into a database

I am attempting to insert the record into m sql database. My very first value is 1 then 1001. My insert will place the first value which is 1 correctly but 1001 and beyond it returns null. How can I change this to read the value and not use the index since it does not go in sequential order?

Insert Method:

SqlCommand addJob = new SqlCommand(@"INSERT INTO JobNumber ( JobID, CustomerID, JobDescription, IsActive) VALUES ( @JobID, (SELECT @CustomerID FROM Customer C WHERE C.CustomerID = @CustomerID), @JobDescription, @IsActive)", dbConn);
addJob.Parameters.AddWithValue("@JobID", Convert.ToInt32(jobidTextBox.Text));
addJob.Parameters.AddWithValue("@CustomerID", Convert.ToInt32(customeridComboBox.SelectedIndex) + 1);
addJob.Parameters.AddWithValue("@JobDescription", jobdescriptionTextBox.Text);
addJob.Parameters.AddWithValue("@IsActive", isactiveCheckBox.Checked);
dbConn.Open();
addJob.ExecuteNonQuery();

尝试使用组合框的选定文本/选定项目属性而不是索引。

您应该像这样检索文本项

addJob.Parameters.AddWithValue("@CustomerID", Convert.ToInt32(customeridComboBox.Items[customeridComboBox.SelectedIndex+ 1] );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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