Store the value of a textbox

January 2017

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.


Hello... I'm a new user of I'm creating an 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.


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 :     

<form id="form1" runat="server">     
<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>     
</body> 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     
End If     
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     


Solved by jennifer


Published by aakai1056. Latest update on March 10, 2012 at 08:55 AM by Virginia Parsons.
This document, titled "Store the value of a textbox," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (