“位置0没有行如何解决”

时间:2019-05-18 09:46:08

标签: c# html mysql

在两天之间查找数据期间,出现错误“位置0没有行”

MySqlConnection connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["Demo"].ConnectionString.ToString());

string str = "select * from sample where name='" + Session["name"] + "' and date between '" + txtfirstdate.Text + "' and '" + txtenddate.Text + "'";

MySqlCommand cmd = new MySqlCommand(str, connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);

DataSet set = new DataSet();

connection.Open();
adapter.Fill(set);
connection.Close();

//var table = set.Tables[0];
if (set != null)
{
    lblname.Text = set.Tables[0].Rows[0]["name"].ToString();
    lbldate.Text = set.Tables[0].Rows[0]["date"].ToString();
}

1 个答案:

答案 0 :(得分:0)

阅读错误消息。您的结果集表没有行。 如果条件,请替换。

 if (set.Tables[0].Rows.Count > 0) {

        lblname.Text = set.Tables[0].Rows[0]["name"].ToString();
        lbldate.Text = set.Tables[0].Rows[0]["date"].ToString();
    }