我在aspx文件中有以下代码。我想在查询返回0行或null时显示一条消息。如何检查这个是正确的,因为我的示例没有捕获空值。
protected void search_Click(object sender, EventArgs e)
{
string searchItem;
searchItem = "Select * FROM test WHERE (name like '%"+searchTxt.Text.ToString()+"%')";
SqlDataSource.SelectCommand = searchItem;
if (SqlDataSource.SelectCommand == null)
hiddenMsg.Visible = true;
else
SqlDataSource.SelectCommand = searchItem;
}
答案 0 :(得分:1)
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count == 0)
{
display your message that no rows returned
}
else {
display rows returned
}
享受