ASP.Net Tutorials

Insert Data From a Form Into an SQL Database

insert.aspx

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" %>
<%@ Import Namespace="system.data" %>
<%@ Import Namespace="system.data.sqlClient" %>

<script runat="server">

sub mySubmitButton (sender as object, e as eventArgs)
dim myConnection as sqlConnection
dim myInsertString as string
dim myInsertCommand as sqlCommand

myConnection = new SqlConnection("SERVER=xxxxxxxx; UID=xxxxxxxx; PWD=xxxxxxxx; DATABASE=xxxxxxxx")
myInsertString = "INSERT my_quotes (first_name, last_name, quote, subject) VALUES (@textBoxFirstName, @textBoxLastName, @textBoxQuote, @textBoxSubject)"

myInsertCommand = new sqlCommand(myInsertString, myConnection)
myInsertCommand.parameters.Add("@textBoxFirstName", textBoxFirstName.text)
myInsertCommand.parameters.Add("@textBoxLastName", textBoxLastName.text)
myInsertCommand.parameters.Add("@textBoxQuote", textBoxQuote.text)
myInsertCommand.parameters.Add("@textBoxSubject", textBoxSubject.text)

dim rowsAffected as Integer = 0

try

myConnection.open()

rowsAffected = myInsertCommand.ExecuteNonQuery
if rowsAffected > 0 then myStatusLabel.text = "<b>Records have been added</b>"

finally

myInsertCommand.dispose()
myConnection.close()

end try

end sub
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<body>

<p><strong>Insert Records</strong></p>
<form runat="server">
<table border="0" cellspacing="2" cellpadding="0">
<tr>
<td>First Name:</td>
<td><asp:textBox id="textBoxFirstName" runat="server" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><asp:textBox id="textBoxLastName" runat="server"/></td>
</tr>
<tr>
<td>Quote</td>
<td><asp:textBox id="textBoxQuote" rows="5" textMode="multiline" columns="40" runat="server" /></td>
</tr>
<tr>
<td>Subject</td>
<td><asp:textBox id="textBoxSubject" runat="server"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:button text="Insert" onClick="mySubmitButton" runat="server" /></td>
</tr>
</table>
<p><asp:label id="myStatusLabel" runat="server" /></p>
</form>


</body>
</html>

Insert Records
 
ASP.Net
Other Tutorials