 |
-
Powered by

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Inserting Records using a DataGrid and Code-Behind forms
Patrick Barnes has contributed an altered version of Inserting
Records from the Quickstarts. Patrick has jumped into developing
into .NET with both feet! He loves it and took this example from the
Quickstarts and used "CODE-BEHIND FORMS" to learn this
method. This method is a "Level 2" of being able to learn
this! He felt sharing this code sample would help others that are just
starting out!
Here is the code
WebPage code
<%@ Page Inherits="MyCoolDLL" src="datagrid4_nodatatier.vb" Debug="True" Trace="true" Language="VB" %>
<html>
<body style="font: 10pt verdana">
<form runat="server">
<h3><font face="Verdana">Inserting a Row of Data</font></h3>
<table width="95%">
<tr>
<td valign="top">
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
MaintainState="false"
/>
</td>
<td valign="top">
<table style="font: 8pt verdana">
<tr>
<td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">Add a New Author:</td>
</tr>
<tr>
<td nowrap>Author ID: </td>
<td><input type="text" id="au_id" value="000-00-0000" runat="server"></td>
</tr>
<tr>
<td nowrap>Last Name: </td>
<td><input type="text" id="au_lname" value="Doe" runat="server"></td>
</tr>
<tr nowrap>
<td>First Name: </td>
<td><input type="text" id="au_fname" value="John" runat="server"></td>
</tr>
<tr>
<td>Phone: </td>
<td><input type="text" id="phone" value="808 555-5555" runat="server"></td>
</tr>
<tr>
<td>Address: </td>
<td><input type="text" id="address" value="One Microsoft Way" runat="server"></td>
</tr>
<tr>
<td>City: </td>
<td><input type="text" id="city" value="Redmond" runat="server"></td>
</tr>
<tr>
<td>State: </td>
<td>
<select id="stateabbr" runat="server">
<option>CA</option>
<option>IN</option>
<option>KS</option>
<option>MD</option>
<option>MI</option>
<option>OR</option>
<option>TN</option>
<option>UT</option>
</select>
</td>
</tr>
<tr>
<td nowrap>Zip Code: </td>
<td><input type="text" id="zip" value="98005" runat="server"></td>
</tr>
<tr>
<td>Contract: </td>
<td>
<select id="contract" runat="server">
<option value="0">False</option>
<option value="1">True</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td style="padding-top:15">
<input type="submit" OnServerClick="AddAuthor_Click" value="Add Author" runat="server">
</td>
</tr>
<tr>
<td colspan="2" style="padding-top:15" align="center">
<span id="Message" MaintainState="false" style="font: arial 11pt;" runat="server"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
|
Code Behind form Module
Option Strict Off
Imports System
Imports System.Data
Imports System.Data.SQL
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Public Class MyCoolDLL : Inherits Page
Protected WithEvents Message As System.Web.UI.HTMLControls.HTMLContainerControl
Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
Protected WithEvents au_id As System.Web.UI.HTMLControls.HTMLInputText
Dim MyConnection As SQLConnection
Sub Page_Load(Sender As Object, E As EventArgs)
MyConnection = New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
If Not (IsPostBack)
BindGrid()
End If
End Sub
Sub AddAuthor_Click(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyCommand As SQLCommand
If Request.Params("au_id") = "" Or Request.Params("au_fname") = "" Or Request.Params("au_lname") = "" Or Request.Params("phone") = ""
Message.InnerHtml = "ERROR: Null values not allowed for Author ID, Name or Phone"
Message.Style("color") = "red"
BindGrid()
End If
Dim InsertCmd As String = "insert into Authors values (@Id, @LName, @FName, @Phone, @Address, @City, @State, @Zip, @Contract)"
MyCommand = New SQLCommand(InsertCmd, MyConnection)
MyCommand.Parameters.Add(New SQLParameter("@Id", SQLDataType.VarChar, 11))
MyCommand.Parameters("@Id").Value = au_id.value
MyCommand.Parameters.Add(New SQLParameter("@LName", SQLDataType.VarChar, 40))
MyCommand.Parameters("@LName").Value = Request.Params("au_lname")
MyCommand.Parameters.Add(New SQLParameter("@FName", SQLDataType.VarChar, 20))
MyCommand.Parameters("@FName").Value = Request.Params("au_fname")
MyCommand.Parameters.Add(New SQLParameter("@Phone", SQLDataType.Char, 12))
MyCommand.Parameters("@Phone").Value = Request.Params("phone")
MyCommand.Parameters.Add(New SQLParameter("@Address", SQLDataType.VarChar, 40))
MyCommand.Parameters("@Address").Value = Request.Params("address")
MyCommand.Parameters.Add(New SQLParameter("@City", SQLDataType.VarChar, 20))
MyCommand.Parameters("@City").Value = Request.Params("city")
MyCommand.Parameters.Add(New SQLParameter("@State", SQLDataType.Char, 2))
MyCommand.Parameters("@State").Value = Request.Params("stateabbr")
MyCommand.Parameters.Add(New SQLParameter("@Zip", SQLDataType.Char, 5))
MyCommand.Parameters("@Zip").Value = Request.Params("zip")
MyCommand.Parameters.Add(New SQLParameter("@Contract", SQLDataType.VarChar,1))
MyCommand.Parameters("@Contract").Value = Request.Params("contract")
MyCommand.ActiveConnection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Added</b><br>" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "ERROR: A record already exists with the same primary key"
Else
Message.InnerHtml = "ERROR: Could not add record, please ensure the fields are correctly filled out"
End If
Message.Style("color") = "red"
End Try
MyCommand.ActiveConnection.Close()
BindGrid()
End Sub
Sub BindGrid()
Dim MyCommand As SQLDataSetCommand = new SQLDataSetCommand("select * from Authors", MyConnection)
Dim DS As DataSet = new DataSet()
MyCommand.FillDataSet(DS, "Authors")
MyDataGrid.DataSource=DS.Tables("Authors").DefaultView
MyDataGrid.DataBind()
End Sub
End Class |
|
|
tio |