Ask a question »

Store the value of a textbox

September 2015

ASP.NET is an application framework which can be used to combine other applications such as VB, SQL databases to create dynamic and interactive web pages. One such example is to create a textbox using Visual Basic framework to gather the user's input. The value entered by the user in the textbox can be saved to databases like SQL, which can be used dynamically for other parts of the program. This not only provides the programmer the flexibility to pass a value dynamically but also engages the user in a more visual manner by showing nice graphic options such as a textbox, listbox or combobox.

Store the value of a textbox




Issue


Hello... I'm a new user of asp.net... I'm creating an asp.net webpage using VB code. I wanted to know how to store the textbox value (input from user) into the SQL database table...
I need a sample code for this. I'm using ASP.NET 3.5, Visual Studio 2008, and SQL server 2005.

Solution


If the html page has the textboxes where the user will input his/her data, then the user has to click on submit after filling the form. Here is a sample code for you.


html code :     

<body>     
<form id="form1" runat="server">     
<div>     
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>     
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>     
<br />     
<asp:Label ID="Label2" runat="server" Text="Place"></asp:Label>     
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>     
<br />     
<br />     
<asp:Button ID="Button1" runat="server" Text="Submit" />     
<br />     
<asp:Label ID="Label3" runat="server"></asp:Label>     
</div>     
</form>     
</body>     

asp.net code :     

Imports System.Data     
Imports System.Data.SqlClient     

Partial Class _Default     
Inherits System.Web.UI.Page     

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click     
'assigning string with values of textbox.     
Dim strName, strPlace As String     
strName = TextBox1.Text     
strPlace = TextBox2.Text     
'declaring sql connection. please complete it by urself     
Dim sqlcon As New SqlConnection("server=;database=;uid=;password=")     

'checking if connection is open. if open close it and open again.     
If sqlcon.State = ConnectionState.Open Then     
sqlcon.Close()     
End If     
sqlcon.Open()     
Dim strcommand As String     
'the insert command is stored in strcommand     
'Table Name : Contact     
'Column of table : Name, Place     
strcommand = "Insert into Contact(Name,Place) values ('" + strName + "','" + strPlace + "')"     
Dim sqlcomm As New SqlCommand(strcommand, sqlcon)     
Dim o As String = sqlcomm.ExecuteNonQuery()     
label3.text = "Entry Added!"     

End Sub     
End Class     


Note


Solved by jennifer
For unlimited offline reading, you can download this article for free in PDF format:
Store-the-value-of-a-textbox.pdf

See also

In the same category

Published by aakai1056. - Latest update by Virginia Parsons
This document entitled « Store the value of a textbox » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.