Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 帮助解决错误_C# - Fatal编程技术网

C# 帮助解决错误

C# 帮助解决错误,c#,C#,我正在从数据表中读取一行,并试图插入到数据库中。在行中,某些单元格/列不包含值。所以,我试图首先检查行是否包含值,然后插入数据库。这是我的密码 string barcode = null; string itemName = null; double cost = 0; double price = 0; double Stock = 0; string dept = null; double tax = 0; barcode = Convert.ToString(dr[0]); if (d

我正在从数据表中读取一行,并试图插入到数据库中。在行中,某些单元格/列不包含值。所以,我试图首先检查行是否包含值,然后插入数据库。这是我的密码

string barcode = null;
string itemName = null;
double cost = 0;
double price = 0;
double Stock = 0;
string dept = null;
double tax = 0;

barcode = Convert.ToString(dr[0]);

if (dr[1] != string.Empty || dr[1] != null)
    itemName = Convert.ToString(dr[1]);

if (dr[2] != string.Empty || dr[2] != null)
    cost = Convert.ToDouble(dr[2]);

if (dr[3] != string.Empty || dr[3] != null)
    price = Convert.ToDouble(dr[3]);

Stock = Convert.ToDouble(dr[4]);

if (dr[5] != string.Empty || dr[5] != null)
    dept = Convert.ToString(dr[5]);

if (dr[6] != string.Empty || dr[6] != null)
    tax = Convert.ToDouble(dr[6]);
但是,我得到了一个错误:

可能是意外的引用比较,以获得值比较

我怎样才能纠正这个问题

if (dr[1] != string.Empty || dr[1] != null)

将始终返回
true
,即使字符串为空或null也是如此。这是因为某些内容不能同时为string.Empty和null!您应该使用
&&
而不是
|

首先,发布您的错误

其次,
String.IsNullOrEmpty()
非常成功

出现此错误是因为
dr[1]
etc.返回的是一个对象,而不是字符串-必须将其转换为字符串或调用
ToString()
,才能进行正确的字符串比较。另外(参见@antisanity的答案)您可能只需要

if (!string.IsNullOrEmpty( dr[1].ToString()))
if (!string.IsNullOrEmpty( dr[1].ToString()))