Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All ,

i have one dataGrid in my WebForm and i want to use
a Stored Procedure to populate the DataGrid.

Can any one Help me please . . .

Many Thanks.
Posted
Updated 18-May-12 19:10pm
v2

 
Share this answer
 
Hi ,
Check this
C#
//retrieve Data .
using (
                  SqlConnection con =
                      new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
        {


            using (SqlCommand cmd = new SqlCommand("usp_test", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("tst", txt.Text);
                DataTable dt = new DataTable();
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                adpt.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

Calling Stored procedures in ADO.NET[^]
http://www.csharp-station.com/Tutorial/AdoDotNet/lesson07[^]
http://www.c-sharpcorner.com/UploadFile/718fc8/saving-record-using-stored-procedure-in-ado-net/[^]
http://msdn.microsoft.com/en-us/library/59x02y99.aspx[^]
Best Regards
M.Mitwalli
 
Share this answer
 
v2
Hi,

Use this,

C#
SqlCommand cmd=new SqlCommand("spGetData", con);
//con is your connection object.
SqlDataAdapter sda=new SqlDataAdapte(cmd) ;
DataTable dt=new DataTable();
sda.Fill(dt);
con.Close();
DataGrid1.DataSource=dt;
DataGrid1.DataBind();


This will help you.
All the best.

-AK
 
Share this answer
 
Use as
C#
          SqlCommand cmd = new SqlCommand("yourstoredprocedure",yourconnection);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@rid", Session["cust_id"].ToString());
// you can add more parameters
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
           dt.Locale = System.Globalization.CultureInfo.InvariantCulture;
            adp.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
 
Share this answer
 
Let us put GridView1 control on the web form from Toolbox. The coding is straight forward and is like the following:

C#
protected void Page_Load(object sender, EventArgs e)
{
 
string strSQLconnection = "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("select * from table1", sqlConnection);
sqlConnection.Open();
 
SqlDataReader reader = sqlCommand.ExecuteReader();
       
GridView1.DataSource = reader;
GridView1.DataBind();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900