在通过使用存储过程的结果填充DataTable对象来填充gridview时遇到问题。
这是我的代码:
SqlCommand sql = new SqlCommand();
sql.Connection = Conn;
sql.CommandText = "usp_CalcByProgFY_Weighted";
sql.CommandType = CommandType.StoredProcedure;
sql.Parameters.Add(new SqlParameter("@FiscalYear", ListBox2.SelectedItem.ToString()));
DataTable dt = new DataTable();
Conn.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sql;
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
我不断收到此错误:System.Web.dll中发生了类型为'System.Web.HttpException'的异常,但未在用户代码中处理其他信息:找不到名称为'FiscalYear'的字段或属性在所选数据源上
答案 0 :(得分:0)
一个猜测,也许是这样:
ListBox2.SelectedItem.ToString()
应该是这样
ListBox2.SelectedItem.Value.ToString()
答案 1 :(得分:0)
也许您需要的是SqlDataAdapter
DataTable dt = new DataTable();
using(SqlConnection connection = new SqlConnection(ConnectionString))
using(SqlCommand command = new SqlCommand("usp_CalcByProgFY_Weighted",connection))
using(SqlDataAdapter adapter = new SqlDataAdapter(command))
{
command.Type = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@FiscalYear", ListBox2.SelectedItem.Value.ToString()));
adapter.Fill(dt);
}
GridView2.DataSource = dt;
GridView2.DataBind();