Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a drop down list with Names Active ,Assigned etc..

This i am adding in code behind i am getting a error like conversion failed.
C#
cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = ddlState.SelectedItem.Value;
This is some part of my procedure
SQL
else if(@Status!=0  AND @GroupId = 0)
Begin
select A.GroupName , B.Brand , B.Model , B.SerialNo , B.Status, B.AddedOn from
GroupDetails as A
inner join
DeviceDetails as B
on A.GroupId = B.GroupId where SerialNo like  '%%' AND B.Status = @Status
End

error is caused due to this..

please rectify me..
suggestions??
help??

What I have tried:

Where i am going wrong, how to convert it.. please help
Posted
Updated 21-Feb-17 23:22pm
v2
Comments
Graeme_Grant 22-Feb-17 4:58am    
Please share the error message in your question by clicking on the "Improve question" to the right of your name.
F-ES Sitecore 22-Feb-17 5:17am    
What type is @Status? You are adding it as a varchar yet using it as an int in the SP by comparing it to 0. If @Status is "Hello World" then how can you compare that to 0?

If @Status is an int then make it so in the SP, then convert ddlState.SelectedItem to an int when you add it to the Parameters collection and change the type of the parameter too.

To preempt your next question google "c# convert string to int"

Why are you passing a varchar value to a parameter you are using as an integer?
Because that is what you are doing - when you do an integer comparison:
SQL
if(@Status!=0  AND @GroupId = 0)
the system attempts to convert the string in @Status to an integer value. And in this case, it fails becuase whatever you passed isn't an integer.

Either change the test to compare strings:
SQL
if(@Status != '0'  AND @GroupId = 0)

Or better, find out what is in your dropdown list and either correct the source, or check for that.
 
Share this answer
 
From code, you are sending a NVarchar, but using that to compare with an integer, which is completely wrong.

You have to make them sync. Use one data type at both ends.
 
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