trying to save TextBox.Text value to database

J

jason

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("sqlconnectionstring")

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("sqlconnectionstring")

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
 
C

Cor Ligthert

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/d.../frlrfsystemwebuipageclassispostbacktopic.asp...

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

Not using it opens your server for Hackers using Injection
http://msdn.microsoft.com/library/d...en-us/dnsqlmag04/html/InjectionProtection.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
 
J

jason

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
 

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

Top