string pno= this.dgvPrj.CurrentRow.Cells[0].Value.ToString();
DataSet myds = datacon.getds("select PrjNo,Description, Respon,"
+ "Dept, StartDate,EndDate, Stations,"
+ "EES1, EES21, EES22,EES34,EES5,"
+ "PrjMgmt, Total from ProjectOverview where PrjNo='"+pno+"'" , "ProjectOverview"); prjNoTextBox.Text = Convert.ToString(myds.Tables["ProjectOverview"].Rows[0].ItemArray[0]);//这一行出现了there is no row at position 0,
不知道是什么原因,请大家指点一下,数据集里是有表的
DataSet myds = datacon.getds("select PrjNo,Description, Respon,"
+ "Dept, StartDate,EndDate, Stations,"
+ "EES1, EES21, EES22,EES34,EES5,"
+ "PrjMgmt, Total from ProjectOverview where PrjNo='"+pno+"'" , "ProjectOverview"); prjNoTextBox.Text = Convert.ToString(myds.Tables["ProjectOverview"].Rows[0].ItemArray[0]);//这一行出现了there is no row at position 0,
不知道是什么原因,请大家指点一下,数据集里是有表的
请问表为什么会是空的呢?如何解决?换成myds.Tables["ProjectOverview"].Rows[0][0] 也是同样的错误
string pno= this.dgvPrj.CurrentRow.Cells[0].Value.ToString();
messagebox.show(pno);//测试一下是否条件为空,导致查询的数据为空
DataSet myds = datacon.getds("select PrjNo,Description, Respon,"+
"Dept, StartDate,EndDate, Stations,"+
"EES1, EES21, EES22,EES34,EES5," +
"PrjMgmt, Total from ProjectOverview where PrjNo='"+pno+"'" , "ProjectOverview"); prjNoTextBox.Text = myds.Tables["ProjectOverview"].Rows[0].ItemArray[0].toString();
你的错误不关最后一句的错误,而是在获取数据时出错的。
这一局错误了
ItemArray是一个属性,返回的是一个数组,应该赋值给一个数组,而且他也没有提供下标的访问
所以你的使用是错误的,下列方法可以帮助你
ds.Tables[0].Rows[0]["姓名"];返回第一行的列名是“姓名”的字段值
ds.Tables[0].Rows[0][0]返回第一行第一列的值
条件不为空, pno是有值的
string pno= this.dgvPrj.CurrentRow.Cells[0].Value.ToString();
messagebox.show(pno);
DataTable mydt = new DataTable();
SqlDataApdater da=new SqlDataApdater("select PrjNo,Description, Respon,"+
"Dept, StartDate,EndDate, Stations,"+
"EES1, EES21, EES22,EES34,EES5," +
"PrjMgmt, Total from ProjectOverview where PrjNo='"+pno+"'" ,conn);
da.fill(mydt);
if(dt.rows.count>0)
{
prjNoTextBox.Text = mydt.Rows[0].ItemArray[0].toString();
}
看是否有数据条数
是我自己写的,
public DataSet getds(string str, string str_table)
{
OleDbConnection con = this.getcon();
con.Open();
OleDbDataAdapter ada = new OleDbDataAdapter(str, con);
DataSet myds = new DataSet();
ada.Fill(myds,str_table);
return myds;
}
string pno= this.dgvPrj.CurrentRow.Cells[0].Value.ToString();
//messagebox.show(pno);
DataTable mydt = new DataTable();
SqlDataApdater da=new SqlDataApdater("select PrjNo,Description, Respon,"+
"Dept, StartDate,EndDate, Stations,"+
"EES1, EES21, EES22,EES34,EES5," +
"PrjMgmt, Total from ProjectOverview where PrjNo='"+pno+"'" ,conn);
da.fill(mydt);
if(dt.rows.count>0)
{
prjNoTextBox.Text = mydt.Rows[0].ItemArray[0].toString();
} //那你用这个试试,是否为空,如果mydt不为空,则是你返回的table为空