将var char值active转换为int时转换失败 [英] Conversion failed while converting var char value active to int

查看:86
本文介绍了将var char值active转换为int时转换失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,名称有效,已分配等。



这是我添加代码后面我收到错误,如转换失败。

 cmd.Parameters.Add(  @ Status,SqlDbType.NVarChar).Value = ddlState.SelectedItem.Value; 

这是我程序的一部分

  else   if (@ Status!= 0  AND   @ GroupId  =  0 
开始
选择 A.GroupName,B.Brand,B.Model,B.SerialNo,B.Status,B.AddedOn from
GroupDetails as A
inner join
DeviceDetails as B
A.GroupId = B.GroupId 其中 SerialNo < span class =code-keyword> like ' %%' AND B.Status = @状态
结束


由于这个原因导致
错误..



请纠正我..

建议??

帮助??



我尝试过:



我哪里错了,如何转换它...请帮助

解决方案

为什么要将varchar值传递给a你用作整数的参数?

因为你正在做的事情 - 当你进行整数比较时:

  if (@ Status!= 0  AND   @ Gro upId  =  0 

系统尝试将@Status中的字符串转换为整数值。在这种情况下,它会失败,因为你传递的不是整数。



要么改变测试来比较字符串:

< pre lang =SQL> if @ Status != ' 0' AND @ GroupId = 0



或者更好,找出下拉列表中的内容并更正来源,或检查。


从代码中,您发送的是一个NVarchar,但使用它来与一个整数进行比较,这是完全错误的。



你必须使它们同步。在两端使用一种数据类型。


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.

cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = ddlState.SelectedItem.Value;

This is some part of my procedure

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

解决方案

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:

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:

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.


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.


这篇关于将var char值active转换为int时转换失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆