C# ASP.NET Sql Command Update Statement Source Code Example
Sql Command Update Statement
Purpose: – Illustrates using Sql Command Update Statement in C-Sharp ASP.NET.
Prerequistes:
- Install Visual Studio 2022
- Watch this video to enable web forms in Visual Studio 2022 Enable Web Forms websites in Visual Studio 2022
- Install SQL Server Express
- Download Northwind Database
- 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:
- Use Visual Studio 2022
- 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.
- Add New folder named “Database_ADONET”
- Right-click project name in solution explorer;
- add new folder;
- name of folder could be: Database_ADONET
- Add Web Form Named SqlCommandUpdate 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 SqlCommandUpdate
- Click on copy code in code below to copy code into web form SqlCommandUpdate.aspx
- Click on copy code in second set of code below to copy code into code-behind SqlCommandUpdate.aspx.cs
- Right-click on SqlCommandUpdate.aspx in solution explorer and select view in browser
Step 1: Click on Copy Code to Cut-n-paste code into SqlCommandUpdate.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlCommandUpdate.aspx.cs" Inherits="Database_ADONET_SqlCommandUpdate" %> <title></title> <form id="form1" runat="server"> <div> <table> <tbody> <tr> <td>Find this First Name:</td> <td> <asp:textbox id="txtFirstName" runat="server"></asp:textbox> </td> </tr> <tr> <td>Update Last Name with this:</td> <td> <asp:textbox id="txtLastName" runat="server"></asp:textbox> </td> </tr> </tbody> </table> <asp:button id="btnUpdate" runat="server" text="Update" onclick="btnUpdate_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 SqlCommandUpdate.aspx.cs
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; partial class Database_ADONET_SqlCommandUpdate : System.Web.UI.Page { protected void btnUpdate_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(); // 1. Create Command // Sql Update Statement string updateSql = "UPDATE Employees " + "SET LastName = @LastName " + "WHERE FirstName = @FirstName"; SqlCommand UpdateCmd = new SqlCommand(updateSql, thisConnection); // 2. Map Parameters UpdateCmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10, "FirstName"); UpdateCmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 20, "LastName"); UpdateCmd.Parameters["@FirstName"].Value = txtFirstName.Text; UpdateCmd.Parameters["@LastName"].Value = txtLastName.Text; UpdateCmd.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> |