private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
// In database there are three columns: proid, proname, and unitprice.
// I need this to retrieve in respective textbox when i double click
// the value in datagrid view
SqlDataAdapter da = new SqlDataAdapter(" select * from tblproduct where proid = " +
Convert.ToInt16(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()) + "", con);
// Something wrong near this select statement so getting error index was out of range.
da.Fill(dt);
textBox1.Text = dt.Rows[0][0].ToString();
textBox2.Text = dt.Rows[0][1].ToString();
textBox3.Text = dt.Rows[0][2].ToString();
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
-2
A
回答
5
这只能在四个不同的线路发生:
// either SelectedRows or Cells is zero length
dataGridView1.SelectedRows[0].Cells[0]
// either Rows is zero length or there are no columns returned
dt.Rows[0][0]
// either Rows is zero length or there is only 1 column returned
dt.Rows[0][1]
// either Rows is zero length or there are only 2 columns returned
dt.Rows[0][2]
最有可能的线路?
// there are no SelectedRows
dataGridView1.SelectedRows[0].Cells[0]
// there are no Rows returned
dt.Rows[0][0]
0
如果SqlDataAdapter的就像是我用过的其他适配器,您需要先建立连接,它从你给的代码示例我们不会出现你在干什么。您可能需要尝试如下所示:
SqlConnection connex = new SqlConnection();
connex.ConnnectionString = "connection string to data";
try
{
//connect to the database
connex.Open();
//set the select command to be used for pulling in info
da.SelectCommand.Connection = connex;
//fill the dbData object with data from the database
da.Fill(dt);
//close database connection
connex.Close();
}
catch (Exception ex) { }
+0
我相信“con”在连接中的SqlDataAdapter –
+0
我已经声明连接字符串全局先生 – pooja
相关问题
- 1. 指数走出
- 2. 指数超出范围。必须是非负的错误
- 3. 指数走出阵列vb.net的界限
- 4. 的Python:获得指数走出字典
- 5. 指数超出范围。必须是非负数且小于集合的大小
- 6. 指数超出范围,必须是非负数,并小于集合的大小
- 7. 指数超出范围。必须是非负数且小于集合的大小。
- 8. 指数超出范围。必须是非负数且小于集合的大小。参数名:指数-6
- 9. 防止指数走出与#each_index
- 10. 计算地址:指针+非负数
- 11. 指数超出范围。必须是非负的,并小于集合的大小
- 12. 非负整数
- 13. 正弦波360度是走出非零
- 14. 指数超出范围。必须是非负值或小于收集的大小
- 15. 错误;指数超出范围。必须是非负的并且小于
- 16. 计算负数到非整数指数的pow()
- 17. 指数和负数
- 18. 负指数值?
- 19. 负指数
- 20. 指数超出范围。必须是非负数且小于集合的大小。参数名称索引
- 21. 指数超出范围。必须是非负数且小于集合的大小。参数名称:索引
- 22. 指数超出范围。必须是非负数且小于集合的大小。参数名称:索引SQLite
- 23. 指数超出范围。必须是非负数且小于集合的大小。参数名称索引:
- 24. 指数超出范围。必须是非负数且小于集合的大小。参数名:在C#asp.Net
- 25. 指数走出与SQLite数据库绑定异常
- 26. 指数超出范围。必须为非负且小于集合参数
- 27. ExtremeML负指数导出问题
- 28. python scipy.stats.powerlaw负指数
- 29. PHP - array_fill负指数
- 30. 负片段指数?
在哪一行..? –
你至少应该给关于您的问题一个句子,不只是将代码粘贴 – Jonesopolis
尝试'Convert.ToInt32(...' – mcalex