C# ASP.NET Sql Command Insert Statement Source Code Example

Sql Command Insert Statement

Purpose: – Illustrates using Sql Command Insert Statement in C-Sharp ASP.NET.

Prerequistes:

  1. Install Visual Studio 2022
  2. Watch this video to enable web forms in Visual Studio 2022 Enable Web Forms websites in Visual Studio 2022
  3. Install SQL Server Express
  4. Download Northwind Database
  5. Attach Northwind Database to Databases in Sql Express

Notes:

  • You can build your own library of syntax examples by using same web site over and over and just adding new web forms.

Instructions:

  1. Use Visual Studio 2022
  2. Create new web site;
    • Select Create New Project
    • Select ASP.NET Web Forms Site Template
    • name of project could be CSharp_ASPNET_Syntax
    • name of solution could be CSharp_ASPNET_Syntax.
  3. Add New folder named “Database_ADONET”
    • Right-click project name in solution explorer;
    • add new folder;
    • name of folder could be: Database_ADONET
  4. Add Web Form Named SqlCommandInsert to Database_ADONET folder
    • Right-click Database_ADONET folder;
    • add new item;
    • Select Web Form
    • Check place code behind in separate file
    • Web Form name could be SqlCommandInsert
  5. Click on copy code in code below to copy code into web form SqlCommandInsert.aspx
  6. Click on copy code in second set of code below to copy code into code-behind SqlCommandInsert.aspx.cs
  7. Right-click on SqlCommandInsert.aspx in solution explorer and select view in browser

Step 1: Click on Copy Code to Cut-n-paste code into SqlCommandInsert.aspx

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="SqlCommandInsert.aspx.cs" Inherits="Database_ADONET_SqlCommandInsert" %>
 
 
;
<title></title>
 
 
    <title></title>
 
 
    <form id="form1" runat="server">
 
<div>
 
<table>
 
<tbody>
<tr>
 
<td>First Name:</td>
 
 
<td>
                        <asp:textbox id="txtFirstName" runat="server"></asp:textbox>
</td>
 
</tr>
 
 
<tr>
 
<td>Last Name:</td>
 
 
<td>
                        <asp:textbox id="txtLastName" runat="server"></asp:textbox>
</td>
 
</tr>
 
</tbody>
</table>
 
 
            <asp:button id="btnInsert" runat="server" text="Insert" onclick="btnInsert_Click">
            <asp:label id="lblErrMsg" runat="server" text="lblErrMsg" forecolor="#FF3300" visible="False"></asp:label>
            <asp:gridview id="GridView1" runat="server" datasourceid="sdsEmployees">
            </asp:gridview>
            <asp:sqldatasource id="sdsEmployees" runat="server" connectionstring="<%$ ConnectionStrings:Northwind_ConnectionString %>" selectcommand="SELECT [FirstName], [LastName] FROM [Employees]"></asp:sqldatasource>
        </asp:button></div>
 
    </form>

Step 2: Click on Copy Code to Cut-n-paste code into SqlCommandInsert.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
partial class Database_ADONET_SqlCommandInsert : System.Web.UI.Page
{
    protected void btnInsert_Click(object sender, System.EventArgs e)
    {
        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind_ConnectionString"].ConnectionString);
        //Create Command object
        SqlCommand nonqueryCommand = thisConnection.CreateCommand();
        try
        {
            // Open Connection
            thisConnection.Open();
            // Create INSERT statement with named parameters
            nonqueryCommand.CommandText = "INSERT  INTO Employees (FirstName, LastName) VALUES (@FirstName, @LastName)";
            // Add Parameters to Command Parameters collection
            nonqueryCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 10);
            nonqueryCommand.Parameters.Add("@LastName", SqlDbType.VarChar, 20);
 
            nonqueryCommand.Parameters["@FirstName"].Value = txtFirstName.Text;
            nonqueryCommand.Parameters["@LastName"].Value = txtLastName.Text;
 
            nonqueryCommand.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            // Display error
            lblErrMsg.Text = ex.ToString();
            lblErrMsg.Visible = true;
        }
        finally
        {
            // Close Connection
            thisConnection.Close();
        }
        GridView1.DataBind();
    }
    protected void
    Page_Load(object sender, System.EventArgs e)
    {
        lblErrMsg.Visible = false;
    }
}

Step 3: Click on Copy Code to Cut-n-paste code into web.config right after the appSettings section

<connectionstrings>
<add name="Northwind_ConnectionString" connectionstring="Server=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI">
</add><add name="Pubs_ConnectionString" connectionstring="Server=(local)\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=SSPI">
</add></connectionstrings>