
关于asp.net页面更新数据库的问题
我想问个很弱智的问题,因为是新手,我有个textbox,label,button然后我想单击button插入数据库,下次登陆是自动在textbox中读出插入的那个值,然后...
我想问个很弱智的问题,因为是新手,我有个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
{
TextBox1.Text = Label1.Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sConnectionString;
sConnectionString = "server=PQH;uid=cw1e069;pwd=123456;database=cw1e069_db";
SqlConnection conn = new SqlConnection(sConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
if (Label1.Text == "")
{
cmd.CommandText = "insert into NewQuestion(email,question,mark) values('1212','question1','" + TextBox1.Text + "')";
cmd.ExecuteNonQuery();
}
else
{
TextBox1.Text = this.TextBox1.Text;
cmd.CommandText = "update NewQuestion set mark='" + TextBox1.Text + "' where email='1212' and question='question1'";
cmd.ExecuteNonQuery();
conn.Close();
} 展开
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
{
TextBox1.Text = Label1.Text;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sConnectionString;
sConnectionString = "server=PQH;uid=cw1e069;pwd=123456;database=cw1e069_db";
SqlConnection conn = new SqlConnection(sConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
if (Label1.Text == "")
{
cmd.CommandText = "insert into NewQuestion(email,question,mark) values('1212','question1','" + TextBox1.Text + "')";
cmd.ExecuteNonQuery();
}
else
{
TextBox1.Text = this.TextBox1.Text;
cmd.CommandText = "update NewQuestion set mark='" + TextBox1.Text + "' where email='1212' and question='question1'";
cmd.ExecuteNonQuery();
conn.Close();
} 展开
3个回答
展开全部
从代码逻辑看,好象没有错误呀.
具体等有时间试了给你答复.
试了一下你的代码.整个代码逻辑上没有错误.
所以只能推理出你的错误了.
首先你要看一下,上面那块代码,就是读取数据的那块代码.
是不是放在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();
Label1.Text = sdr[0].ToString();
//la.Text = sdr[0].ToString();
}
sdr.Close();
if (Label1.Text=="")
{ }
else
{
TextBox1.Text = Label1.Text;
}
}
}
代码奉上,如果不是这个原因,那我也就不知道了。
具体等有时间试了给你答复.
试了一下你的代码.整个代码逻辑上没有错误.
所以只能推理出你的错误了.
首先你要看一下,上面那块代码,就是读取数据的那块代码.
是不是放在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();
Label1.Text = sdr[0].ToString();
//la.Text = sdr[0].ToString();
}
sdr.Close();
if (Label1.Text=="")
{ }
else
{
TextBox1.Text = Label1.Text;
}
}
}
代码奉上,如果不是这个原因,那我也就不知道了。

2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
展开全部
if (!IsPostBack)
{
//页面载入时给textbox赋值的代码写在这里,因为点按钮的时候会刷新一下页面,回滚了,所以textbox中的值还是以前的
}
{
//页面载入时给textbox赋值的代码写在这里,因为点按钮的时候会刷新一下页面,回滚了,所以textbox中的值还是以前的
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的问题我知道,事你的方法没再if (!IsPostBack)
{
具体方法
}里调用
{
具体方法
}里调用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询