Step 1: Create Stored Procedure, see below code and modify according to your need.
CREATE PROCEDURE [dbo].[GetProducts]
(
@CategoryID int
)
AS
SELECT ProductID, ProductName FROM Products WHERE CategoryID = @CategoryID
Step 2: Please see below code and explaination for each line.
Complete Code
SqlConnection conn = new SqlConnection("Data
Source=localhost;Database=Northwind;Integrated Security=SSPI");
SqlCommand command = new SqlCommand("GetProducts", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 1;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds, "Products");
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "Products";
Explanation of above code line by line
Make sqlconnection using your connectionstring
SqlConnection conn = new SqlConnection("Data
Source=localhost;Database=MyDB;Integrated Security=SSPI");
Declare sqlcommand and pass your Stored Procedure name and make connection to it.
All pass all the parameter to Stored Procudure.
SqlCommand command = new SqlCommand("GetProducts", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@CategoryID", SqlDbType.Int).Value = 1;
Declare sqldataadapter and assign command to this,
SqlDataAdapter adapter = new SqlDataAdapter(command);
Declare dataset and fill dataset using sqldataadapter and table name "Products".
DataSet ds = new DataSet();
adapter.Fill(ds, "Products");
Fill datagrid by dataset ds,
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "Products";
Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com
No comments :
Post a Comment