我正在开发一个银行应用程序,其中我编写了一个帮助程序类
发生的事情是每当我连续,5到7次或更多次,按下刷新按钮,它会给我这个错误:Fill: SelectCommand.Connection property has not been initialized.
任何机构都可以指定我在代码中缺少的内容,或者我还能做些什么来使其更好地执行而没有任何错误?
我的助手类功能是:
private static OdbcCommand cmd;
private static OdbcDataAdapter da;
private static DataTable dt;
public static void GetDataInDataGrid(string sp_Name, GridView gv)
{
dt = new DataTable();
try
{
using (cmd = new OdbcCommand(sp_Name))
{
cmd.Connection = Connection.ConnString.ConnectionString;
using (da = new OdbcDataAdapter(cmd))
{
da.Fill(dt);
if (dt.Rows.Count > 0)
{
gv.DataSource = dt;
gv.DataBind();
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
错误发生在以下代码中,位于throw语句
try
{
if (!IsPostBack)
{
herlperUtility.GetDataInDataGrid("{ CALL asp_sp_GetDataForSupervisor }", this.gvSuperviseDataGrid);
if (this.gvSuperviseDataGrid.DataSource == null)
{
this.divFailure.InnerText = "No Records Found!!!";
this.divFailure.Attributes.Add("style", "display:block;margin-top:20px;");
}
}
}
catch (Exception ex)
{
throw ex;
}
答案 0 :(得分:1)
写
using (cmd = new OdbcCommand(sp_Name,Connection.ConnString.ConnectionString))
取代
using (cmd = new OdbcCommand(sp_Name))
{
cmd.Connection = Connection.ConnString.ConnectionString;