【发布时间】:2011-05-19 11:51:47
【问题描述】:
我正在制作添加、编辑删除模块...为此,我在 c# 中有以下代码,
private void listOfCompany_Load(object sender, EventArgs e)
{
//MessageBox.Show(updID.ToString());
con.ConnectionString = @"PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\mayur patil\\My Documents\\Dairy_db\\tblCompany_1.mdb";
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string sql = "SELECT * From tblCompany_1 WHERE ID="+updID;
da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
da.Fill(ds);
// dataGridView1.DataSource = ds.Tables[0];
DataRow dr = ds.Tables[0].Rows.Count;
textBox1.Text = dr.ItemArray.GetValue(0).ToString();
textBox4.Text = dr.ItemArray.GetValue(2).ToString();
/* int t = ds.Tables[0].Columns.Count;
for (int i = 0; i < cnt; i++)
{
dataGridView1.Rows[i].Cells[0].Value = dr.ItemArray.GetValue(0).ToString();
dataGridView1.Rows[i].Cells[1].Value = dr.ItemArray.GetValue(1).ToString();
dataGridView1.Rows[i].Cells[2].Value = dr.ItemArray.GetValue(2).ToString();
}*/
}
当我调试应用程序,然后在添加、编辑和删除按钮时,它在“DataRow dr = ds.Tables[0].Rows[0];”处出现错误即“已处理 IndexOutOf RangeException”,错误是“位置 0 处没有行。”...我应该如何克服这个问题?
【问题讨论】:
-
在尝试创建 DataRow 之前,我会检查 ds.Tables[0].Rows.Count 是否不为 0
-
在 "DataRow dr = ds.Tables[0].Rows[0];" 行放一个断点看看你的“ds”变量里面是否有任何东西。
标签: c#