使用复选框在GridView中显示存储过程的结果

时间:2012-05-24 10:50:17

标签: c# asp.net .net asp.net-mvc

我需要在gridview中使用复选框显示存储过程的结果。但是我的结果集并没有被绑定到gridview,因为我正在处理一个已经将连接字符串作为dll内置的现有门户网站。这是我的代码..

protected void Go_Rgn_Click(object sender, EventArgs e)
{
    SecurityController SC = new SecurityController();
    plhTable.Visible = true;
    plhChoose.Visible = false;
    plhForm.Visible = false;
    string Rgn = afRgn.Text;
    DataTable dt = new DataTable();

    dt=SC.BindGridView(Rgn);
    if (dt.Rows.Count > 0)
    {
        gvAll.DataSource = dt;
        gvAll.DataBind();
    }
}

编译器抛出错误

  

无法将类型void转换为System.Data.DataTable

请告诉我这个。

DataAccess Layer:

public DataTable BindGridView(string rgn)
{
    string spdName = "spd_get_default_region";
    DbCommand cmd = DB.GetStoredProcCommand(spdName);
    DB.AddInParameter(cmd, "Rgn", DbType.String, rgn);
    return  DB.ExecuteNonQuery(cmd);
}

1 个答案:

答案 0 :(得分:2)

当预期输出是数据表时,您正在尝试返回空白:

错误在线:

return  m_WorkoutPortal_DB.ExecuteNonQuery(cmd);

ExecuteNonQuery不返回数据表

编辑:

public DataTable BindGridView(string rgn)
{
    string spdName = "spd_get_default_region";

    DbCommand cmd = m_WorkoutPortal_DB.GetStoredProcCommand(spdName);
    m_WorkoutPortal_DB.AddInParameter(cmd, "Rgn", DbType.String, rgn);

    SqlDataAdapter da = new SqlDataAdapter(cmd); // Create a SQL Data Adapter and assign it the cmd value. 

    DataTable dt = new DataTable(); // Create a new Data table
    da.Fill(dt); // Fill the data table with the results from the query

    return dt; // return the data table
 }


protected void Go_Rgn_Click(object sender, EventArgs e)
{
  SecurityController SC = new SecurityController();
  plhTable.Visible = true;
  plhChoose.Visible = false;
  plhForm.Visible = false;
  string Rgn = afRgn.Text;

  gvAll.DataSource = SC.BindGridView(Rgn);
  gvAll.DataBind();
}