Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:14:32+00:00 2026-06-03T11:14:32+00:00

i use this code for exporting gridview to Excel in asp.net – c# …..but

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:14:34+00:00Added an answer on June 3, 2026 at 11:14 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code: [image setImage:[UIImage imageNamed:@some@2x.png]]; and it work on ios 4.3, but
I use Devexpress ASP.NET component. My question is; Can i use Devexpress Exporting Code
I use this code to load a .Net assembly to PowerShell: [System.Reflection.Assembly]::Load(System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
I have already seen a post about the error but This is some what
I have a gridview control in ASP.Net 2.0 and i need to export this
I use this code to update data in database table. Can reuse same code
I use this code: http://blogswizards.com/plugin-development/sliding-boxes-and-captions-with-jquery On a simple gallery site I am building. Specifically
I use this code to process a date string coming in from a json
I use this code which is taken from MVC futures and attach the Attribute
I use this code to generate a random number. Random R = new Random(0);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.