关于asp.net页面更新数据库的问题

来源:百度知道 编辑:UC知道 时间:2024/06/02 01:46:31
我想问个很弱智的问题,因为是新手,我有个textbox,label,button然后我想单击button插入数据库,下次登陆是自动在textbox中读出插入的那个值,然后再次更新,点击button时更新数据库,但我总是更新以前的值,因为开始调用了textbox.text,代码如下,请高手们帮忙;

string sConnectionString;
sConnectionString = "server=PQH;uid=cw1e069;pwd=123456;database=cw1e069_db";
SqlConnection conn = new SqlConnection(sConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "select mark from NewQuestion where email='1212' and question='question1'";
SqlDataReader sdr = cmd.ExecuteReader();

while (sdr.Read())
{
// TextBox1.Text = sdr[0].ToString();
Label1.Text = sdr[0].ToString();
//la.Text = sdr[0].ToString();
}
sdr.Close();
if (Label1.Text=="")
{ }
else

从代码逻辑看,好象没有错误呀.
具体等有时间试了给你答复.

试了一下你的代码.整个代码逻辑上没有错误.
所以只能推理出你的错误了.
首先你要看一下,上面那块代码,就是读取数据的那块代码.
是不是放在Page_Load中的,如果是在Page_Load, 那么又是不是在
if (!IsPostBack) 中, 因为WEB页面的按钮,提交一次页面会先进入Page_Load,然后再进入Button1_Click的方法的,所以如果读取数据的代码不在
if (!IsPostBack) 当中,那么就会造成在你更改数据之前先覆盖了你的数据,所以无法更改。可以跑断点发现。

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sConnectionString;
sConnectionString = "server=PQH;uid=cw1e069;pwd=123456;database=cw1e069_db";
SqlConnection conn = new SqlConnection(sConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "select mark from NewQuestion where email='1212' and question='question1'";
SqlDataReader sdr = cmd.ExecuteReader();

while (sdr.Read())
{
// TextBox1.Text = sdr[0].ToString();
L