同一页面中有2个gridviews,2个存储过程。 C#

时间:2018-11-01 18:17:49

标签: c# asp.net sql-server stored-procedures

我要在同一页面中2个gridview报告。 Gridview1从存储过程1获取输出 Gridview2从存储过程2获取输出

我的gridview1代码如下所示。我想知道如何在同一页面中但从不同的存储过程中填充gridview2。

   string constr = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    //con.Open();
    SqlCommand cmd = new SqlCommand();

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.CommandText = "OTD";

    cmd.Connection = con;

    try
    {

        con.Open();
        string sDate = DateTime.Now.ToString();
        DateTime datevalue = (Convert.ToDateTime(sDate.ToString()));

        string dy = datevalue.Day.ToString();
        string mn = datevalue.Month.ToString();
        string yy = datevalue.Year.ToString();
        GridView1.EmptyDataText = "No Records Found";
        cmd.Parameters.AddWithValue("@Year", yy);
        cmd.Parameters.AddWithValue("@Month", mn);
        GridView1.DataSource = cmd.ExecuteReader();

        GridView1.DataBind();

1 个答案:

答案 0 :(得分:0)

假设这一切都在同一.aspx页的代码后面

在您的第一个网格视图绑定之后

cmd =新的SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "My second SP name is herer";

cmd.Connection = con;

尝试     {

    //con.Open(); i am already opened you neverclosed me

    GridView1.EmptyDataText = "No Records Found";
     //

添加您需要的任何参数 //         GridView2.DataSource = cmd.ExecuteReader();

    GridView2.DataBind();

} (赶上前){

}