Click here to Skip to main content
15,885,869 members
Articles / Web Development / ASP.NET

Sending a DataTable to a Stored Procedure

Rate me:
Please Sign up or sign in to vote.
4.75/5 (51 votes)
10 Jul 2012CPOL3 min read 331.1K   3.4K   104  
Sending a DataTable to a Stored Procedure using Table Valued Parameters.
#region Developer Notes
//Objective : To execute the procedure and return data to the business logic page
//Created By : Amit Kumar
//Created Date : 12/01/2012
//Company : KALS Information System Ltd, Bangalore
/************************************************************************************************
 * Modified date        Modified By         Version              Reasons for Modification       *             
 * **********************************************************************************************
 *                                                                                              *
 * **********************************************************************************************
//Considerations: Through this class we can maintain business logic of the Address Details Page
//
//Functions in this Class:
 // 1. fnConOpen            :   Opens the connection
 *  2. fnConClose           :   Closes the connection
 *  3. fnEXECStoredProc     :   Executes a procedures which returns a dataset
 *  4. fnStoredProc         :   Executes a procedures and gives datatable as its parameter
*/
#endregion

#region Namespaces
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
#endregion

/// <summary>
/// Objective : Data access layer class
/// </summary>
public class csDBMgr
{
    #region Connection String
    /// <summary>
    /// Objective : Specifies connection string
    /// </summary>
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EmpCon"].ConnectionString);
    #endregion

    #region Connection Open
    /// <summary>
    /// Type        : Function
    /// Objective   : Opens a connection
    /// </summary>
    public void fnConOpen() 
    {
        if (con.State != ConnectionState.Open) {
            con.Open();
        }
    }
    #endregion

    #region Connection Close
    /// <summary>
    /// Type        : Function
    /// Objective   : Closes a connection
    /// </summary>
    public void fnConClose() 
    {
        if (con.State != ConnectionState.Closed) 
        {
            con.Close();
        }
    }
    #endregion

    #region Execute Procedure Which Returns Dataset
    /// <summary>
    /// Type        : Function
    /// Objective   : Execute procedure which returns dataset
    /// Parameters  :
    ///         strProcName - Stored Procedure Name
    ///         dtParamDetails - Stored procedure parameters details
    /// </summary>
    /// <param name="strProcName"></param>
    /// <param name="dtParamDetails"></param>
    /// <returns></returns>
    public DataSet fnExecStoredProc()
    {
        DataSet dsRecords = new DataSet();
        try
        {
            fnConOpen();
            SqlCommand cmdProc = new SqlCommand("spEmpDetails", con);
            cmdProc.CommandType = CommandType.StoredProcedure;
            cmdProc.Parameters.AddWithValue("@Type", "FetchDetails");
            cmdProc.Parameters.AddWithValue("@Details", null);
            SqlDataAdapter sdaTablesColl = new SqlDataAdapter(cmdProc);
            sdaTablesColl.Fill(dsRecords);
            cmdProc.Dispose();
            sdaTablesColl.Dispose();
        }
        catch (Exception )
        {
            dsRecords = null;
        }
        finally {
            fnConClose();
        }
        return dsRecords;
    }
    #endregion

    #region Execute Procedure Which Returns string
    /// <summary>
    /// Type        : Function
    /// Objective   : Execute procedure and make changes in the database table and returns the string
    /// Parameters  :
    ///         dtDetails - DataTable with new and edited values
    /// </summary>
    /// <param name="strProcName"></param>
    /// <param name="dtParamDetails"></param>
    /// <returns></returns>
    public string fnStoredProc(DataTable dtDetails)
    {
        string strMsg = "";
        try
        {
            fnConOpen();
            SqlCommand cmdProc = new SqlCommand("spEmpDetails", con);
            cmdProc.CommandType = CommandType.StoredProcedure;
            cmdProc.Parameters.AddWithValue("@Type", "InsertDetails");
            cmdProc.Parameters.AddWithValue("@Details", dtDetails);
            cmdProc.ExecuteNonQuery();
            strMsg = "Saved successfully.";
        }
        catch (SqlException e) {
            //strMsg = "Data not saved successfully.";
            strMsg = e.Message.ToString();
        }
        finally 
        {
            fnConClose();
        }
        return strMsg;
    }
    #endregion

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Malaysia Malaysia
I've been working with various Microsoft Technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an attitude for learning new skills and utilizing that in my work.


--Amit Kumar
You can reach me at:
Facebook | Linkedin | Twitter | Google+

Comments and Discussions