4

I am new to asp.net and stuck at a very stupid problem. But I cannot figure it out. I have a form which is filled by data from DB on page_load, and user update the form's input's text and click the "update" button. It updates but it updates 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);
 }

here is the stored procedure

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
6
  • Please add the content of your stored procedure. Commented Mar 13, 2013 at 10:05
  • what do you see in txt_firstname.Text when you click on button Guncelle again in debug mode? Commented Mar 13, 2013 at 10:06
  • it displays the new data. Commented Mar 13, 2013 at 10:07
  • show us the stored procedure. I think issue is there Commented Mar 13, 2013 at 10:08
  • which version of .net you use? Commented Mar 13, 2013 at 10:09

2 Answers 2

1

You mention you filling page controls with data on page load. Wrap your code on Page_Load like this:

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

Because when you hitting UpdateBtn Page_Load event is being fired and your changes being re-writed with 'old' data from DB.

Sign up to request clarification or add additional context in comments.

Comments

1

You should check Page.IsPostBack Property on page load.

See following link

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

1 Comment

I am using if(!isPostBack)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.