473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

trying to save TextBox.Text value to database

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
2 5677
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: HolaGoogle | last post by:
hi all, can you please tell me what i should do to avoid session timeout when displaying my database info in my asp form (DisplayUserDatabase.asp)??? ** actualy it does load and display the...
2
by: Alpha | last post by:
Hi, I have a window based program. One of the form has several textboxes and a datagrid. The textboxes are bind to the same dataset table as the datagrid and the text changes to reflect different...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
4
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml...
0
by: vinod babu via .NET 247 | last post by:
(Type your message here) -------------------------------- From: vinod babu Here my adding a new row code Private Sub cmdadd1_Click(ByVal sender As System.Object, ByVal e As...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
1
by: Irene | last post by:
Hello all! I'm creating a web site in ASP.NET (VB.NET). One of the requirements was to allow users to create orders going through several steps. A must have is to have an option to save the work...
2
by: toddw607 | last post by:
Hi all! I am attempting to bring an SQL Server table into ASP.NET using the datagrid . I have set all cells to be a text box by which the user can just click on and edit the cell. I have a...
2
by: BOS | last post by:
Hi there, I just create a form that contains name, Address, City, State, Zip, Question Checked box, and dropdown list selection for the user to fill-out the answer in the texbox, checkbox or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.