I have a system which uses SQL server management with Visual Studio. The combo box shows the users name in the drop down and when you select the name the ID associated with the record should be populated into the combo box. When I then press save to run an update in the SQL, the system tries to save the name as the ID. Which does not work as it is not an integer.
On load of the form this code runs to show the name. On the button how do I convert back to the ID
try {
var dcnn = new SqlConnection(dbconnection.DatabaseString);
dcnn.Open();
var command =
new SqlCommand("select StudentID, Firstname, Surname from student;", dcnn);
using (SqlDataReader reader = command.ExecuteReader()) {
cbxstudent.DisplayMember = "Text";
cbxstudent.ValueMember = "Value";
var items = new List<Object>();
while (reader.Read()) {
items.Add(new { Text = reader[1].ToString() + " " + reader[2].ToString(),
Value = reader[0].ToString() });
}
cbxstudent.DataSource = items;
}
dcnn.Close();
}
catch (Exception ex) {
MessageBox.Show("" + ex);
}