Easy Insert with ASP.NET 2.0

Using ASP.NET 2.0 makes inserting information from web user interfaces to a database a snap. Based on the 3-tier software architecture paradigm, an explanation and example of how to accomplish this task is below. The example will start with a simple data model you can use to insert the sample data. Next, there will be a proposed interface design to support the database. An example of how the ASP.NET code behind the interface can be set up to handle the data pass and lastly, a single line of code written to execute the passing of data from the ASP layer to the database.

First, construct a simple database using some ADO compatible database software such as MS SQL or Access. For the purpose of our example we will construct a model based on the following design.

Name Text
Age Integer
Gender Integer

Next, create an interface to support the data model. This example uses Visual Basic as the programming code by which the data insert operation is executed. Similar results may be accomplished using another language such as C#. A text box control is suggested for the name field. A populated drop-down list is suggested for age, and a radio button list is suggested for the gender field. Also, a button should be added for the end-user to click as a trigger to perform the actual insert.

Don’t forget to add a SQL DataSource Control; configure it to include insert capabilities in advanced mode and include all the fields from the database. An example could look like:

<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="18-25" Value="0" />
        <asp:ListItem Text="26-32" Value="1" />
        <asp:ListItem Text="33-40" Value="2" />
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem Text="Male" Value="0"></asp:ListItem>
        <asp:ListItem Text="Female" Value="1"></asp:ListItem>

Now, in the ASP.NET interface layer, assign the appropriate parameters to the appropriate fields.

<asp:SqlDataSource ID="SqlDataSource1" runat="server">
        <asp:ControlParameter Name="Name" ControlID="TextBox1"
            PropertyName="Text" Type="Char" />
        <asp:ControlParameter Name="Age" ControlID="DropDownList1"
            PropertyName="SelectedValue" Type="Int32" />
        <asp:ControlParameter Name="Gender" ControlID="RadioButtonList1"
            PropertyName="SelectedValue" Type="Int32" />

This code observes the contents of the controls named in the ControlID tags and points them in the direction of the database’s ControlParameter Name which specifies where in the database the information should go. Of course, data types are specified in Type and PropertyName is the control property from which the information should be extracted.

Now, here comes the fun part – a single line of Visual Basic to handle the pass from ASP.NET to the database. Simply double-click the button created in the user-interface, and add this line:


If all went as intended, the project executes and the user should be able to type in their name to the text box on the interface, select their age from the drop down list, select their gender from the radio buttons, click to submit and (voila!) the information is inserted into the database build from the initial data model.

This is one example of how more advanced versions of ASP.NET reduce development time. The ease with which data-driven applications may be constructed using these tools and supporting technologies is a testament to rapid application development techniques as they are applied to the web.