Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a problem with my page. I have 2 procedure. First one is to select data from sql server and the second is to update the data if the row is change.

When I am running the procedure on C# they work, but when I run the both procedure it does not update the data. It does not recognize that I changed the data. My code:
C#
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data.SqlTypes; 
public partial class controll_update : System.Web.UI.Page 
{ 
SqlConnection con; 
SqlCommand cmd = new SqlCommand(); 
SqlDataReader rd = null; 
protected void Page_Load(object sender, EventArgs e) 
{ 
TextBoxid1.Text = Page.Request.QueryString["id"]; 
con = new SqlConnection("Server=localhost;Initial Catalog=shahar;Integrated Security=True;"); 
cmd = new SqlCommand("select_for_update", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.Add("@id", SqlDbType.NChar, 20).Value = Page.Request.QueryString["id"]; 
con.Open(); 
rd = cmd.ExecuteReader(); 
if (rd.Read()) 
TextBoxfname1.Text = rd[1].ToString(); 
TextBoxid2.Text = rd[3].ToString(); 
TextBoxlname.Text = rd[2].ToString(); 
con.Close(); 

} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
TextBoxid1.Text = Page.Request.QueryString["id"]; 
con = new SqlConnection("Server=localhost;Initial Catalog=shahar;Integrated Security=True;"); 
cmd = new SqlCommand("update_pro", con); 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.Add("@id", SqlDbType.NChar).Value = Page.Request.QueryString["id"]; 
cmd.Parameters.Add("@fname", SqlDbType.NChar).Value = TextBoxfname1.Text; 
cmd.Parameters.Add("@lname", SqlDbType.NChar).Value = TextBoxlname.Text; 
cmd.Parameters.Add("@id_num", SqlDbType.NChar).Value = TextBoxid2.Text; 
con.Open(); 
cmd.ExecuteNonQuery(); 
con.Close(); 
} 

protected void TextBoxfname1_TextChanged(object sender, EventArgs e) 
{ 

} 
protected void TextBoxlname_TextChanged(object sender, EventArgs e) 
{ 

} 
} 

I dont have any error, it just goes back to original data.

Thank you
Posted
Updated 24-Sep-10 8:50am
v3
Comments
Henry Minute 24-Sep-10 14:50pm    
Try to use more accurate tags when asking questions. It will improve your chances of getting an answer.

It is behaving exactly as you are telling it to. On the initial visit to the page the PgaeLoad event populates the textboxes from the database. After entering new text and clicking the button a postback is generated which causes the PageLoad event to fire once again, returning the textboxes to the original values from the database.

use IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
    // existing code
  }
}
 
Share this answer
 
wow, it was so simple... thank you very much, it helped!!!
 
Share this answer
 
Comments
AspDotNetDev 24-Sep-10 15:49pm    
FYI, you can add comments directly to answers rather than creating fake answers. Also, I'm sure Mark would appreciate it if you voted on his answer and accepted his solution (there is a button that allows you to do this).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900