Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Gridview导出到Excel文件-错误_C#_Asp.net_Compiler Errors_Export To Excel_Error Correction - Fatal编程技术网

C# 将Gridview导出到Excel文件-错误

C# 将Gridview导出到Excel文件-错误,c#,asp.net,compiler-errors,export-to-excel,error-correction,C#,Asp.net,Compiler Errors,Export To Excel,Error Correction,我使用此代码将gridview导出到asp.net-c#中的Excel…但我在代码中发现了一些错误 我正在为sql命令使用存储过程,我的代码如下 C#code加载事件(调用GetData方法) 这里GetData()方法 public partial class Admin_ResultDisplay : System.Web.UI.Page { SqlConnection cn; SqlCommand cmd = new SqlCommand(); pro

我使用此
代码将gridview导出到asp.net-c#
中的Excel…但我
在代码中发现了一些错误

我正在为
sql命令使用
存储过程
,我的代码如下

C#code加载事件(
调用GetData方法

这里
GetData()方法

   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(); 
    }
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();
    }
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt=新的DataTable();
String strConnString=System.Configuration.ConfigurationManager.ConnectionString[“DbConnect”].ConnectionString;
//SqlConnection cn=新的SqlConnection(strConnString);
SqlDataAdapter sda=新的SqlDataAdapter();
会话[“sqlcmd”]=cmd;

cmd.CommandType=CommandType.Text;//这是因为您在按钮事件中定义了
cmd
,如下所示,它在作用域中是局部的。如果您需要全局定义,请尝试相应地定义

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

如果你想在pageu加载中
cmd
,那么你可以在pageu加载中再次调用SP并使用它

protected void Page_Load(object sender, EventArgs e) 
 {   
   cn = new SqlConnection(ConfigurationManager.ConnectionStrings     
   ["DbConnect"].ConnectionString);
   SqlCommand cmd = new SqlCommand("GetExamResults", cn);  
但是建议您将
cmd
值存储在会话中,并在页面加载中再次使用它,这样您就可以不再使用
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
,错误为您提供了所需的信息。cmd确实不存在。您需要在page_load事件中创建一个SqlCommand实例。根据您提供的代码,您仅在按钮事件中定义cmd,而该按钮事件对page_load不可用d事件。

当然,在DataTable dt=GetData(cmd)之前,您没有声明SqlCommand cmd;
/(此处给出错误)cmd在当前上下文中不存在。
。此外,我建议您声明私有DataTable GetData()而不是私有DataTable GetData(SqlCommand cmd)CM Kanode-那么我必须在哪个位置进行更改??您需要在Page_Load事件中创建一个SqlCommand实例。此外,您根据Rahul的建议所做的更改是不完整的。您正在使用会话变量,但您确实是先创建了会话变量吗?Rahul-thanx作为指南…我用全局变量进行了尝试但是我需要GetExamResults过程的参数??那么我该怎么做呢?给我一个示例解决方案,这样我就能理解…Rahul-根据你的指导,我做了更改,但仍然给出了错误…它在page_load事件中有空值,这就是它给出错误的原因…**对象引用未设置为对象的实例**
我在m中进行了更改y问题
您可以看到它…您是否将cmd值存储到会话中?请输入断点,然后查看在
会话[“sqlcmd”]中存储的内容
.Rahul-它不直接接受任何内容,并给出错误消息……请查看问题中的编辑我做了什么??如果我的代码中有任何错误,请告诉我……在使用cmd后,您需要将cmd存储在session@button事件代码块中。同样,在
Getdata()中
method您正在会话中存储cmd,但我没有看到您在该代码块中定义cmd。