错误:位置0没有行 [英] Bug : There is no row at position 0

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

问题描述

当我使用文本框搜索名称时请帮助

当我写错了名字

编译器显示错误(位置0没有行) )..



i想要一个按钮盒请检查名称...



c#代码是:



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 :

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();
           }

推荐答案

我之前已经回答:0号位没有行可翻译为数字行是0。有些东西告诉我这不是一个bug,而是一个运行时异常。



你实际的bug正在解决 dt.Rows [ 0] 。你应该永远不要这样使用它,原因我上面解释过。至少检查行数是否大于0(使用if语句)并且如果不是,则不要尝试获取不存在的行。



-SA
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


你的问题是你的答案。



如果系统告诉你在索引0处没有行,那么这意味着您没有从数据库返回任何数据。

您可以通过在分配之前执行快速检查来解决此问题。

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.
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();
}


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

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