Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}


But no matter what number i put in the Rows[0] section i keep getting the same error
Any help would be appreciated
Posted
Comments
Pawan Kiran 23-Oct-10 5:29am    
Try By Changing UserList.SelectedItem near "Select Condition"
Or
add this line in your code
this.UserList.ValueMember="name";

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)
 
Share this answer
 
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:

C#
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();
}
 
Share this answer
 
Comments
Bikash Shrestha From Nepal 23-Oct-10 1:10am    
you are right.
C#
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();
}
 
Share this answer
 
Comments
Bikash Shrestha From Nepal 23-Oct-10 11:15am    
If this helped you then please Vote
Try like this:
private void UserList_SelectedIndexChanged(object sender, EventArgs e)
{
       if(!string.IsnullOrEmpty(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();
            }
            else
            {
               //blah blah
            }
        }
        else
        {
         // blah blah blah
        } 
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900