位置0没有行 [英] No rows at position 0

查看:55
本文介绍了位置0没有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很想在列表框选择中填充文本框,但我不断收到错误消息
位置0没有行

这是即时通讯使用的代码

Well im tying to populate a text box on a list box selection and i keep getting the error
No rows at position 0

This is the code im using

    myConnection.GetConnection();
    DataSet sds = new DataSet();
    MySqlCommand sql = new MySqlCommand("SELECT * FROM users", myConnection.GetConnection());
    MySqlDataAdapter mydap = new MySqlDataAdapter(sql);
    mydap.Fill(sds, "users");
    this.UserList.DataSource = sds.Tables["users"];
    this.UserList.DisplayMember = "name";

}

private void UserList_SelectedIndexChanged(object sender, EventArgs e)
{
    //int cid = Int32.Parse(this.UserList.SelectedValue.ToString());
    MySqlCommand sql1 = new MySqlCommand("SELECT * FROM users WHERE name = ''" + this.UserList.SelectedValue.ToString() + "''", myConnection.GetConnection());
    MySqlDataAdapter DA = new MySqlDataAdapter(sql1);
    DataSet DS = new DataSet();
    DA.Fill(DS, "users");

    this.Usernametxtbox.Text = DS.Tables["users"].Rows[0]["name"].ToString();
}



但是无论我在Rows [0]部分中输入多少数字,我都会不断遇到相同的错误



But no matter what number i put in the Rows[0] section i keep getting the same error
Any help would be appreciated

推荐答案

好吧,这很简单,它意味着数据集不包含任何行!

简单地使用DEBUGGER可以告诉您,DS.Tables ["users"].Rows.Count为零....

检查形成的正在执行的查询.直接在SQL中尝试相同的查询,看看是否能得到任何回报.好像有问题,您没有从数据库获取任何数据,因此在您盲目使用第一行值时会出现错误(在使用前应检查是否返回了任何数据……即,行数> 0)
Well, it''s simple means that the dataset does not contain any rows!

A simple use of DEBUGGER can show you that, DS.Tables["users"].Rows.Count is ZERO....

Check the query formed that is getting executed. Try the same query in SQL directly and see if you get back anything. It looks like there is some issue and you are getting no data from DB and thus the error as you blindly use first row value (you should put a check if there is any data returned or not before using it... i.e. row count>0)


如果您要做的只是在单列中提取第一行中的信息,我将查看ExecuteScalar函数(
If all that you are trying to do is to pull out the information in the first row in a single column, I would look into the ExecuteScalar function (SqlCommand.ExecuteScalar Method[^]). Also, all you''re doing is checking if the value is in the database. So, I would do something more like:

MySqlCommand sql1 = new MySqlCommand("SELECT name FROM users WHERE name = '" + this.UserList.SelectedValue.ToString() + "'", myConnection.GetConnection());

if(sql1.ExecuteScalar()==null)
{
  this.Usernametxtbox.Text="Not Found";
}
else
{
  this.Usernametxtbox.Text = this.UserList.SelectedValue.ToString();
}


You better Check whether the execution of the sql command returned any rows or not. 
in this way you can avoid errors. check the code below

private void UserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int cid = Int32.Parse(this.UserList.SelectedValue.ToString());
            MySqlCommand sql1 = new MySqlCommand("SELECT * FROM users WHERE name = '" + this.UserList.SelectedValue.ToString() + "'", myConnection.GetConnection());
            MySqlDataAdapter DA = new MySqlDataAdapter(sql1);
            DataSet DS = new DataSet();
            DA.Fill(DS, "users");

if(DS.Tables["users"].Rows.Count>0)
{
    this.Usernametxtbox.Text =   DS.Tables["users"].Rows[0]["name"].ToString();
}


这篇关于位置0没有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆