我需要在带有复选框的网格 View 中显示存储过程的结果。但是我的结果集没有绑定(bind)到 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();
}
}
编译器抛出一个错误
请就此给我建议。
数据访问层:
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);
}
最佳答案
当预期的输出是数据表时,您试图返回一个空值:
错误在行:
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();
}
关于c# - 使用复选框在 GridView 中显示存储过程的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10736004/