Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help when i use the textbox to search for a name
and when i wrote a wrong name
the compiler show bug ( There is no row at position 0)..

i want a meassage box Please Check The Name ...

The c# code is :

C#
private void button1_Click(object sender, EventArgs e)
        {
            showcustomer sc = new showcustomer();
            sc.Show();
           DataTable dt= cc.searchbyname(txtsearch.Text);
               DataRow row = dt.Rows[0];
               sc.lblname.Text = row["Cust_Name"].ToString();
               sc.lblphone.Text = row["Cust_Phone"].ToString();
               sc.lbladdress.Text = row["Cust_Address"].ToString();
               sc.lblemail.Text = row["Cust_Email"].ToString();
               sc.lblcompany.Text = row["Cust_Company"].ToString();
               sc.lblid.Text = row["Cust_Id"].ToString();
           }
Posted
Updated 18-Mar-13 9:51am
v2
Comments
joshrduncan2012 18-Mar-13 15:49pm    
What is your question?
Sergey Alexandrovich Kryukov 18-Mar-13 15:51pm    
This is the re-post. I already removed one non-question.
—SA
Mostafa Metwally 18-Mar-13 15:52pm    
sorry i updated my question
frostcox 18-Mar-13 15:54pm    
Put if(dt.Rows.Count != 0)
{
Logic here....
}
And you will receive no error, your data table must have no rows so that's why your getting the error
Mostafa Metwally 18-Mar-13 15:57pm    
I Tried This and the Same Problem

I already answered before: "there is no row at position 0" can be translated as "the number of rows is 0". Something tells me that this is not a "bug", but a run-time exception.

You actual bug is addressing dt.Rows[0]. You should never use it like this, by the reason I explained above. At least check if the number of rows is more then 0 (using "if" statement) and don't try to get a non-existing row if it is not.

—SA
 
Share this answer
 
Comments
Mostafa Metwally 18-Mar-13 16:10pm    
Thank You Mr Sergy
Voted 5
Sergey Alexandrovich Kryukov 18-Mar-13 16:12pm    
You are welcome, but will you accept the answer formally (green button)?
—SA
Mostafa Metwally 18-Mar-13 16:18pm    
It's My pleasure Mr Sergy To Accept your Answer
And Sorry For My non question
Sergey Alexandrovich Kryukov 18-Mar-13 16:26pm    
Thank you for accepting. As to non-questions, it's no problem a problem anymore, as soon as you realize importance of formal questioning (to remind you: we really have problems being overwhelmed with non-questions; you may not see it because many were removed).
Good luck, call again.
—SA
Your question is your answer.

If the system tells you that there is no row at index 0, then it means you haven't returned any data from the database.
You can fix this by performing a quick check before your assignments.
C#
DataTable dt= cc.searchbyname(txtsearch.Text);
if( dt.Rows.Any() ) // or this will also work... ( dt.Rows.Count > 0 )
{
   DataRow row = dt.Rows[0];
   sc.lblname.Text = row["Cust_Name"].ToString();
   sc.lblphone.Text = row["Cust_Phone"].ToString();
   sc.lbladdress.Text = row["Cust_Address"].ToString();
   sc.lblemail.Text = row["Cust_Email"].ToString();
   sc.lblcompany.Text = row["Cust_Company"].ToString();
   sc.lblid.Text = row["Cust_Id"].ToString();
}
 
Share this answer
 
Comments
Mostafa Metwally 18-Mar-13 16:09pm    
thank you Mr. Marcus

Voted 5
Sergey Alexandrovich Kryukov 18-Mar-13 16:13pm    
Exactly, a quick check, a 5. (I also answered.)
—SA
fjdiewornncalwe 18-Mar-13 17:34pm    
Thanks, Sergey.

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