3

我需要在带有复选框的网格视图中显示存储过程的结果。但是我的结果集没有绑定到 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

请就此给我建议。

数据访问层:

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);
}
4

1 回答 1

2

当预期的输出是数据表时,您试图返回一个 void:

错误在行:

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();
}
于 2012-05-24T10:53:08.217 回答