Home > Archive > ASP .NET > November 2004 > Viewstate problem with textbox values and stored procedure
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Viewstate problem with textbox values and stored procedure
|
|
| Peter D.C. 2004-11-29, 4:10 pm |
| 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
| |
| Karl Seguin 2004-11-29, 4:10 pm |
| 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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Peter D.C." <Peter D.C.@discussions.microsoft.com> wrote in message
news:5A9ABAAB-1090-4538-8413-5B4DFC38DEFF@microsoft.com...
> 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
| |
| Peter D.C. 2004-11-29, 4:10 pm |
| 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
"Karl Seguin" wrote:
> 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
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "Peter D.C." <Peter D.C.@discussions.microsoft.com> wrote in message
> news:5A9ABAAB-1090-4538-8413-5B4DFC38DEFF@microsoft.com...
> through a
>
>
>
| |
| Karl Seguin 2004-11-29, 4:10 pm |
| 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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Peter D.C." <Peter D.C.@discussions.microsoft.com> wrote in message
news:EEED6979-6F65-4F31-926B-263FD85D1A32@microsoft.com...
> 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[color=darkred]
> the SQL tabel is updated when the Update buttom is used.
>
> Peter
>
>
> "Karl Seguin" wrote:
>
the[color=darkred]
form.[color=darkred]
a[color=darkred]
| |
| Peter D.C. 2004-11-29, 8:57 pm |
| 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
"Karl Seguin" wrote:
> 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
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "Peter D.C." <Peter D.C.@discussions.microsoft.com> wrote in message
> news:EEED6979-6F65-4F31-926B-263FD85D1A32@microsoft.com...
> another
> in
> the
> form.
> a
>
>
>
| |
| Karl Seguin 2004-11-29, 8:57 pm |
| 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
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Peter D.C." <Peter D.C.@discussions.microsoft.com> wrote in message
news:4EC989C4-A2DB-471E-8917-2BBD4DA80A9F@microsoft.com...
> 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[color=darkred]
> new question must be - is the SqlDataReader the wrong way to execute a
> parameterized Stored Proc call in this situation.
>
> Peter
>
>
> "Karl Seguin" wrote:
>
with[color=darkred]
still[color=darkred]
the[color=darkred]
from[color=darkred]
stored[color=darkred]
Page.IsPostBack[color=darkred]
date-field[color=darkred]
the[color=darkred]
the[color=darkred]
the[color=darkred]
in[color=darkred]
exactly..[color=darkred]
asp.net[color=darkred]
"re-updates"[color=darkred]
because[color=darkred]
disable[color=darkred]
| |
| Peter D.C. 2004-11-30, 9:04 am |
| 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
"Karl Seguin" wrote:
> 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
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "Peter D.C." <Peter D.C.@discussions.microsoft.com> wrote in message
> news:4EC989C4-A2DB-471E-8917-2BBD4DA80A9F@microsoft.com...
> for
> Proc/Dataset
> The
> with
> still
> the
> from
> stored
> Page.IsPostBack
> date-field
> the
> the
> the
> in
> exactly..
> asp.net
> "re-updates"
> because
> disable
>
>
>
| |
| Karl Seguin 2004-11-30, 9:04 am |
| uhmm..didn't think so....
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Peter D.C." <Peter D.C.@discussions.microsoft.com> wrote in message
news:4E65C488-8DB1-44BE-90F6-C8341473EC6E@microsoft.com...
> 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[color=darkred]
> is invisible?
>
> Peter
>
> "Karl Seguin" wrote:
>
shouldn't be[color=darkred]
to[color=darkred]
can't[color=darkred]
suggest[color=darkred]
event[color=darkred]
in[color=darkred]
MyCmd.ExecuteReader()".[color=darkred]
it's[color=darkred]
is[color=darkred]
before[color=darkred]
values[color=darkred]
through[color=darkred]
without[color=darkred]
with[color=darkred]
regetting[color=darkred]
THEN[color=darkred]
code[color=darkred]
message[color=darkred]
|
|
|
|
|