Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have two tables, tbl1 have primary key with string, tbl2 have foreign key to that primary key ,when I try to insert into tbl2 value from combobox.Selected value
this error was occurred
Conversion failed when converting the nvarchar value 'FMCS17002' to data type int.

I try to use
CmbStudents.SelectedValue.ToString()
but not working 
and CmbStudents.SelectedItem
also not Working
Thank's ,,,

What I have tried:

cmd = New SqlCommand("insert into SupervisionAndExaminer values(@p1, @p2, @p3)", SqlSeverDB)

            cmd.Parameters.AddWithValue("@p1", CmbStudents.SelectedValue)
            cmd.Parameters.AddWithValue("@p2", CmbInstructorss.SelectedValue)
            cmd.Parameters.AddWithValue("@p3", CmbStudents.SelectedValue)
            cmd.ExecuteNonQuery()
Posted
Updated 11-May-17 0:24am
Comments
CHill60 11-May-17 5:48am    
The error is coming from your sql command. You must have a column defined as an int and you are trying to insert the value 'FMCS17002'.
Either parse the integer out of that text or change the column type on the database. I can't advise you further as we don't know anything about your table or the data you are trying to inser

The column where you are entering values has datatype integer

Make sure you are supplying data according to data type of a column of a table.

If you want to add "FMCS17002" then you must have to set datatype varchar(30) where 30 is a length of a character, You can set length according to your need, I have just set for example.

You can not insert this value as an integer.

I hope you got my point.
 
Share this answer
 
v2
The error is
Conversion failed when converting the nvarchar value 'FMCS17002' to data type int
So you don't want a string (what SelectedValue already is in your case) but an integer. At least one of the database fields you are trying to set has the type int and the string value "FMCS17002" can't be converted to an integer.

So you have to change the database to use strings (NVARCHAR) for the corresponding fields or change the combo boxes to contain only integers (or strings representing valid integers so that they can be converted).

[EDIT]
The question has been changed after posting my solution:
Quote:
I have two tables, tbl1 have primary key with string, tbl2 have foreign key to that primary key ,when I try to insert into tbl2 value from combobox.Selected value
You have to query the ID from tbl1 first using the corresponding combo box string:
SQL
SELECT id FROM tbl1 WHERE name=selected_value
and pass the returned ID as parameter to the INSERT command.
[/EDIT]
 
Share this answer
 
v2
Thank you my friends ^^
the error in my code I exchange the positions of comboboxes and repeat
CmbStudents.SelectedValue

and there is another error in function that fill my combobox
The Problem was solved ^^
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900