【问题标题】:Export Gridview To Excel File - Error将 Gridview 导出到 Excel 文件 - 错误
【发布时间】:2012-05-07 15:27:29
【问题描述】:

我用这个code for exporting gridview to Excel in asp.net - c#.....但是我found some error in my code

我将stored procedure 用于sql command,我的代码如下......

C# 代码加载事件 (Calling GetData method)

   public partial class Admin_ResultDisplay : System.Web.UI.Page
   {
    SqlConnection cn;
    SqlCommand cmd = new SqlCommand();

    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString);
        cn.Open();

        SqlCommand cmd = (SqlCommand)Session["sqlcmd"];
        DataTable dt = GetData(cmd);
        GridView1.DataSource = dt;
        GridView1.DataBind(); 
    }

这里GetData() Method

private DataTable GetData(SqlCommand cmd)
{
    DataTable dt = new DataTable();
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString;
    //SqlConnection cn = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();

    Session["sqlcmd"] = cmd;
    cmd.CommandType = CommandType.Text;  // <= ERROR POINTED HERE....
    cmd.Connection = cn;
    try
    {
        cn.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        cn.Close();
        sda.Dispose();
        cn.Dispose();
    }
}

拉胡尔:

根据您的指导,我进行了更改,但仍然报错......

还有ERROR 像这样的东西......它在page_load 事件中具有空值,这就是它给出错误的原因......

对象引用未设置为对象的实例

【问题讨论】:

  • 当然,你还没有声明SqlCommand cmd;在 DataTable 之前 dt = GetData(cmd); //(gives error here) cmd is not exist in current context。另外我建议你声明私有 DataTable GetData() 而不是私有 DataTable GetData(SqlCommand cmd)

标签: c# asp.net compiler-errors export-to-excel error-correction


【解决方案1】:

这是因为您在按钮事件中定义cmd,如下所示,它在范围内是本地的。如果您在全局范围内需要它,请尝试相应地定义。

protected void btn_insert_Click(object sender, EventArgs e)    
 {
 try
     {
         SqlCommand cmd = new SqlCommand("GetExamResults", cn); 

编辑:

如果您也想在 page_load 中cmd,那么您可以在 page_load 中再次调用 SP 并使用它。喜欢

protected void Page_Load(object sender, EventArgs e) 
 {   
   cn = new SqlConnection(ConfigurationManager.ConnectionStrings     
   ["DbConnect"].ConnectionString);
   SqlCommand cmd = new SqlCommand("GetExamResults", cn);  

但建议您将 cmd 值存储在 session 中,然后在 page_load 中再次使用它,就像您使用 cmd 时一样

SqlCommand cmd = new SqlCommand();
// Your code goes here

Session["sqlcmd"] = cmd;

在页面加载中

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlCommand cmd = (SqlCommand)Session["sqlcmd"];

然后根据您的需要在 page_load 中使用cmd

【讨论】:

  • rahul -thanx 为您提供指导……我在全球范围内尝试过,但我需要 GetExamResults 程序的参数?那我该怎么做?给我一个示例解决方案,以便我理解...
  • Rahul - 根据您的指导,我进行了更改,但仍然给出错误......它在 page_load 事件中具有空值,这就是它给出错误的原因......**对象引用未设置为一个对象的实例**I have make changes in my question你可以看到它...
  • 您是否将 cmd 值存储到会话中?请放一个断点,看看session["sqlcmd"]中存储了什么。
  • Rahul - 它不需要任何东西直接给出错误消息....查看我在问题中的编辑我做了什么??如果我的代码有任何错误,请告诉我...
  • 完成 cmd 后,您需要将 cmd 存储在 session@button 事件代码块中。同样,在您的 Getdata() 方法中,您将 cmd 存储在会话中,但我没有看到您在该代码块中定义 cmd。
【解决方案2】:

错误是为您提供所需的信息。 cmd 确实不存在。您需要在 Page_Load 事件中创建一个 SqlCommand 实例。从您提供的代码中,您只是在按钮事件中定义了 cmd,这对 Page_Load 事件不可用。

【讨论】:

  • CM Kanode - 那么我必须在哪个地方进行更改??
  • 您需要在 Page_Load 事件中创建一个 SqlCommand 实例。此外,您根据 Rahul 的建议所做的更改是不完整的。您正在使用会话变量,但您实际上是否首先创建了会话变量?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2013-09-25
相关资源
最近更新 更多