-
Nov 15th, 2017, 01:11 PM
#1
Thread Starter
Member
Saving to SQL issue
I have a web form that retrieves information from a SQL 2008 database, and some data has percentage symbols in it (%), which throws SQL errors when attempting to save. I am using parameterized queries to save, but the SQL stored Proc will not let me save. Can anybody tell me how to take the literal contents of a drop-down choice, including any characters like %, and write to the database without error?
The parameterized query is like this:
cmd.parameters.AddWithValue("@DBField", DDL1.selectedValue)
The stored proc is an update statement in the usual form
update table set column = @value
-
Nov 15th, 2017, 01:33 PM
#2
Re: Saving to SQL issue
What data type is the field in the database, and what is the data type of the value that you are putting into .AddWithValue?
Assuming that the field is VarChar based, either of these should fix it:
Code:
cmd.parameters.AddWithValue("@DBField", DDL1.selectedValue.ToString)
cmd.parameters.AddWithValue("@DBField", CStr(DDL1.selectedValue))
-
Nov 15th, 2017, 01:59 PM
#3
Thread Starter
Member
Re: Saving to SQL issue
Originally Posted by si_the_geek
What data type is the field in the database, and what is the data type of the value that you are putting into .AddWithValue?
Assuming that the field is VarChar based, either of these should fix it:
Code:
cmd.parameters.AddWithValue("@DBField", DDL1.selectedValue.ToString)
cmd.parameters.AddWithValue("@DBField", CStr(DDL1.selectedValue))
Thanks for that. The CStr did the trick!
-
Nov 15th, 2017, 05:54 PM
#4
Re: Saving to SQL issue
To be specific, AddWithValue infers the type of the parameter from the value that you provide. That means that you need to use a value that is compatible with the way the parameter is used in the SQL code.
It's also worth noting that many people recommend against using AddWithValue for text data especially but other places too. That's because a database like SQL Server has various types that can store text so AddWithValue simply makes a best guess. Sometimes that guess will be correct and other times it won't matter but there may be some occasions where it guesses the wrong type and that makes a difference in the SQL code that causes unexpected behaviour. If that's possible then it's better to call Add, specify the data type explicitly and then set the Value of the parameter.
-
Nov 16th, 2017, 09:48 AM
#5
Thread Starter
Member
Re: Saving to SQL issue
Originally Posted by jmcilhinney
To be specific, AddWithValue infers the type of the parameter from the value that you provide. That means that you need to use a value that is compatible with the way the parameter is used in the SQL code.
It's also worth noting that many people recommend against using AddWithValue for text data especially but other places too. That's because a database like SQL Server has various types that can store text so AddWithValue simply makes a best guess. Sometimes that guess will be correct and other times it won't matter but there may be some occasions where it guesses the wrong type and that makes a difference in the SQL code that causes unexpected behaviour. If that's possible then it's better to call Add, specify the data type explicitly and then set the Value of the parameter.
Thanks a lot for the tips..I appreciate it!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|