I am developing a project in ASP.NET with c# and SQL Server 2005 as back end. It includes a page profile.aspx
which displays the information of a user from database. The session variable is used to keep track of the current logged in user.
我正在ASP.NET中开发一个项目,其中c#和SQL Server 2005作为后端。它包括页面profile.aspx,它显示数据库中用户的信息。会话变量用于跟踪当前登录的用户。
The profiles
table in the database contains a username
column and 10 others columns like dept, address, contact, skills, interests
etc etc.
数据库中的配置文件表包含用户名列和其他10个列,如dept,地址,联系人,技能,兴趣等。
I am displaying all these values on profile.aspx
. Another page is edit_profile.aspx
which comes up when the edit button on profile.aspx
is clicked. Here the data is displayed in textboxes, with older entries already displayed, which can be edited, and click the Update
button to confirm.
我在profile.aspx上显示所有这些值。另一个页面是edit_profile.aspx,当单击profile.aspx上的编辑按钮时会出现该页面。此处数据显示在文本框中,已显示较旧的条目(可以编辑),然后单击“更新”按钮进行确认。
The update query runs fine, there is no error, but the values are not updates in the database tables. What is the possible reason? Solution?
更新查询运行正常,没有错误,但值不是数据库表中的更新。可能的原因是什么?解?
Thank you
protected void Page_Load(object sender, EventArgs e)
{
string CNS = ConfigurationManager.ConnectionStrings["myconn"].ToString();
SqlConnection con = new SqlConnection(@CNS);
SqlDataAdapter sda = new SqlDataAdapter("select * from profiles where username='" + Session["currentusername"].ToString()+"'", con);
DataSet ds = new DataSet();
sda.Fill(ds, "profiles");
txt_name.Text = ds.Tables["profiles"].Rows[0][0].ToString();
txt_deptt.Text = ds.Tables["profiles"].Rows[0][1].ToString();
txt_qualificatns.Text = ds.Tables["profiles"].Rows[0][2].ToString();
txt_add.Text = ds.Tables["profiles"].Rows[0][3].ToString();
txt_contacts.Text = ds.Tables["profiles"].Rows[0][4].ToString();
txt_interests.Text = ds.Tables["profiles"].Rows[0][5].ToString();
txt_awards.Text = ds.Tables["profiles"].Rows[0][6].ToString();
txt_website.Text = ds.Tables["profiles"].Rows[0][7].ToString();
txt_skills.Text = ds.Tables["profiles"].Rows[0][8].ToString();
txt_mstatus.Text = ds.Tables["profiles"].Rows[0][9].ToString();
ds.Reset();
}
protected void Button1_Click(object sender, EventArgs e)
{
string CNS = ConfigurationManager.ConnectionStrings["myconn"].ToString();
SqlConnection con = new SqlConnection(@CNS);
SqlCommand sda = new SqlCommand("update profiles set department='"+txt_deptt.Text+"',qualifications='"+txt_qualificatns.Text+"', address='"+txt_add.Text+"', contacts='"+txt_contacts.Text+"', interests='"+txt_interests.Text+"', awards='"+txt_awards.Text+"', website='"+txt_website.Text+"', skills='"+txt_skills.Text+"', mstatus='"+txt_mstatus.Text+"' where username='" + Session["currentusername"].ToString() + "'", con);
DataSet ds = new DataSet();
try
{
con.Open();
int temp=sda.ExecuteNonQuery();
con.Close();
if (temp >= 1)
{
lbl_message.ForeColor = System.Drawing.Color.Green;
lbl_message.Text = "Profile Updated Successfully!";
}
else
{
lbl_message.ForeColor = System.Drawing.Color.Red;
lbl_message.Text = "Integer less than 1";
}
}
catch
{
lbl_message.ForeColor = System.Drawing.Color.Red;
lbl_message.Text = "Try Again Later, An Error Occured!";
}
//Response.Redirect("profile.aspx");
}
}
1 个解决方案
#1
0
You are overwriting the contents of your textboxes every time the page loads so the user inputted conntet is never written to the database...
每次页面加载时都会覆盖文本框的内容,因此用户输入的conntet永远不会写入数据库...
Look at the Page.IsPostBack method. Basically, wrap the commands to fill the textboxes with
查看Page.IsPostBack方法。基本上,包装命令以填充文本框
if (!Page.IsPostBack) {}
To only load the values into the text box the first time the page loads (and so not overwrite the user entered values when you click the button you need to check that the page isn't a post back.
要仅在第一次加载页面时将值加载到文本框中(因此,当您单击按钮时不要覆盖用户输入的值,您需要检查页面是否不是回发。
I think maybe a book on basic ASP.Net will help answer many questions you may have early on.
我想也许一本关于基础ASP.Net的书将有助于回答你早期可能遇到的许多问题。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
string CNS = ConfigurationManager.ConnectionStrings["myconn"].ToString();
SqlConnection con = new SqlConnection(@CNS);
SqlDataAdapter sda = new SqlDataAdapter("select * from profiles where username='" + Session["currentusername"].ToString()+"'", con);
DataSet ds = new DataSet();
sda.Fill(ds, "profiles");
txt_name.Text = ds.Tables["profiles"].Rows[0][0].ToString();
txt_deptt.Text = ds.Tables["profiles"].Rows[0][1].ToString();
txt_qualificatns.Text = ds.Tables["profiles"].Rows[0][2].ToString();
txt_add.Text = ds.Tables["profiles"].Rows[0][3].ToString();
txt_contacts.Text = ds.Tables["profiles"].Rows[0][4].ToString();
txt_interests.Text = ds.Tables["profiles"].Rows[0][5].ToString();
txt_awards.Text = ds.Tables["profiles"].Rows[0][6].ToString();
txt_website.Text = ds.Tables["profiles"].Rows[0][7].ToString();
txt_skills.Text = ds.Tables["profiles"].Rows[0][8].ToString();
txt_mstatus.Text = ds.Tables["profiles"].Rows[0][9].ToString();
ds.Reset();
}
}
#1
0
You are overwriting the contents of your textboxes every time the page loads so the user inputted conntet is never written to the database...
每次页面加载时都会覆盖文本框的内容,因此用户输入的conntet永远不会写入数据库...
Look at the Page.IsPostBack method. Basically, wrap the commands to fill the textboxes with
查看Page.IsPostBack方法。基本上,包装命令以填充文本框
if (!Page.IsPostBack) {}
To only load the values into the text box the first time the page loads (and so not overwrite the user entered values when you click the button you need to check that the page isn't a post back.
要仅在第一次加载页面时将值加载到文本框中(因此,当您单击按钮时不要覆盖用户输入的值,您需要检查页面是否不是回发。
I think maybe a book on basic ASP.Net will help answer many questions you may have early on.
我想也许一本关于基础ASP.Net的书将有助于回答你早期可能遇到的许多问题。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
string CNS = ConfigurationManager.ConnectionStrings["myconn"].ToString();
SqlConnection con = new SqlConnection(@CNS);
SqlDataAdapter sda = new SqlDataAdapter("select * from profiles where username='" + Session["currentusername"].ToString()+"'", con);
DataSet ds = new DataSet();
sda.Fill(ds, "profiles");
txt_name.Text = ds.Tables["profiles"].Rows[0][0].ToString();
txt_deptt.Text = ds.Tables["profiles"].Rows[0][1].ToString();
txt_qualificatns.Text = ds.Tables["profiles"].Rows[0][2].ToString();
txt_add.Text = ds.Tables["profiles"].Rows[0][3].ToString();
txt_contacts.Text = ds.Tables["profiles"].Rows[0][4].ToString();
txt_interests.Text = ds.Tables["profiles"].Rows[0][5].ToString();
txt_awards.Text = ds.Tables["profiles"].Rows[0][6].ToString();
txt_website.Text = ds.Tables["profiles"].Rows[0][7].ToString();
txt_skills.Text = ds.Tables["profiles"].Rows[0][8].ToString();
txt_mstatus.Text = ds.Tables["profiles"].Rows[0][9].ToString();
ds.Reset();
}
}