form_to_db.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SQLClient" %>
<script Language="VB" Option="Explicit" runat="server">
Sub Page_Load(Src as object, E as EventArgs)
' Hide Insert Confirmation Message
pnlConfirm.Visible = False
End Sub
Sub btnSave_OnClick(Src as object, E as EventArgs)
Dim dtDateTimeInput as DateTime
Dim strSQL as String
Dim objConnection as SqlConnection
Dim objCommand as SqlCommand
If Page.IsValid Then
' Save the Data to the DB
' Check Values - The validators do most of this, but I was
' too lazy to come up with (ie. write) a custom validator for
' the Date/Time field.
Try
validtxtDateTimeField.Text = ""
dtDateTimeInput = CDate(txtDateTimeField.Text)
Catch Exp As Exception
'Response.Write(Exp)
validtxtDateTimeField.Text = "Please enter a valid Date/Time value."
Exit Sub
End Try
' Build our SQL String
strSQL = ""
strSQL = strSQL & "INSERT INTO scratch "
strSQL = strSQL & "(text_field, integer_field, date_time_field) " & vbCrLf
strSQL = strSQL & "VALUES ("
strSQL = strSQL & "'" & txtTextField.Text & "'"
strSQL = strSQL & ", "
strSQL = strSQL & txtIntegerField.Text
strSQL = strSQL & ", "
strSQL = strSQL & "'" & dtDateTimeInput & "'"
strSQL = strSQL & ");"
'Response.Write(strSQL)
' Set up our connection.
objConnection = New SqlConnection("Data Source=10.2.2.133;" _
& "Initial Catalog=samples;User Id=samples;Password=password;" _
& "Connect Timeout=15;Network Library=dbmssocn;")
objCommand = New SqlCommand(strSQL, objConnection)
objCommand.Connection.Open()
objCommand.ExecuteNonQuery()
objCommand.Connection.Close()
' Display Confirmation Message:
lblSQL.Text = strSQL
pnlConfirm.Visible = True
End If
End Sub
</script>
<html>
<head>
<title>ASP.NET Form to Database Sample from ASP 101</title>
</head>
<body>
<form runat="server">
<table border="0">
<tr>
<td align="right"><strong>Text Field:</strong></td>
<td align="left">
<asp:TextBox id="txtTextField" maxlength="10" runat="server" />
<asp:RequiredFieldValidator id="validtxtTextField"
ControlToValidate="txtTextField"
Display="Dynamic"
Text="Please enter some text."
ForeColor="#FF0000"
runat="server"
/>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right"><strong>Integer Field:</strong></td>
<td align="left">
<asp:TextBox id="txtIntegerField" runat="server" />
<asp:RequiredFieldValidator id="validtxtIntegerFieldReq"
ControlToValidate="txtIntegerField"
Display="Dynamic"
Text="Please enter a number between -32768 and 32767."
ForeColor="#FF0000"
runat="server"
/>
<asp:RangeValidator id="validtxtIntegerFieldRange"
ControlToValidate="txtIntegerField"
Display="Dynamic"
MinimumValue="-32768"
MaximumValue="32767"
Type="Integer"
Text="Please enter a number between -32768 and 32767."
ForeColor="#FF0000"
runat="server"
/>
</td>
</tr>
<tr>
<td align="right"><strong>Date/Time Field:</strong></td>
<td align="left">
<asp:TextBox id="txtDateTimeField" runat="server" />
<asp:Label id="validtxtDateTimeField"
ForeColor="#FF0000"
runat="server"
/>
</td>
</tr>
<tr>
<td> </td>
<td><asp:Button id="btnSave" runat="server"
OnClick="btnSave_OnClick"
Text="Save To Database"
/>
</td>
</tr>
</table>
<asp:Panel id="pnlConfirm" runat="server">
<h2>Thanks for submitting your information to us!</h2>
<p>
<strong>The resulting SQL statement was:</strong>
<pre><asp:Label id="lblSQL" runat="server" /></pre>
</p>
</asp:Panel>
<p>
You can see your entry by using our
<a href="db_count.aspx?showtable=true">DB Count Sample</a>.
</p>
</form>
</body>
</html>