Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Here is the code:
C#
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select Loginname,password from pegtables.tblusers where userid=1", ConfigurationManager.AppSettings["CS_localsource"]);
da.Fill(ds,"emp");

DataTable dt = new DataTable(ds.Tables[0].TableName.ToString());
DataRow dr = dt.Rows[0];

textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();


And I have the following in my App.config file:
XML
<configuration>
<appsettings>
<add key="CS_localsource" value="Data source=myinstance;Integrated Security=SSPI;Database=mydb" />
</appsettings>
</configuration>


Here I am trying to populate two textboxes username and password ,which i am accessing from database.But i ended up with "there is no row at position 0" exception.what wrong i am doing here?
Posted
Updated 17-May-11 0:46am
v2

C#
DataTable dt = new DataTable("tblusers");
SqlDataAdapter da = new SqlDataAdapter("select Loginname,password from pegtables.tblusers where userid=1", ConfigurationManager.AppSettings["CS_localsource"]);
da.Fill(dt);
if(dt.Rows.Count == 1) // Better check for only one row as this makes more sense
{
    DataRow dr = dt.Rows[0];

    textBox1.Text = dr[0].ToString();
    textBox2.Text = dr[1].ToString();
}
else
{
    if(dt.Rows.Count == 0)
    {
        //Do something here to notify that the user was not foung
    }
    else
    {
        //Inform about too many hits here
    }
}


Hope that helps you!

-MRB
 
Share this answer
 
v3
I think this will help you..... I'm giving you this code as per your code....
private void Button_click(object sender, EventArgs e)
{
  Button _sender = (Button)sender;
  _sender.Enabled = false;
  DataSet ds = new DataSet();
  SqlDataAdapter da = new SqlDataAdapter("select Loginname,password from pegtables.tblusers where  userid=1", ConfigurationManager.AppSettings["CS_localsource"]);
  
  try{
  da.Fill(ds,"emp");
  }
  catch(Exception ex)
  { throw new Exception(ex.Message);}         
  DataTable dt = ds.Tables[0];
  if(dt.Rows.Count>0)
  {
    DataRow dr = dt.Rows[0];
    textBox1.Text = Convert.ToString(dr[0]);
    textBox2.Text = Convert.ToString(dr[1]);
  }
  else
  {
     //code to give message about no user found with this username and password...
  }
  _sender.Enabled = true;
}
 
Share this answer
 
In answer to your comment to my original answer, here is how I would do a simple User access class :

C#
class User
        {
            public string UserName { get; set; }
            public string Password { get; set; }

            private static User GetUser(int userID)
            {
                using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["CS_localsource"]))
                {
                    SqlCommand cmd = new SqlCommand("select Loginname, password from pegtables.tblusers where userid=@UserID", con);
                    cmd.Parameters.AddWithValue("@UserID", userID);
                    con.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    User currentUser = new User();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            currentUser.UserName = dr.IsDBNull(0) ? "Unknown User" : dr.GetString(0);
                            currentUser.Password = dr.IsDBNull(1) ? "UnknownPassword" : dr.GetString(1);
                        }
                        return currentUser;
                    }
                    else
                    {
                        throw new Exception("User not found");
                    }
                }

            }
        }


I way prefer to use a DataReader to get my info from the data source than using DataSets and such like.

Hope this helps
 
Share this answer
 
C#
DataSet ds = new DataSet();
  SqlDataAdapter da = new SqlDataAdapter("select Loginname,password from pegtables.tblusers where userid=1", ConfigurationManager.AppSettings["CS_localsource"]);
  da.Fill(ds,"emp");
               
  DataTable dt = new DataTable(ds.Tables[0].TableName.ToString());
  
if(dt.Rows.Count>0)
{
  textBox1.Text = dr[0][0].ToString();
  textBox2.Text = dr[0][1].ToString();
}
 
Share this answer
 
v3
<pre>
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select Loginname,password from pegtables.tblusers where userid=1", ConfigurationManager.AppSettings["CS_localsource"]);
da.Fill(ds);

DataTable dt = new DataTable();
dt=ds.Tables[0];
if(dt.Rows.Count>0)
{
textBox1.Text = dr[0][0].ToString();
textBox2.Text = dr[0][1].ToString();
}
</pre>
 
Share this answer
 
v3
Comments
Prasanta_Prince 2-Jun-11 9:24am    
Modify because of format.
I think the problem lies here

C#
DataTable dt = new DataTable(ds.Tables[0].TableName.ToString());
  DataRow dr = dt.Rows[0];


although you initiate a new DataTable, it does not yet have any rows. I am not sure what you are trying to achieve overall, but I think you might be looking to do something like this

C#
DataTable dt = ds.Tables[0];
which would make dt equal to the first table in the DataSet.

Hope this helps
 
Share this answer
 
Comments
M.CHAITHANYA 17-May-11 7:04am    
Here I am trying to populate two textboxes username and password ,which i am accessing from database.But i ended up with "there is no row at position 0" exception.what wrong i am doing here? .
This is a normal error that occurs when you are trying to access a row that is not there.The Perfect solution to avoid this error is to check whether that row does exist there,i.e in your case you are trying to access the first and second rows of datatable dt,so to avoid error in code try this :



<code>
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select Loginname,password from pegtables.tblusers where userid=1", ConfigurationManager.AppSettings["CS_localsource"]);
da.Fill(ds,"emp");

DataTable dt = new DataTable(ds.Tables[0].TableName.ToString());
if(dt.Rows.Count&amp;gt;0)
{
DataRow dr = dt.Rows[0];
textBox1.Text = dr[0].ToString();
textBox2.Text = dr[1].ToString();
}
</code>
 
Share this answer
 
v3
The problem is because the datatable is blank or query return blank result. just check the Rowcount before asignment. and then put it inside try{} catch{} block.
 
Share this answer
 
This can be solve by below given steps

Dataset dSet = new Dataset();
if(dSet.Tables.Count>0) // if Yes
{
if(dSet.Tables[0].Rows.Count> 0)
{
//Your logic
}
}
 
Share this answer
 

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