Calling Any Stored Procedure In ADO.NET

Sqlser2005 Table[BookMaster] Structure

CREATE TABLE YourTableName
(
    BookID INT,
    Title NVARCHAR(50),
    Author NVARCHAR(50),
    Pubs NVARCHAR(50),
    Category NVARCHAR(50),
    EntryDate DATETIME,
    Price DECIMAL(18, 2),
    PurchasedFrom NVARCHAR(50),
    BookFor NCHAR(10),
    Status INT
);

Stored procedure in ADO.NET 

CREATE PROCEDURE [dbo].[SPADDBOOK]
(
    @Title nvarchar(50) = NULL,
    @Author nvarchar(50) = NULL,
    @Pubs nvarchar(50) = NULL,
    @Category nvarchar(50) = NULL,
    @EntryDate nvarchar(50) = NULL,
    @Price float = NULL,
    @PurchasedFrom nvarchar(50) = NULL,
    @BookFor nvarchar(50) = NULL,
    @BookId int OUTPUT,
    @Status int OUTPUT
)
AS
BEGIN
    SET NOCOUNT ON;
    SET @Price = CAST(@Price AS float);
    INSERT INTO BookMaster (Title, Author, Pubs, Category, EntryDate, Price, PurchasedFrom, BookFor)
    VALUES (@Title, @Author, @Pubs, @Category, @EntryDate, @Price, @PurchasedFrom, @BookFor);
    SET @BookId = SCOPE_IDENTITY();
    IF @@ERROR = 0
        SET @Status = 1;
    ELSE
        SET @Status = 0;
END

Front End Code FRM CODE

public partial class BooksList : Form
{
    DataAccess DA = new DataAccess();
    DataTable DT;
    private void button2_Click(object sender, EventArgs e)
    {
        int ValidationFlag = 1;
        if (txtBookTitle.Text == "")
        {
            MessageBox.Show("Please Enter Book Title");
            txtBookTitle.Focus();
            ValidationFlag = 0;
        }
        else if (txtAuthor.Text == "")
        {
            MessageBox.Show("Please Enter Book Author");
            txtAuthor.Focus();
            ValidationFlag = 0;
        }
        else if (txtentrydate.Text == "")
        {
            MessageBox.Show("Please Enter Book Entry Date");
            txtentrydate.Focus();
            ValidationFlag = 0;
        }
        else if (txtprice.Text == "")
        {
            MessageBox.Show("Please Enter Book Price");
            txtprice.Focus();
            ValidationFlag = 0;
        }
        else if (txtPublication.Text == "")
        {
            MessageBox.Show("Please Enter Book Publication");
            txtPublication.Focus();
            ValidationFlag = 0;
        }
        else if (txtpurchasefrom.Text == "")
        {
            MessageBox.Show("Please Enter Book Purchase From");
            txtpurchasefrom.Focus();
            ValidationFlag = 0;
        }

        if (ValidationFlag == 1)
        {
            string BookType = rbrent.Checked ? "RENT" : "SALE";
            string[,] PramIn = new string[8, 2]
            {
                { "@Title", txtBookTitle.Text },
                { "@Author", txtAuthor.Text },
                { "@Pubs", txtPublication.Text },
                { "@Category", txtcategory.Text },
                { "@EntryDate", txtentrydate.Text },
                { "@Price", txtprice.Text },
                { "@PurchasedFrom", txtpurchasefrom.Text },
                { "@BookFor", BookType }
            };
            string[,] PramOut = new string[2, 2]
            {
                { "@BookId", "SqlDbType.Int" },
                { "@Status", "SqlDbType.Int" }
            };

            string[,] OutResult;
            int NewBookId = 0;
            int StatusId = 0;
            try
            {
                DataAccess DA = new DataAccess();
                OutResult = (string[,])DA.CallStoredProcedure(PramIn, PramOut, "SPADDBOOK");
                NewBookId = Convert.ToInt32(OutResult[0, 1]);

Data Access Class Code in ADO.NET

class DataAccess
{
    SqlConnection Conn;
    SqlDataAdapter DA = new SqlDataAdapter();
    SqlCommandBuilder CB;
    DataTable DT;
    public SqlConnection OpenConnection()
    {
        Conn = new SqlConnection("data source=;database=bookmaster;Integrated Security=SSPI");
        return Conn;
    }
    public Array CallStoredProcedure(string[,] MyINPram, string[,] MyOUTPram, string MySPName)
    {
        string Result;
        Conn = OpenConnection();
        SqlCommand CmdSp = new SqlCommand();
        CmdSp.Connection = Conn;
        CmdSp.CommandText = MySPName.ToString();
        CmdSp.CommandType = CommandType.StoredProcedure;
        // Input parameters
        for (int i = 0; i < MyINPram.Length / 2; i++)
        {
            CmdSp.Parameters.AddWithValue(MyINPram[i, 0], MyINPram[i, 1]);
        }
        // Output parameters
        for (int i = 0; i < MyOUTPram.Length / 2; i++)
        {
            if (MyOUTPram[i, 1] == "SqlDbType.VarChar")
            {
                CmdSp.Parameters.Add(MyOUTPram[i, 0], SqlDbType.VarChar, 150);
            }
            else
            {
                CmdSp.Parameters.Add(MyOUTPram[i, 0], SqlDbType.Int, 4);
            }
            CmdSp.Parameters[MyOUTPram[i, 0]].Direction = ParameterDirection.Output;
        }
        Conn.Open();
        CmdSp.ExecuteNonQuery();
        Conn.Close();
        string[,] Temparray = new string[MyOUTPram.Length / 2, 2];
        for (int i = 0; i < MyOUTPram.Length / 2; i++)
        {
            Result = Convert.ToString(CmdSp.Parameters[MyOUTP


Similar Articles