Viewstate problem with textbox values and stored procedure

G

Guest

Hi

I want to update data hold in several textbox controls on an asp.net form.
But it seems like it is the old textbox values that is "re-updates" through a
stored procedure who updates a SQL tabel. I know the SP works, because a
smalldatetime-field in the database is changed. I've tried to disable
viewstate on all textbox controls.

Any ideas to solve this problem.

Thanks.
Peter
 
K

Karl Seguin

Textboxes automatically maintain the inputted value with or without
viewstate. My guess is that you are repopulating the textboxes with the
orignal value than updating..something like:

public Sub Page_Load
dim dr as datarow = GetInformation(x);
txtUserName.Text = dr("userName")
txtPassword.Text = dr("Password")
...
end sub

public sub SomeButton_Click()
UpdateData(txtUsername.text, txtPassword.Text)
end sub


When the page postsback Page_Load fires first and you are regetting the
information and resetting the textbox values to the old stuff, THEN the
event fires and the row is updated... The trick is to wrap the code in the
page_load in a If NOT Page.IsPostBack

of course, since you've provided no code, it's hard to tell exactly..

Karl
 
G

Guest

All the textboxes are populated based on a dataset result from a stored
procedure like this (only one tabel in the dataset):
For Each cRow In dsTabel.Tables("tblName").Rows
Me.ctlID.Text = cRow("ID").ToString
and so on....

When I want to update all the values from the textboxes back through another
stored procedure I do it like this:

MyConn.Open()
SQL = "ProcSomeName"
Dim MyCmd As New SqlCommand(SQL, MyConn)
MyCmd.CommandType = CommandType.StoredProcedure
MyCmd.Parameters.Add("@ID", SqlDbType.Int).Value = Me.ctlID.Text
and so on for all the textboxes .....

I use a SqlDataReader to execute the stored procedure like this:
Dim Reader As SqlDataReader
Reader = MyCmd.ExecuteReader()

The Sub doing this update it allready wrapped in "If Not Page.IsPostBack
Then". As mention does the execution of the SP works because a date-field in
the SQL tabel is updated when the Update buttom is used.

Peter
 
K

Karl Seguin

The problem isn't with the update code being in the if statement, it's with
the for each statement...my guess, without seeing more of your code is still
that:
For Each cRow In dsTabel.Tables("tblName").Rows
Me.ctlID.Text = cRow("ID").ToString
...
next
gets executed when the page postback...and that this would happen before the
update does, thus overwriting what the user entered with the old values from
the dataset.

Karl
 
G

Guest

I think you're right. I have now wrapped the call in the Page_Load event for
the Private Sub (FillVauesInTextBoxes), that based on a Stored Proc/Dataset
populates the textboxes, like this:

If Not Page.IsPostBack Then
Dim ID As String = Request.QueryString("ID")
FillValuesInTextBoxes(ID)
End If

But now I get a runtime error - stating that a the inputstring was not in
the right format - stopping at the line "Reader - MyCmd.ExecuteReader()". The
new question must be - is the SqlDataReader the wrong way to execute a
parameterized Stored Proc call in this situation.

Peter
 
K

Karl Seguin

Peter,
SqlDataReader is fine. Purist would probably tell you that you shouldn't be
returning an SqlDataReader from your data layer to your presentation
layer...and instead use either typed datasets (yack, not much better) or
custom business objects. I'm such a purist, but for now we'll skip over
that issue.

Taking off my purist hat, there's nothing wrong with what you are trying to
do. I imagine it's just a small error..nothing fundamental to your
architecture. Again, you've provided very little code to go on, so I can't
provide any guesses as to what the problem is. The only thing I can suggest
is that maybe ID is null or something or not what you are
expecting...dunno...

Karl
 
G

Guest

Again, you're right. If I set the visible property of the ID textbox to true
- then it works!! Is the value of the ID-textbox not available if the textbox
is invisible?

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top