ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Calling Update SQL Server Stored Procedures using ADO.Net
Author Name: Mudassar Khan Published Date: May 29, 2009
Filed Under :
ADO.Net
 |
SQL Server
Views: 4229

This is the last part of the SQL Server Stored Procedure series where I will explain how to call and create stored procedures that update existing data in the SQL Server database using ADO.Net Classes.

If you need to know more about the syntax of Stored Procedures refer my article Using Stored Procedures in SQL Server Database

For this tutorial I am using the Employees Table of NorthWind Database. You can download the NorthWind Database from here

 

Connection String

<connectionStrings>

      <add name="conString"

        connectionString="Data Source=.\SQLEXPRESS;

                          database=Northwind;Integrated Security=true"/>

</connectionStrings>

 

Namespaces

You will need to import the following namespaces

 

C#

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

 

VB.Net

Imports System.Data

Imports System.Data.SqlClient

Imports System.Configuration

 


Update Stored Procedures

Below is an Update stored procedure that takes FirstName and LastName of the employee as parameters and based on that it updates the city and country of the employee.

   

    

 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[UpdateEmployeeDetails]

      @FirstName varchar(50),

      @LastName varchar(50),

      @City varchar(50),

      @Country varchar(50)

AS

BEGIN

      SET NOCOUNT ON;

      UPDATE  Employees SET City = @City, Country = @Country

      WHERE FirstName=@FirstName AND LastName=@LastName

END

 

Now in order to execute the above stored procedure in your ASP.Net Web applications refer the code below.  As you will notice I am passing the values to the parameters @FirstName, @LastName, @City and @Country using the TextBoxes txtFirstName, txtLastName, txtCity and txtCountry respectively. Since the stored procedure does not return anything I am using the ExecuteNonQuery method of the SQL Command object


C#

String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "UpdateEmployeeDetails";

cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text.Trim();

cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text.Trim();

cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim();

cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim();

cmd.Connection = con;

try

{

    con.Open();

    cmd.ExecuteNonQuery();

    lblMessage.Text = "Record updated successfully";

}

catch (Exception ex)

{

    throw ex;

}

finally

{

    con.Close();

    con.Dispose();

}



VB.Net

 

Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString

Dim con As New SqlConnection(strConnString)

Dim cmd As New SqlCommand()

cmd.CommandType = CommandType.StoredProcedure

cmd.CommandText = "UpdateEmployeeDetails"

cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text.Trim()

cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text.Trim()

cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim()

cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim()

cmd.Connection = con

Try

   con.Open()

   cmd.ExecuteNonQuery()

   lblMessage.Text = "Record updated successfully"

Catch ex As Exception

   Throw ex

Finally

   con.Close()

   con.Dispose()

End Try

 

This completes the final part of stored procedure article. You can download the code in VB.Net and C# for the above part using the link below.

UpdateStoredProc.zip (4.12 kb)

<< Part - III Getting ID of the newly inserted record in SQL Server Database using ADO.Net


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Nida Zafar said:
Hibr Is there a procedure that would do both insertion andor update. For example . if the employee is not present insert it and if hes there then edit his details.
March 27, 2010  

Vaibhav Maheshwari said:
I Suggest that you should have 3 download links. One for C sharp code . one for Vb.net . one for full .br Thanx for this tut just looking for this
May 30, 2010  

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code