Skip to content

Instantly share code, notes, and snippets.

@sin2akshay
Forked from appforest/cs_dataset-sp
Last active April 24, 2018 12:32
Show Gist options
  • Save sin2akshay/03caf76de5e892841df68749b2ed262f to your computer and use it in GitHub Desktop.
Save sin2akshay/03caf76de5e892841df68749b2ed262f to your computer and use it in GitHub Desktop.
C# - Calling a SQL Server Stored Procedure. Using a dataset to get the data.
string CS = ConfigurationManager.ConnectionStrings["test_connectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter da = new SqlDataAdapter("testProcedure3", con); // Using a Store Procedure.
//SqlDataAdapter da = new SqlDataAdapter("SELECT 'this is a test text' as test", con); To use a hard coded query.
da.SelectCommand.CommandType = CommandType.StoredProcedure; // Comment if using hard coded query.
DataSet ds = new DataSet(); // Definition: Memory representation of the database.
da.SelectCommand.Parameters.AddWithValue("@ggg", 95); // Repeat for each parameter present in the Store Procedure.
da.Fill(ds); // Fill the dataset with the query data
GridView1.DataSource = ds; // Set the dataso... meh, you get it.
GridView1.DataBind();
//TextBox1.Text=GridView1.Rows[0].Cells[0].Text; If you want to pass the data to another element.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment