Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can u call the two store procedure inside the code behind file in(page_load event).

grid view and datalist there are two control used,,,,how can you call two procedure for that???

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (Page.IsPostBack == false)
       {
           SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["BuildMyPCConnectionString"].ToString());
           objConn.Open();

           SqlCommand objCmd = objConn.CreateCommand();
           objCmd.CommandType = CommandType.StoredProcedure;
           objCmd.CommandText = "PR_ProductDetail_SelectByPK";
           objCmd.Parameters.AddWithValue("@ProductID", Convert.ToInt32(Request.QueryString["ProductID"]));

           SqlDataReader objSdr = objCmd.ExecuteReader();

           dlProductDetail.DataSource = objSdr;
           dlProductDetail.DataBind();
       }

   }
this is for datalist ...how can u call store procedure for gridview....
Posted
Updated 27-Mar-13 23:44pm
v2

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["BuildMyPCConnectionString"].ToString());
            objConn.Open();
 
            SqlCommand objCmd = objConn.CreateCommand();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "PR_ProductDetail_SelectByPK";
            objCmd.Parameters.AddWithValue("@ProductID", Convert.ToInt32(Request.QueryString["ProductID"]));
 
            SqlDataReader objSdr = objCmd.ExecuteReader();
 
            dlProductDetail.DataSource = objSdr;
            dlProductDetail.DataBind();
//gridview code begins

 objCmd = objConn.CreateCommand();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "PR_ProductDetail_SelectByPK_two";
            objCmd.Parameters.AddWithValue("@ProductID", Convert.ToInt32(Request.QueryString["ProductID"]));
 
           SqlDataReader  objSdr2 = objCmd.ExecuteReader();
 
            grdProductDetail.DataSource = objSdr2;
            grdProductDetail.DataBind();

objSdr.Close();
objSdr2.Close()


        }
 
    }
 
Share this answer
 
v2
Comments
Ankit Gajera 28-Mar-13 7:24am    
this is not work...
vinayraghavendra.hs 28-Mar-13 7:52am    
create new sqlcommand object with different name
bbirajdar 28-Mar-13 10:42am    
Corrected the code.. Dont just say 'it does not work' ..Post the error message
VB
hi

if your binding same data to both then bind reader object to gridview also. else create new command and datareader object and bind it. create different sqlcommand object
 
Share this answer
 
Comments
Ankit Gajera 28-Mar-13 8:00am    
can you say like this....

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["BuildMyPCConnectionString"].ToString());
objConn.Open();

SqlCommand objCmd = objConn.CreateCommand();
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.CommandText = "PR_ProductID_SelectByPK";
objCmd.Parameters.AddWithValue("@ProductID", Convert.ToInt32(Request.QueryString["ProductID"]));

SqlDataReader objSdr = objCmd.ExecuteReader();

dlProductDetail.DataSource = objSdr;
dlProductDetail.DataBind();


SqlCommand objCommand = objConn.CreateCommand();
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.CommandText = "PR_ProductSpec_SelectByPK";
objCommand.Parameters.AddWithValue("@ProductID", Convert.ToInt32(Request.QueryString["ProductID"]));
SqlDataReader objSqldr = objCmd.ExecuteReader();
GridView1.DataSource = objSqldr;
dlProductDetail.DataSource = objSqldr;
GridView1.DataBind();


}

}
this is not work error occur
There is already an open DataReader associated with this Command which must be closed first.
vinayraghavendra.hs 28-Mar-13 8:13am    
in above pgm for 2nd procedure you used objCmd.ExecuteReader(); its wrong use objCommand.ExecuteReader(); if then also problem persist then close old connection and open new one.
vinayraghavendra.hs 28-Mar-13 8:15am    
you don need to bind data to datalist in 2nd procedure pgm early binding is enough.
Try below..
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
             //for datalist
            SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["BuildMyPCConnectionString"].ToString());
            objConn.Open();
 
            SqlCommand objCmd = objConn.CreateCommand();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "PR_ProductDetail_SelectByPK";
            objCmd.Parameters.AddWithValue("@ProductID", Convert.ToInt32(Request.QueryString["ProductID"]));
 
            SqlDataReader objSdr = objCmd.ExecuteReader();
            dlProductDetail.DataSource = objSdr;
            dlProductDetail.DataBind();
            
            //for gridview
            SqlCommand objCmd1 = objConn.CreateCommand();
            objCmd1.CommandType = CommandType.StoredProcedure;
            objCmd1.CommandText = "PR_ProductDetail_SelectByPK_Gridview";
            objCmd1.Parameters.AddWithValue("@ProductID", Convert.ToInt32(Request.QueryString["ProductID"]));
 
            SqlDataReader objSdr1 = objCmd1.ExecuteReader();
            Gridview1.DataSource = objSdr1;
            Gridview1.DataBind();
            
            con.Close();

        }
    }
 
Share this answer
 
Comments
[no name] 28-Mar-13 8:39am    
There is no need to post yet another solution. Improve the one you already have. And this code would not even compile anyway. You are declaring multiple variables with the same name in the same scope. Additionally, this is pretty much the same as solution 1.
C#
protected void Search_Zip_Plan_Age_Button_Click(object sender, EventArgs e)
   {
       using (SqlConnection cn = new SqlConnection())
       {
           cn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PriceFinderConnectionString"].ToString();
           cn.Open();

           using (SqlCommand cmd = cn.CreateCommand())
           {
               cmd.CommandText = "get_zip_plan_age";
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.CommandText = "get_lowest_female_rate";
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.CommandText = "get_lowest_male_rate";
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.CommandText = "get_carrier_info";
               cmd.CommandType = CommandType.StoredProcedure;


               SqlParameter parm = cmd.CreateParameter();
               parm.ParameterName = "@insur_age";
               parm.DbType = DbType.Int64;
               parm.Value = Convert.ToInt64(this.insur_age.Text);
               cmd.Parameters.Add(parm);

               parm = cmd.CreateParameter();
               parm.ParameterName = "@zip_code";
               parm.DbType = DbType.String;
               parm.Value = this.ZipCode.Text;
               cmd.Parameters.Add(parm);

               parm = cmd.CreateParameter();
               parm.ParameterName = "@plan_code";
               parm.DbType = DbType.String;
               parm.Value = this.PlanCode.Text;
               cmd.Parameters.Add(parm);

               SqlDataReader reader = cmd.ExecuteReader();
               Zip_Plan_Age_GridView.DataSource = reader;
               Zip_Plan_Age_GridView.DataBind();
           }
       }
   }
 
Share this answer
 
v2
Comments
Ankit Gajera 28-Mar-13 10:20am    
no this will not work again that error occur......here is already an open DataReader associated with this Command which must be closed first.
Ankit Gajera 28-Mar-13 10:22am    
abov code not understand what you say actually....please implemanting in my code,,,,
Ankit Gajera 29-Mar-13 3:08am    
this is an error....

There is already an open DataReader associated with this Command which must be closed first.

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