Results 1 to 5 of 5

Thread: Saving to SQL issue

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    35

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    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))

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    35

    Re: Saving to SQL issue

    Quote Originally Posted by si_the_geek View Post
    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!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    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.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    35

    Re: Saving to SQL issue

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width