我有一个简单的.aspx页面,带有导航按钮但是虽然First&最后一个按钮工作正常,下一个&以前只在记录1和1之间移动2由于某种原因,我无法解决。 我仍然是C#和ASP.NET的新手,但这看起来很基本,令人尴尬! 我正在使用SQL Server 2012和Visual Studio 2017。 这很奇怪,因为记录计数确认所有记录都已被读取。
C# Code:
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
//
SqlDataReader dr;
DataSet ds;
int recordcount = 0;
int i;
SqlConnection con = new SqlConnection(@"Data Source=ASPIREV17;Initial Catalog=dbSVCCAssets;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=ASPIREV17;Initial Catalog=dbSVCCAssets;Integrated Security=True");
cmd = new SqlCommand("SELECT * FROM SignboardInspection", con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
con.Open();
da.Fill(ds, "SignboardInspection");
Response.Write("Connection is Open Now!!");
dr = cmd.ExecuteReader();
recordcount = ds.Tables[0].Rows.Count;
Response.Write("Record Count : " + recordcount);
if (recordcount > i)
{
TextBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();
TextBox2.Text = ds.Tables[0].Rows[i]["DateInspected"].ToString();
TextBox3.Text = ds.Tables[0].Rows[i]["NextInspection"].ToString();
TextBox4.Text = ds.Tables[0].Rows[i]["Comments"].ToString();
}
else
{
lblMessage.Text = ("No Records Returned");
}
dr.Read();
con.Close();
Response.Write("Connection is Closed Now!!");
}
protected void btnFirst_Click(object sender, EventArgs e)
{
if (ds.Tables[0].Rows.Count > 0)
{
i = 0;
TextBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();
TextBox2.Text = ds.Tables[0].Rows[i]["DateInspected"].ToString();
TextBox3.Text = ds.Tables[0].Rows[i]["NextInspection"].ToString();
TextBox4.Text = ds.Tables[0].Rows[i]["Comments"].ToString();
}
else
{
lblMessage.Text = ("First Record");
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
if (i < ds.Tables[0].Rows.Count - 1)
{
i++;
TextBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();
TextBox2.Text = ds.Tables[0].Rows[i]["DateInspected"].ToString();
TextBox3.Text = ds.Tables[0].Rows[i]["NextInspection"].ToString();
TextBox4.Text = ds.Tables[0].Rows[i]["Comments"].ToString();
}
else
{
lblMessage.Text = ("This is the Last Record");
}
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
if (i == ds.Tables[0].Rows.Count - 1)
{
i--;
TextBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();
TextBox2.Text = ds.Tables[0].Rows[i]["DateInspected"].ToString();
TextBox3.Text = ds.Tables[0].Rows[i]["NextInspection"].ToString();
TextBox4.Text = ds.Tables[0].Rows[i]["Comments"].ToString();
}
else
{
lblMessage.Text = ("This is the First Record");
}
}
请建议并保持温和!