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



I want to execute the below query using the parameters @Data and @Id taken from the UI.


C#
query = string.Format("update tablename set Confirmed=0 where data in (@Data) and id=@Id");
                    sqlParameters = new SqlParameter[2];
                    sqlParameters[0] = new SqlParameter("@Data", SqlDbType.VarChar);
                    sqlParameters[0].Value = Convert.ToString(_data);                    
                    sqlParameters[1] = new SqlParameter("@Shopid", SqlDbType.Int);
                    sqlParameters[1].Value = Convert.ToString(_shopid);


value for the parameters from UI are given below:

@Data=123,456
@Id=1

My problem is, I get the below error message when I run the application:

"Conversion failed when converting the varchar value '123,456' to data type int."

Please help me fix this.

Thanks in advance.
Posted
Comments
Herman<T>.Instance 2-Oct-13 1:41am    
change the , to a . and then you know that you're not working with a integer
Rahul Krishnan R 2-Oct-13 1:50am    
123 and 456 are two different values which cannot be separated using a.

hi,
Update your query like this:
C#
query = string.Format("update tablename set Confirmed=0 where data in ('+CAST( (@Data) AS VARCHAR) +') and id=@Id");
                    sqlParameters = new SqlParameter[2];
                    sqlParameters[0] = new SqlParameter("@Data", SqlDbType.VarChar);
                    sqlParameters[0].Value = Convert.ToString(_data);                    
                    sqlParameters[1] = new SqlParameter("@Shopid", SqlDbType.Int);
                    sqlParameters[1].Value = Convert.ToString(_shopid);
 
Share this answer
 
You are atempting dynamic SQL and SQL is trying to cast the @Data parameter to an integer value. Rather try something like this:

C#
query = string.Format("update tablename set Confirmed=0 where data in ({0}) and id=@Id", Convert.ToString(_data));
                    sqlParameters = new SqlParameter[1];
                    sqlParameters[0] = new SqlParameter("@Shopid", SqlDbType.Int);
                    sqlParameters[0].Value = Convert.ToString(_shopid);
 
Share this answer
 
Comments
Rahul Krishnan R 2-Oct-13 2:09am    
Thank you very much Eon Malherbe. You reply saved my day..

Also, could you pls explain me what {0} means in the query. I am a beginner in c#.
Eon Malherbe 31-Oct-13 1:40am    
The {x} is a placeholder used in string.Format. {0} is the first parameter, {1} the second etc. For example: string.Format("the {0} car drove {1} mph", "red", 30) will result in "the red car drove 30 mph".
SQL
query = string.Format("update tablename set Confirmed=0 where data in ('+CAST( (@Data) AS VARCHAR(20)) +') and id=@Id");
                    sqlParameters = new SqlParameter[2];
                    sqlParameters[0] = new SqlParameter("@Data", SqlDbType.VarChar,20);
                    sqlParameters[0].Value = Convert.ToString(_data);
                    sqlParameters[1] = new SqlParameter("@Shopid", SqlDbType.Int);
                    sqlParameters[1].Value = Convert.ToString(_shopid);
 
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