我正在尝试在详细信息视图中显示数据库记录。我需要使用下一个按钮
从一个记录移动到另一个记录我有一个显示记录ID的文本框。这段代码正常工作,但它在最后一条记录之前停止,而不显示最后一条记录。
这是我的代码
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
cmd.Connection = con;
con.Open();
cmd.CommandText = "select * from quoation111 where id=1";
dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = "1";
}
dr.Close();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
cmd.Connection = con;
con.Open();
string s = "select count(*) from quoation111";
cmd.CommandText = s;
cmd.ExecuteNonQuery();
con.Close();
if (s != TextBox1.Text)
{
int s1 = Convert.ToInt32(TextBox1.Text) + 1;
TextBox1.Text = s1.ToString();
}
else
{
Response.Redirect("Default3.aspx");
}
}
答案 0 :(得分:0)
尝试以下方法读取值,相对做一些更改!!!
cmd.Connection = con;
con.Open();
string s = "select count(*) from quoation111";
cmd.CommandText = s;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds); //Filling Data into Data Adapter
int rowcount = ds.Tables[0].Rows.Count ;
for (int i = TextBox1.Text;i<= rowcount;i++)
{
TextBox1.Text = i.ToString();
}