In my DB I have a table dept table (see below) and in my application I have a form where a user has to add his info and chose a dept from which he works for. It works fine with the Id’s. Problem is that in my employee table I have “DeptID” as a foreign key, and it doesn’t make sense for a user to click the dropdown and see 1,2,3 etc…rather I want a user to see “IT, Accounting etc..” (of which I can populate the dropdown by selecting the DeptName), problem comes when I have to insert the info into the employee table. For example when the user selected IT from the dropdown I must insert 1 in my employee table bcoz IT =1
|
DeptID |
DeptName |
|
1 |
IT |
|
2 |
Accounting |
|
EmpID |
DeptID |
Name |
Surname |
|
001 |
2 |
Dorothy |
Ntombela |
|
002 |
1 |
Dorothy |
Maseko |
string cat = "Select DeptId from Department Where DeptName = '" + cboDept.Text + "' ";
int hello = Convert.ToInt32(cat);
command.CommandText = "Insert into Employee(EmpId, DeptId, Name, Surname) Values ('" + num + "', '" + hello + "' , '" + txtname.text + "', '" + txtSurname.text + "') " ;
Error : "input string was not in a correct format"
Someone please help
DorothyPosted Nov 7, 2006, 2:55 AM
savan gandhiPosted Nov 2, 2006, 9:34 AM
Scott LyslePosted Nov 2, 2006, 8:05 AM
Why don't you just use the selectd index value from the drop down list and add 1 to it? Assuming the values are in the correct order, this will directly give you the correct value without trying to figure it out from the string.
e.g.,
Private
Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChangedMessageBox.Show(ComboBox1.SelectedIndex + 1,
"Selection Number") End Sub