Asp.net update table with a click of a button

I am new to asp.net and stuck in a very silly issue. But I can’t understand. I have a form that is filled with data from the database on page_load, and the user updates the text for entering the form and clicks the Refresh button. It is updated, but updated with old data.

WHY DOES IT UPDATE IT WITH OLD DATA?

here is the aspx form

<form id="form1" runat="server"> <table> <tr> <td>ID</td> <td><asp:Label ID="lbl_id" runat="server" Text=""></asp:Label></td> </tr> <tr> <td>FirstName</td> <td><asp:TextBox ID="txt_firstname" runat="server"></asp:TextBox></td> </tr> <tr> <td>LastName</td> <td><asp:TextBox ID="txt_lastname" runat="server"></asp:TextBox></td> </tr> </table> 

here is the code behind

 protected void Guncelle_Click(object sender, EventArgs e) { DbCommand dbCommand; dbCommand = db.GetStoredProcCommand("MedBul_Update_Registration_Request"); db.AddInParameter(dbCommand, "id", DbType.Int16, request_id); db.AddInParameter(dbCommand, "FirstName", DbType.String, txt_firstname.Text.ToString().Trim()); db.AddInParameter(dbCommand, "LastName", DbType.String, txt_lastname.Text.ToString().Trim()); db.ExecuteNonQuery(dbCommand); } 

procedure is stored here

 Create PROCEDURE [dbo].[MedBul_Update_Registration_Request] (@id int,@FirstName varchar(50),@LastName varchar(50)) AS BEGIN update NewProfessionalRequest set FirstName= @FirstName, LastName =@LastName where id = @id END GO 
+4
source share
2 answers

You mentioned that you are filling page controls with page load data. Wrap your code on the_Load page as follows:

 if(!isPostBack) { // populate form with data from DB here } 

Because when you click the UpdateBtn Page_Load event, it starts and your changes are overwritten with old data from the database.

+1
source

You should check the Page.IsPostBack property on the download page.

See the following link

http://msdn.microsoft.com/en-in/library/system.web.ui.page.ispostback.aspx

+1
source

Source: https://habr.com/ru/post/1468930/


All Articles