Powered by Blogger.

Sunday, February 23, 2014

Call stored procedure in Asp.Net using C#.Net



In this post we will discuss how to call a stored procedure in asp.net using C#.Net code. In this example it will take one input parameter as empid and will return the result records for the particular empid.

Also check out:

- RequiredFieldValidator example in Asp.Net

- Get control value using JavaScript with master page in Asp.Net

- Query to get records between two dates in sql server 2008

Below is the full code:

       string connectionString = "Your connection string goes here!";

        SqlConnection con = new SqlConnection(connectionString);

        SqlCommand cmd = new SqlCommand("SP_NameOfYourSP", con);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@empid", SqlDbType.Int).Value = 12345;

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        try
        {
            con.Open();

            da.Fill(ds, "Authors");
        }
        catch (Exception ex)
        {

            throw;
        }
        finally
        {
            con.Close();
        }