Вопрос

i use this code for exporting gridview to Excel in asp.net - c#.....but i found some error in my code

i am using stored procedure for sql command and my code behind is as follows....

C# code load event (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(); 
    }

here 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();
    }
}

RAHUL:

As per your guidence i make changes but still gives error....

And ERROR Something like this...it has null value at page_load event thats why it gives error......

Object reference not set to an instance of an object

Это было полезно?

Решение

That's beacuse you are defining cmd in button event as below which is local in scope. If you need it globally then try defining accordingly.

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

EDIT:

If you want to cmd in page_load as well then you can call the SP again in page_load and use it. Like

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

But suggest you store the cmd value in session ans reuse it again in page_load like, where your use of cmd getting over

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

Session["sqlcmd"] = cmd;

In page_load

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

Then use cmd in page_load as well per your need

Другие советы

The error is giving you the information that you need. cmd does, indeed, not exist then. You need to create an instance of SqlCommand in your Page_Load event. From the code you have provided, you are only defining cmd in the button event which is not available to the Page_Load event.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top