我正在ASP.net中编写一个项目,我有六个文本框来显示数据库中下一条记录的信息。我还有一个基于某个选定字段的搜索按钮,它将搜索我的数据库中的第一条记录。我将第一条记录的代码插入到搜索按钮事件处理程序中。代码如下:
Using System.Data;
Using System.Data.OleDb;
Using System.IO;
OleDbconnection mycon = new OleDbconnection("");
protected void search_btn_Click(object sender, EventArgs e)
{
int i = 0;
DataTable dt;
String searchme = "select * from student where myclass = '" + txtclass.Text + "' and mytype = '" + txtclasstype.Text + "'";
OleDbCommand cmd = new OleDbCommand (searchme, mycon);
mycon.Open();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0)
{
i = 0;
txtsearbox.Text = dt.Rows[i]["Admin_no"].ToString();
txtsurname.Text = dt.Rows[i]["surname"].ToString();
txtfirstname.Text = dt.Rows[i]["firstname"].ToString();
txtothername.Text = dt.Rows[i]["othername"].ToString();
txtsex.Text = dt.Rows[i]["sex"].ToString();
Photo.ImageUrl = dt.Rows[i]["photo"].ToString();
// then I created a session to hold my dt.
Session["dt"] = dt;
}
mycon.Close();
}
上面的代码对我的第一个记录工作正常,但我遇到问题的地方是我的下一个按钮,如下所示:
protected void next_btn_Click(object sender, EventArgs e)
{
int rowIndex = 0;
rowIndex ++;
if(rowIndex <= dt.Rows.Count)
{
txtsearbox.Text = dt.Rows[rowIndex]["Admin_no"].ToString();
txtsurname.Text = dt.Rows[rowIndex]["surname"].ToString();
txtfirstname.Text = dt.Rows[rowIndex]["firstname"].ToString();
txtothername.Text = dt.Rows[rowIndex]["othername"].ToString();
txtsex.Text = dt.Rows[rowIndex]["sex"].ToString();
Photo.ImageUrl = dt.Rows[rowIndex]["photo"].ToString();
}
mycon.Close();
}
它显示的错误是:There is no row at position 1.