Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 17th, 2013, 02:05 PM
Authorized User
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default Object reference not set to an instance of an object.

Hello guys and good day, I need your help.

This is my net code, I've this error in object dr, why?
Can you help me? Thank you in advance.
PHP Code:
    protected void BindEmployeeDetails()
    {
        
myConnectionString.Open();

        
httpCookie CustInfo Request.Cookies["id"]; 

        
SQL "SELECT * FROM ";
        
SQL SQL " tbl ";
        
SQL SQL " WHERE 1 ";
        
        if (
CustInfo != null)
        {
            
SQL SQL " AND id = " Request.Cookies["id"].ToString() + " ;";
        }

        
OdbcCommand cmd = new OdbcCommand(SQLmyConnectionString);
        
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
        
DataSet ds = new DataSet();
        
da.Fill(ds);
        
OdbcDataReader dr null;


        if (
dr.Read())
        {
            if (
ds.Tables[0].Rows.Count 0)
            {
                
gvDetails.DataSource ds;
                
gvDetails.DataBind();
            }
            else
            {
                
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                
gvDetails.DataSource ds;
                
gvDetails.DataBind();
                
int columncount gvDetails.Rows[0].Cells.Count;
                
gvDetails.Rows[0].Cells.Clear();
                
gvDetails.Rows[0].Cells.Add(new TableCell());
                
gvDetails.Rows[0].Cells[0].ColumnSpan columncount;
                
gvDetails.Rows[0].Cells[0].Text "No record found";
            }
        }
        else
        {
            
Response.Write("No user");
        }

        
myConnectionString.Close();
    }


Exception DetailsSystem.NullReferenceExceptionObject reference not set to an instance of an object.

Source Error:


Line 43:         OdbcDataReader dr null;
Line 44:
Line 45:         if (dr.Read())
Line 46:         {
Line 47:             if (ds.Tables[0].Rows.Count 0
 
Old April 17th, 2013, 02:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Isn't that obvious from the code?

OdbcDataReader dr = null;


if (
dr.Read())


You assign a variable the value null and then try to call Read on it. Obviously, the returns a null reference exception. I think you can remove all code related to the reader as you're using a dataset.

Also, Google for "SQL Injection" and read a bit into the topic. If you use the current code as is, it's easy to blow your database away or in the worst case take over your machine.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
cms9651 (April 17th, 2013)
 
Old April 17th, 2013, 03:00 PM
Authorized User
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
Hi there,

Isn't that obvious from the code?

OdbcDataReader dr = null;


if (
dr.Read())


You assign a variable the value null and then try to call Read on it. Obviously, the returns a null reference exception. I think you can remove all code related to the reader as you're using a dataset.

Also, Google for "SQL Injection" and read a bit into the topic. If you use the current code as is, it's easy to blow your database away or in the worst case take over your machine.

Cheers,

Imar
thank you for help.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 14 - pg 517 - Item 9 (Object reference not set to an instance of an object. ) jkoyle BOOK: Beginning ASP.NET 4 : in C# and VB 5 August 25th, 2011 08:50 AM
Getting Error: Object reference not set to an instance of an object. oruckenan BOOK: Beginning ASP.NET 4 : in C# and VB 2 August 23rd, 2011 02:18 PM
Error - Object reference not set to an instance of an object Phil Critchley ASP.NET 4 General Discussion 2 April 30th, 2011 05:18 AM
Object reference not set to an instance of an object. Michael YoeH ASP.NET 3.5 Professionals 5 August 23rd, 2009 02:07 PM
Chap 13 pg448: Object reference not set to an instance of an object tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 15 August 8th, 2009 06:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.