Click here to Skip to main content
15,887,888 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a task which deals with updating dropdownlist. When the value is changed in dropdown list it has to change in database. So the condition for which the value is to be updated is to be retrieved from another table . I tried that but it is throwing a error messaage

"Conversion failed when converting the varchar value to data type int".

C#
protected void Button3_Click(object sender, EventArgs e)
    {
con.open(); 
cmd = new SqlCommand("update marriage_configuration set page_theme = ('" + DropDownList1.SelectedItem + "') where u_e_id=(select R_id from users where Email_id=R_id)", con);
}
Posted
Updated 8-Nov-12 4:45am
v2
Comments
jim lahey 8-Nov-12 10:47am    
what type is the column page_theme?
[no name] 8-Nov-12 10:48am    
You have a type mismatch in SQL check to see if u_e_id and R_id are the same type then if Email_id and R_id are the same type then if page theme is varchar.
Raghavanand 8-Nov-12 10:48am    
page_theme datatype 'int'

1) Use Paramterized Commands to do your work. You are wide open to sql injection this way.
2) DropDownList1.SelectedItem will return an object, not an integer as your query expects. The easiest solution would be to set the ListItem.Value property to an integer.ToString() for each item in your list. Then when you build your query you can use the Convert.ToInt32(DropDownList1.SelectedValue) as your argument. I prefer the following syntax for parsing an integer as opposed to the Convert.ToInt32 syntax.
C#
int selectedValue;
if( Int32.TryParse( DropDownList1.SelectedValue, out selectedValue ) )
   // Do your query
else
   // The TryParse failed so you have an invalid selectedValue.
 
Share this answer
 
v3
If page_theme is an int, you need to supply it an int for your update to work:

C#
protected void Button3_Click(object sender, EventArgs e)
    {
con.open();
cmd = new SqlCommand("update marriage_configuration set page_theme = " + DropDownList1.SelectedItem.Value + " where u_e_id=(select R_id from users where Email_id=R_id)", con);
}


Quoting a value you concatenate into a sql string means it won't be interpreted as an integer, more likely a string type of some sort.

Also, don't ever concatenate SQL strings like that. You're just asking to get hacked by means of SQL injection. Use parameterized queries:

https://www.owasp.org/index.php/SQL_Injection[^]
 
Share this answer
 
v2

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