“位置0处没有行”。问题是什么? [英] "There is no row at position 0." what is the problem?

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

问题描述

protected void btnView_Click(object sender, EventArgs e)
       {

               BA.DeptNo = int.Parse(txtno.Text);
              BA.option = "select";
               DataTable dt = BA.viewRecords();

                 txtname.Text = Convert.ToString( dt.Rows[0]["dname"]);
                 
                txtloc.Text = Convert.ToString(dt.Rows[0]["loc"]);

                if (dt != null)
                {
                    Response.Write("The Records are:");
            }
           else
           {
               Response.Write("The deptno is invalid");
           }

       }

推荐答案

我怀疑你的代码看起来不像那样 - 视觉studio会为你整理缩进 - 所以它真的取决于你真实的,正在执行的代码是什么样的:我认为你不具备

I suspect your code doesn't look like that - Visual studio would have tidied up the indentation for you - so it will really depend on what your real, executing code looks like: I don't think you have the
dt.Rows.Count>0

在实时代码中测试,因为它应该进入行访问的唯一方法是 在位置0的一行。



首先检查你的代码,如果你认为它是正确的,使用VS来使用CTRL + K CTRL +自动整理缩进D - 如果它编译好,那么它将工作 - 并向我们展示代码,

或使用调试器在之前检查DataTable如果 test。

test in the "live" code, as the only way it should get to the row access is if there is a row at position 0.

So start by checking you code, and if you think it's all correct, use VS to auto tidy the indentation using CTRL+K CTRL+D - if it compiles ok then it will work - and show us that code,
or use the debugger to examine the DataTable immediately prior to the if test.


添加以下验证

Add validations as below
protected void btnView_Click(object sender, EventArgs e)
{

    BA.DeptNo = int.Parse(txtno.Text);
    BA.option = "select";
    DataTable dt = BA.viewRecords();
    if (dt!=null && dt.Rows.Count >0)
    {
        txtname.Text = Convert.ToString(dt.Rows[0]["dname"]);

        txtloc.Text = Convert.ToString(dt.Rows[0]["loc"]);

    }
    else
    {
        Response.Write("The deptno is invalid");
    }

}


在您的行之前使用以下验证错误:

if(dt!= null && dt.Rows.Count> 0)
use below validation before your line which is throw error :
if (dt!=null && dt.Rows.Count >0)


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

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