我在Web服务调用中有一条if语句,该语句始终返回false。如果其中没有单词“ VERTICALPALLETS”或“ PALLETLABELS”,我希望它返回一条错误消息。即使此字段中包含这些单词,它仍会返回错误。该查询不是我在应用程序中使用的正确查询。任何帮助深表感谢。
public bool ValidateDevice(string DeviceID, string sVersion, out string MachineID, out string MachineName, out string Plant, out string Printer1, out string Printer2, out string WrapStation, out string Location, out string sMessage)
{
MachineID = "";
MachineName = "";
Plant = "";
Printer1 = "";
Printer2 = "";
Plant = "";
WrapStation = "";
Location = "";
sMessage = "";
bool bTest = CheckVersion(sVersion, out sMessage);
if (bTest == false)
{
sMessage = "You do not meet the minimum version requirements. Contact MIS.";
return false;
}
try
{
SqlConnection connection = new SqlConnection(capmSADPConnectionString);
connection.Open();
string queryString = "select * from DATABASE";
SqlCommand command = new SqlCommand(queryString, connection);
command.Parameters.Clear();
command.Parameters.AddWithValue("@DEVICEID", DeviceID);
command.Parameters.AddWithValue("@APPID", "VTP");
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows == false)
{
email myEmail = new email();
myEmail.SendErrorEmail("No Application Settings for VTP Device ID - " + DeviceID, "VTPError", "VTP Device Settings Error");
sMessage = "Could not find application settings for this device. Please enter settings.";
return false;
}
reader.Read();
MachineID = reader.GetValue(0).ToString();
MachineName = reader.GetValue(1).ToString();
Plant = reader.GetValue(2).ToString();
Location = reader.GetValue(3).ToString();
Printer1 = reader.GetValue(4).ToString();
Printer2 = reader.GetValue(5).ToString();
WrapStation = reader.GetValue(6).ToString();
reader.Close();
reader = null;
command = null;
connection.Close();
connection = null;
if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) || (Location.ToUpper().Contains("PALLETLABELS") == false))
{
sMessage = "Please enter correct location in Application Setup and try again.";
return false;
}
return true;
}
catch (Exception e)
{
sMessage = "Unable to retrieve Application Settings for VTP. Please reopen the application.";
return false;
}
return true;
}
答案 0 :(得分:5)
这里的问题与您的if
语句的结构有关:
if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) || (Location.ToUpper().Contains("PALLETLABELS") == false))
{
sMessage = "Please enter correct location in Application Setup and try again.";
return false;
}
您在说:
If the location does not contain 'VERTICALPALLETS'
Or Else
If the location does not contain 'PALLETLABELS'
除非位置包含文本的两位,否则它将始终为真!
您需要将Or Else
更改为And Also
(因此将||
更改为&&
),因此您的代码将按预期工作。
答案 1 :(得分:1)
使用AND
if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) && (Location.ToUpper().Contains("PALLETLABELS") == false))
更易读的替代解决方案。
if (!(Location.ToUpper().Contains("VERTICALPALLETS") || Location.ToUpper().Contains("PALLETLABELS"))
答案 2 :(得分:1)
此行
if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) || (Location.ToUpper().Contains("PALLETLABELS") == false))
检查Location.ToUpper()
不包含“ VERTICALPALLETS”还是Location.ToUpper()
不包含“ PALLETLABELS”。如果Location.ToUpper()
不能同时包含两者,则条件为true。相反,您想检查Location.ToUpper()
是否包含其中两个:
if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) && (Location.ToUpper().Contains("PALLETLABELS") == false))
{
//...
}
另外,多次计算ToUpper
并没有多大意义,让我们只计算一次:
String upperLocation = Location.ToUpper();
if ((Location.ToUpper().Contains("VERTICALPALLETS") == false) && (Location.ToUpper().Contains("PALLETLABELS") == false))
{
//...
}
答案 3 :(得分:0)
听起来像是问题,除非您要查找同时包含VERTICALPALLETS PALLETLABELS的位置,否则您将一直在寻找“ false”或“ false”条件
我建议您尝试类似
if (! ( condtion1 || condition2) )
return message