我遇到了与用户和数据库输入值进行比较的问题。
这是变量
List <string> onewayorigin = new List<string>();
List <string> onewaydestination = new List<string>();
List <string> onewayterminal = new List<string>();
List <string> onewaydeparture = new List<string>();
这是我与数据库的连接以获取值。
string strSelect;
SqlCommand cmdSelect;
SqlDataReader dtr;
SqlConnection conCust;
string connStr = ConfigurationManager.ConnectionStrings["BusConnectionString"].ConnectionString;
conCust = new SqlConnection(connStr);
strSelect = "Select * From Route";
cmdSelect = new SqlCommand(strSelect, conCust);
conCust.Open();
dtr = cmdSelect.ExecuteReader();
while (dtr.Read())
{
onewayorigin.Add(dtr["OneWayOrigin"].ToString());
onewaydestination.Add(dtr["OneWayDestination"].ToString());
onewayterminal.Add(dtr["OneWayTerminal"].ToString());
onewaydeparture.Add(dtr["OneWayDepartureTime"].ToString());
}
string[] origin = onewayorigin.ToArray();
string[] destination = onewaydestination.ToArray();
string[] ow_terminal = onewayterminal.ToArray();
string[] ow_depart = onewaydeparture.ToArray();
我在这里进行比较。
if (ow_terminal.Equals(ddlterminal.SelectedValue) && ow_depart.Equals(ddlDeparture.SelectedValue))
{
if (origin.Equals(ddlOrigin.SelectedValue) && destination.Equals(ddlDestination.SelectedValue))
{
lblMessage.Text = "Record exist";
}
}
else
{
lblMessage.Text = "No record exist";
}
是的,我承认我是新人。
答案 0 :(得分:1)
问题是您正在比较数组和字符串。这将始终为您提供false
。
请改用Contains
。如下图所示:
if (ow_terminal.Contains(ddlterminal.SelectedValue) && ow_depart.Contains(ddlDeparture.SelectedValue))
{
if (origin.Contains(ddlOrigin.SelectedValue) && destination.Contains(ddlDestination.SelectedValue))
{
lblMessage.Text = "Record exist";
}
}
else
{
lblMessage.Text = "No record exist";
}
答案 1 :(得分:1)
您的比较失败,因为您正在比较具有单个值的完整数组:
ow_terminal.Equals(ddlterminal.SelectedValue)
^ this is an array ^ this is properly a string
要解决此问题,您需要遍历数组以确保元素(在数组中)与ddlterminal.SelectedValue
示例:
foreach(string terminal in ow_terminal)
{
if(terminal == ddlterminal.SelectedValue)
{
...
但是使用Contains
方法可以更轻松地完成此操作:
if(ow_terminal.Contains(ddlterminal.SelectedValue) && ...)
答案 2 :(得分:0)
在列表中添加后,您需要为项目提供索引。 因此,在比较时,您应该考虑项目索引,以便比较相应的值。 您可以使用ArrayList类
ArrayList onewayorigin = new ArrayList() ;
ArrayList onewaydestination = new ArrayList() ;
ArrayList onewayterminal = new ArrayList() ;
ArrayList onewaydeparture = new ArrayList() ;
string strSelect;
SqlCommand cmdSelect;
SqlDataReader dtr;
SqlConnection conCust;
string connStr = ConfigurationManager.ConnectionStrings["BusConnectionString"].ConnectionString;
conCust = new SqlConnection(connStr);
strSelect = "Select * From Route";
cmdSelect = new SqlCommand(strSelect, conCust);
conCust.Open();
dtr = cmdSelect.ExecuteReader();
while (dtr.Read())
{
i = 0;
onewayorigin.Insert(i, dtr["OneWayOrigin"].ToString());
onewaydestination.Insert(i, dtr["OneWayDestination"].ToString());
onewayterminal.Insert(i, dtr["OneWayTerminal"].ToString());
onewaydeparture.Insert(i, dtr["OneWayDepartureTime"].ToString());
i++
}
//to compare
for (int iIndex = 0; iIndex < arr.Count; iIndex++)
{
object o = arr[iIndex];
if (ow_terminal[iIndex].ToString().Contains(ddlterminal.SelectedValue) && ow_depart[iIndex].ToString().Contains(ddlDeparture.SelectedValue))
{
if (onewayorigin[iIndex].ToString().Contains(ddlOrigin.SelectedValue) && destination[iIndex].ToString().Contains(ddlDestination.SelectedValue))
{
lblMessage.Text = "Record exist";
return;
}
else
{
lblMessage.Text = "No record exist";
}
}
else
{
lblMessage.Text = "No record exist";
}
}
如果这有帮助,请标记为有用..