C# 位置-1处没有行
我收到此sql错误,位置-1处没有行 这就是我所做的C# 位置-1处没有行,c#,sql,C#,Sql,我收到此sql错误,位置-1处没有行 这就是我所做的 void showData(int index) { Connection con = new OrderManager.Connection(); SqlDataAdapter sda = new SqlDataAdapter("Select * from [MasterDatabase].[dbo].[Neworder] Where OrderID = '" + TxtBox_OrderID.Te
void showData(int index)
{
Connection con = new OrderManager.Connection();
SqlDataAdapter sda = new SqlDataAdapter("Select * from [MasterDatabase].[dbo].[Neworder] Where OrderID = '" + TxtBox_OrderID.Text + "'", con.ActiveCon());
dt = new DataTable();
sda.Fill(dt);
TxtBox_OrderID.Text = dt.Rows[index][0].ToString();
ClearTextBoxes();
dataGridView1.Rows.Clear();
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["OrderID"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["Date"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["Customer_Name"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["ProductID"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["Product_Name"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["Product_Color"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["Product_PCs"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["Product_Cutting"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["Product_TotalYards"].ToString();
}
label12.Text = "Row Count: " + dt.Rows.Count.ToString();
}
我只想在导航时显示那些OrderID等于数据库中订单ID的记录。我认为您的错误发生在这一行
TxtBox_OrderID.Text = dt.Rows[index][0].ToString();
这不是SQL错误,而是超出数组边界的简单索引。由于某些原因,当您尝试使用datatable的Rows集合中不包含的行时,您会收到此错误消息,而不是不太模糊的IndexOutOfRangeException。如果为索引变量传递的某个值小于零或大于数据表dt中的行数,则会出现此消息。
您没有对查询返回的行数进行任何检查,因此您的查询可能不会返回任何记录或简单数据。index的值为-1
void showData(int index)
{
Connection con = new OrderManager.Connection();
SqlDataAdapter sda = new SqlDataAdapter(".......", con.ActiveCon());
dt = new DataTable();
sda.Fill(dt);
// Protect the access to the rows collection of the table...
if(index < dt.RowsCount && index >= 0)
{
TxtBox_OrderID.Text = dt.Rows[index][0].ToString();
// the code that fills the datagrid
}
else
{
// Message for your user about a record not found
}
}
void showData(int索引)
{
Connection con=new OrderManager.Connection();
SqlDataAdapter sda=新的SqlDataAdapter(“…”,con.ActiveCon());
dt=新数据表();
sda.填充(dt);
//保护对表的rows集合的访问。。。
如果(索引=0)
{
TxtBox_OrderID.Text=dt.Rows[index][0].ToString();
//填充数据网格的代码
}
其他的
{
//关于找不到记录的用户消息
}
}
作为旁注,请尽快遵循给您的建议。您将避免并避免出现问题为什么不在表中设置网格的DataSource属性(并仅选择网格中需要的字段)?但是,由于其中一个DataRow字段名称错误,因此会发生此错误。检查它们什么是
datagridview 1
绑定到的?这是什么景色?它可能无法添加。例如,是否有数据源?与您的问题无关,但请不要将输入连接到SQL中;您的代码中存在着巨大的SQL注入风险——我强烈怀疑还有更多的风险。。。使用parameters@Patrick嗯,我不想这么说,但如果我向您展示“正确的方式”(根据我的主观意见),它不会涉及SqlDataAdapter
或DataTable
。对不起,但不会。它将是:intorderid=。。。;var rows=conn.Query(“从[MasterDatabase].[dbo].[Neworder]中选择*,其中OrderID=@OrderID”,new{OrderID}”).AsList()
使用“dapper”,其中SomeType
是一个与您的数据形状匹配的class
。此查询SqlDataAdapter sda=newsqldataadapter(“Select*from Neworder”,con.ActiveCon())代码>向我显示此查询和此查询SqlDataAdapter sda=newsqldataadapter(“从[MasterDatabase].[dbo].[Neworder]中选择*,其中OrderID='“+TxtBox\u OrderID.Text+”,con.ActiveCon())
在导航时向我显示此错误给了我以下提示:private void btn_NextRecord_Click(对象发送者,事件参数e){pos++;if(pos
在调试模式下按“下一条记录”时。pos为1,因此出现的条件1