零位置没有行. [英] There is no row at position zero..

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

问题描述

嘿,我在以下代码中有一个异常,我不明白为什么存在该异常...


Hey i have an exception in the following code , i can''t understand why the exception exists...


con.Open();

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand("select * from mytable where ID=" + Convert.ToInt32(textBox3.Text.ToString()), con);

            DataTable dt = new DataTable();
            da.Fill(dt);


// Till now all is working well ...
            dataGridView1.DataSource = dt.DefaultView;


// Here i got the exception :(


dt.Rows[0].BeginEdit();

            dt.Rows[0][0] = Convert.ToInt32(textBox3.Text.ToString());
            dt.Rows[0][1] = textBox4.Text.ToString();

            dt.Rows[0].EndEdit();

            SqlCommandBuilder cb = new SqlCommandBuilder(da);


            da.Update(dt);

            con.Close();



添加了代码块[/编辑]



Code block added[/Edit]

推荐答案

看一下数据库-您没有ID匹配Textbox3中值的行.
Look at your database - you have no rows with an ID matching the value in Textbox3.


在位置0没有行.它位于dt.Rows [0] .BeginEdit();
这仅表示表中没有记录.它是空的.

有关网格中的编辑功能,请查看以下文章:
在GridView中就地编辑 [简单的ADO.NET数据库读取,插入,更新和删除使用C# [ ^ ]
There is no row at position 0. and it comes at dt.Rows[0].BeginEdit();
This simply means that there are no records in the table. It''s empty.

For edit feature in grid, have a look at these articles:
Inplace Edit in GridView[^]

Simple ADO.NET Database Read, Insert, Update and Delete using C#[^]


将发生此错误,因为在使用数据表之前,您没有检查以确保您的数据表中有行.在尝试使用之前,应始终进行检查以确保事物存在.
That error will occur because you aren''t checking to make sure you have rows in your data table before using it. You should always check to make sure things exist before trying to use them.
if( dt.Rows.Count > 0 )
{
   // ... Now do your stuff with it.
}


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

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