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 7716763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:42:49+00:00 2026-06-01T02:42:49+00:00

I get a null – reference in this Code-line: **DataRow dr = tableSelectedItems.NewRow();** I

  • 0

I get a null – reference in this Code-line:

 **DataRow dr = tableSelectedItems.NewRow();**

I can’t find out why. Can anyone help me please?
My code should fill the books which were Selected by the user in a datalist. This is to learn how Sessionstate works. I used the book “ASP.NET 3.5 Step by Step” in a German version. I have exactly the same code as them, but i’m using .net 4.0 instead of 3.5.
Can please someone help me??
All the code of the DatalistItemdCommand is:

      protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int nItemIndex = e.Item.ItemIndex;
        this.DataList1.SelectedIndex = nItemIndex;

        BindToinventory();

        //Row's Order: ID, Title, FirstName, LastName, Topic, Publisher

        DataTable dt = (DataTable)DataList1.DataSource;
        string strID = (dt.Rows[nItemIndex][0]).ToString();
        string strTitle = (dt.Rows[nItemIndex][1]).ToString();
        string strAuthorLastName = (dt.Rows[nItemIndex][2]).ToString();
        string strAuthorFirstName = (dt.Rows[nItemIndex][3]).ToString();
        string strTopic = (dt.Rows[nItemIndex][4]).ToString();
        string strPublisher = (dt.Rows[nItemIndex][5]).ToString();

        DataTable tableSelectedItems;
        tableSelectedItems = (DataTable)Session["tableSelectedItems"];

        //Null Reference is here
        DataRow dr = tableSelectedItems.NewRow();
        dr[0] = strID;
        dr[1] = strTitle;
        dr[2] = strAuthorLastName;
        dr[3] = strAuthorFirstName;
        dr[4] = strTopic;
        dr[5] = strPublisher;

        tableSelectedItems.Rows.Add(dr);

        Session["tableSelectedItems"] = tableSelectedItems;

        this.GridView1.DataSource = tableSelectedItems;
        this.GridView1.DataBind();
    }
}
  • 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-01T02:42:50+00:00Added an answer on June 1, 2026 at 2:42 am

    You try to use the session with out first set a value to it / or the value to this session is not the same data type. I suggest to change to this code:

    DataTable tableSelectedItems;
    object otabSelItems  = Session["tableSelectedItems"];
    
    if(otabSelItems is DataTable)
       tableSelectedItems = (DataTable)otabSelItems;
    else
       tableSelectedItems = new DataTable();
    

    how ever you must have in mind that this variable will always be null if the user did not have set cookie enable, or the session have time outs.

    Why the session will be always null if the cookie is off ? because the session variables are associate with the cookie. If the user have cookies off, then in every reload a new cookie is created and the application can not know the previous cookie, and can not connect the previous session with this user and creates a new session.

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

Sidebar

Related Questions

After i publish my web.config i get null reference to all the code that
Can someone tell me why I get the null value for activationLink variable in
I get the warning childNodes is null or not an object' with different line
Can get all triples with value null in specific field? All people with date_of_birth
I get this message when compiling C++ on gcc 4.3 error: ‘NULL’ was not
I get the following error on firebug: this._processor is null and the error is
I'm trying to get a reference to cell and it appears null. If I'm
I get null pointer exception at line mService.start() when i try to bind to
In C# you can use as to convert a type or get null: Object
Why does refButton get null in the following JavaScript code? <html> <head> <title></title> <script

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.