Home

ASP.NET Topics:
Simple Data Entry

 

Introduction

One of the most regular operations you will perform on a web application is to allow visitors to create and submit records to you. On the server side, you can use a database that stores the records. On the browser side, you can create a web page that the visitors will use. Web applications are not exclusively used for the Internet anymore. You can create an application for a company but instead of making it a traditional Windows program, you can create a web-based application but only for internal (intranet) use. In this case also, the employees will use a web page to create and submit records that would be stored on the server.

In this introduction, we will see a simple (probably the simplest) way to create a record, using a simple SQL statement.

 

Practical Learning Practical Learning: Introducing Data Entry

  1. Microsoft SQL Server (Start -> (All) Programs -> Microsoft SQL Server 2005 -> SQL Server Management Studio and click Connect to connect to the database)
  2. On the main menu, click File -> New ->Query With Current Connection
  3. To create a database and a table, type the following statement:
     
    IF DB_ID (N'people1') IS NOT NULL
    DROP DATABASE people1;
    GO
    CREATE DATABASE people1;
    GO
    Use People1;
    GO
    CREATE Table Persons(
    PersonID INT IDENTITY(1,1) PRIMARY KEY,
    FirstName varchar(20),
    LastName varchar(20) NOT NULL);
    GO
  4. To execute the statement, press F5
  5. Start Microsoft Visual Studio or Microsoft Visual C#
  6. To create a web site, on the main menu, click File -> New -> Web Site...
  7. Change the name of the web site to persons1 and set the language to Visual C#
  8. Click OK
  9. In the Solution Explorer, right-click Default.aspx and click Rename
  10. Change the name to index.aspx and press Enter
  11. To create a new web page, on the main menu, click Website -> Add New Item...
  12. In the Templates, make sure Web Form is selected. Change the name to persons.
    Make sure the language is set to Visual C# and click Add
  13. In the bottom section of the code editor, click Design
  14. From the Data section of the Toolbox, drag SqlDataSource and drop it on the form
  15. Click Configure Data Source
  16. In the Configure Data Source wizard, click New Connection
  17. Select the name of the server in the Server Name combo box
  18. In the Select Or Enter A Database Name combo box, select people1
  19. Click Test Connection
     
    Add Connection
  20. Click OK and OK
  21. In the first page of the Configure Data Source wizard, click Next
  22. In the second page of the wizard, change the name of the connection string to cstPeople and click Next
  23. Make sure the Specify Columns From A Table Or View radio button is selected.
    Make sure Persons is selected in the Name combo box box.
    In the Columns list box, click the * check box
     
    Configure Data Source
  24. Click Next and click Finish
  25. In the form, click below the data source and type List of People
  26. From the Data section of the Toolbox, drag GridView and drop it on the form below the List of People title
  27. While the data grid is still selected on the form, in the Properties window, set its DataSourceID to SqlDataSource1
  28. Click Source and change the file as follows:
     
    <%@ Page Language="C#" 
             AutoEventWireup="true" 
             CodeFile="persons.aspx.cs" 
             Inherits="persons" %>
    
    <!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 runat="server">
        <title>People</title>
    </head>
    <body>
        <form id="frmPersons" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" 
                               runat="server" 
                        ConnectionString="<%$ ConnectionStrings:cstPeople %>"
                               SelectCommand="SELECT * FROM [Persons]">
                               </asp:SqlDataSource>
            </div>
            <h1>List of People</h1>
            <asp:GridView ID="GridView1"
                          runat="server" 
                          AutoGenerateColumns="False" 
                          DataKeyNames="PersonID"
                DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="PersonID" 
                                    HeaderText="PersonID" 
                                    InsertVisible="False"
                                    ReadOnly="True" 
                                    SortExpression="PersonID" />
                    <asp:BoundField DataField="FirstName" 
                                    HeaderText="FirstName" 
                                    SortExpression="FirstName" />
                    <asp:BoundField DataField="LastName" 
                                    HeaderText="LastName" 
                                    SortExpression="LastName" />
                </Columns>
            </asp:GridView>
        </form>
        <p>
            <a href="index.aspx">Home</a>
        </p>
    </body>
    </html>
  29. Save the file

Preparing the Application

Before performing data entry, you must get a connection to the database server, which you can do by creating or using a SqlConnection object.

Probably the simplest way to perform data entry in ADO.NET is by using the INSERT INTO TABLE statement. After creating the statement, you can pass it to a command. Once the command is ready, you can execute it.

Practical Learning Practical Learning: Performing Data Entry

  1. To create a new web page, on the main menu, click Website -> Add New Item...
  2. In the Templates, make sure Web Form is selected. Change the name to newperson.
    Make sure the language is set to Visual C# and click Add
  3. In the bottom section of the code editor, click Design
  4. Design the form as follows:
     
    Control Text (ID)
    Label First Name:  
    TextBox   txtFirstName
    Label Last Name:  
    TextBox   txtLastName
    Button Create btnNewPerson
  5. In the bottom section, click Source and change the file as follows (you don't have to do any of these things, it would not affect your exercise, it will only make the form look good):
     
    <%@ Page Language="C#"
             AutoEventWireup="true" 
             CodeFile="newperson.aspx.cs" 
             Inherits="newperson" %>
    
    <!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 runat="server">
        <title>People - New Person</title>
    </head>
    <body>
        <form id="frmNewPerson" runat="server" method="get">
        <div>
            <h1>People - New Person</h1>
            <table>
                <tr>
                    <td style="width: 100px">
                        <asp:Label ID="Label1" 
                                   runat="server" 
                                   Text="First Name:" 
                                   Width="94px">
                        </asp:Label></td>
                    <td style="width: 84px">
                        <asp:TextBox ID="txtFirstName" 
                                     runat="server">
                        </asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px">
                        <asp:Label ID="Label2" 
                                   runat="server" 
                                   Text="Last Name:">
                        </asp:Label></td>
                    <td style="width: 84px">
                        <asp:TextBox ID="txtLastName" 
                                     runat="server">
                        </asp:TextBox></td>
                </tr>
                <tr>
                    <td style="width: 100px">
                    </td>
                    <td style="width: 84px">
                        <asp:Button ID="btnCreate"
                                    runat="server" 
                                    Text="Create Person" /></td>
                </tr>
            </table>
        
        </div>
        </form>
        <p>
            <a href="index.aspx">Home</a>
        </p>
    </body>
    </html>
  6. In the bottom section of the code editor, click Design
  7. On the form, double-click Create and implement its event as follows:
     
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class newperson : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btnNewPerson_Click(object sender,
                                          EventArgs e)
        {
            OleDbConnection cnnPerson = 
                new OleDbConnection(
                      "Provider=Microsoft.Jet.OLEDB.4.0;data source=" +
                            Server.MapPath("~/CGI-BIN/people.mdb"));
    
            try
            {
                cnnPerson.Open();
    
                string strStatement = "INSERT INTO Persons(FirstName, " +
                                                     "LastName) VALUES('" +
                                                      txtFirstName.Text +
                                                     "', '" +
                                                      txtLastName.Text + "');";
                OleDbCommand cmdPerson = 
    			new OleDbCommand(strStatement, cnnPerson);
    
                cmdPerson.ExecuteNonQuery();
    
                txtFirstName.Text = "";
                txtLastName.Text = "";
                txtFirstName.Focus();
            }
            finally
            {
                cnnPerson.Close();
            }
        }
    }
  8. Click the index.aspx tab to access its file
  9. In the bottom section of the code editor, click Source is necessary and change the file as follows:
     
    <%@ Page Language="C#" 
             AutoEventWireup="true"  
             CodeFile="index.aspx.cs" 
             Inherits="_Default" %>
    
    <!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 runat="server">
        <title>People</title>
    </head>
    <body>
    <h1>People</h1>
    <p><a href="persons.aspx">List of People</a></p>
    <p><a href="newperson.aspx">New Person</a></p>
    
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
  10. To execute the application, on the main menu, click Debug -> Start Without Debugging
  11. Click New Person and create a new people before returning to the home page, then click the List of People link
     
  12. Close the browser and the application
 

Home Copyright © 2007-2009 FunctionX, Inc.