Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text box not posting current value 1

Status
Not open for further replies.

flnMichael

Programmer
Nov 13, 2003
96
US
Hey,
Can someone tell me what I'm doing wrong here. I have a textbox being filled via database, but when I change the value of the textbox and hit update, the update query I have set up still sends the old value of the textbox, not the changed value.

Code:
string UpdateCmd = "UPDATE MSHeaders SET value = '"+UUT_Revtxt.Text+"' where MSID = "+myMSID+" and HeaderID = "+myHeaderID[j];

SqlConnection myConnSelect = new SqlConnection(strConnect);
myConnSelect.Open();
myCommand = new SqlCommand(UpdateCmd, myConnSelect);

It actually seems the syntax of the code is right, but it's just not passing the right value into the update query. Any help would be appreciated.

Thanks
Mike
 
Do you check for a postback on the page load?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
yeah I do, but with the onclick function when I hit the update button would it even go to the pageload function? Once the update button is hit, it should run the update query and redirect to another page. When I navigate back to the update page, the same old value is there, not what I put to change it...
 
yeah I do, but with the onclick function when I hit the update button would it even go to the pageload function?
Try it and see what happens. You'll notice that the Page Load event fires before your button code does hence the reason that your textbox appears to have the "old" information.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
k, now I tried it with no initial value in the text box and when I enter something it actually sticks with it this time and updates fine. Now I'm really confused, is there something staying with the text box in the pageload that I should be aware of?
 
you were right, it does do the pageload before running the update code. I was checking the postback in the wrong place because of that. That's some handy information you've given me, and I greatly appreciate it!

Thanks
Mike
 
OK here's a simple example of what I'm trying to explain.

Create a new page with a textbox and a button and add the following code:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "Hello"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Write(TextBox1.Text)
    End Sub
What happens when you hit button1? Answer is "hello" is written. Now this time change the text and see what is written. The answer is again "hello".

The reason for it is because when you hit button1, the page is reloaded and therefore the Page Load event is ran before the click event of the button (and therefore the Textbox1.Text is reset to "hello").

Now if you check for a postback e.g.
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            TextBox1.Text = "Hello"
        End If
    End Sub

You'll see whatever you typed in the textbox.

That is what I think is happening in your case - it sounds like you set the value of the textbox in the page load and therefore it gets reset each time you click a button.

Hope that made sense.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I guess you replied whilst I was typing out my reply - at least you have it fixed now anyway.

Glad to help.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top