By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

trying to save TextBox.Text value to database

P: n/a
hello. i am just trying to save a TextBox.Text value to a database, but
strangely, when the value is changed on the web form, the changes are
not recognized in the event where i try to save the information.

sample.aspx contents:

<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server"
Width="560px" Height="512px" Wrap="True" TextMode="MultiLine">
</asp:TextBox>

<asp:Button id="Button1" runat="server" Text="Save Changes">
</asp:Button>
</form>

sample.aspx.vb contents:

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load

Dim sQuery As String = "SELECT col1 FROM table1 WHERE id=100"

Dim sConn = ConfigurationSettings.AppSettings("sqlconnectionst ring")

Dim oConn = New SqlConnection(sConn)
oConn.Open()
Dim oCmd = oConn.CreateCommand()
oCmd.CommandText = sQuery
Dim oReader = oCmd.ExecuteReader()
oReader.Read()
TextBox1.Text = oReader("col1").ToString()
oReader.Close()
oConn.Close()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

Dim sQuery As String = "UPDATE table1 SET col1='" + _
fixer(TextBox1.Text) + "' WHERE id=100"

Dim sConn = ConfigurationSettings.AppSettings("sqlconnectionst ring")

Dim oConn = New SqlConnection(sConn)
oConn.Open()
Dim oCmd = oConn.CreateCommand()
oCmd.CommandText = sQuery
oCmd.ExecuteNonQuery()
oConn.Close()
End Sub

Function fixer(ByVal datainfo)
If datainfo <> "" Then
fixer = Replace(datainfo, "'", "''")
End If
End Function
the Page_Load event works just fine. the data is queried from the
database, and is populated into TextBox1. let's say, for example, the
data is the following string (in TextBox1):

Hello Everyone. This is a String.

the problem is as follows. let's say i modify this string to the
following example:

Hello. String.

then i click Button1. i would expect the database to be updated with
the new TextBox1.Text value, which should be "Hello. String." however
the value that gets sent during the Button1_Click event to the database
is the ORIGINAL value:

Hello Everyone. This is a String.

why does the TextBox1.Text property not change when i modify the text
inside of it? is there a different property i should be using to get at
the actual, visible text in a TextBox?

thanks for any help, i'm sure it's a silly question.

jason

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jason,

Your problem is that you are setting your textbox again after that the page
is posted back.

Use for that
If Not IsPostback then
http://msdn.microsoft.com/library/de...tbacktopic.asp...

As well do I advice you to use command.parameters.
http://msdn.microsoft.com/library/de...eterstopic.asp

Not using it opens your server for Hackers using Injection
http://msdn.microsoft.com/library/de...Protection.asp

And for the same do not use the + to concatenate strings however the &. It
can give really terrible unexpected behaviour when there is only a value in
that string. It is than in some situations possible that it starts counting.

I did not check for more things, however I hope that above helps something.

Cor
Nov 21 '05 #2

P: n/a
If Not IsPostBack did the trick! thank you for the explanation.

and thank you for the recommendations on the other topics as well. i
will look into the links you provided and try to change the code
accordingly.

jason

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.