Question:

ASP.Net - Not Set To Instance of Object - Error when retrieving string from Query URL

by  |  earlier

0 LIKES UnLike

In Visual Studio 2005, I get the error when trying to pull a query value from previous page:

"System.NullReferenceException: Object reference not set to an instance of an object."

Which refers to the line "string itemID = Request.QueryString["itemID"].ToString()...

Here is some code:

string itemID = Request.QueryString["itemID"].ToString()...

SqlConnection dbConnect = new SqlConnection(ConfigurationManager.Conne...

SqlCommand getProduct = new SqlCommand("SELECT * FROM PRODUCTS WHERE itemID=" itemID , dbConnect);

//Open the connection.

dbConnect.Open();

SqlDataReader myData = getProduct.ExecuteReader();

if (myData.HasRows)

{

//Advance the first records.

myData.Read();

 Tags:

   Report

3 ANSWERS


  1. Are you sure there's a querystring value for "itemID"? Try testing for null first before casting and assigning it to your itemID string:

    string itemID = "";

    if (Request.QueryString["itemID"] != null)

    {

           itemID = Request.QueryString["itemID"].ToString()...

    }

    if (itemID != "")

    {

         //Do your SQL here

    }


  2. Error CS0001 compiler internal error

    Memory overflow error CS0003

    Error CS0004 upgraded to the wrong warning

    Error CS0005 compiler option should be followed by the correct parameters

    CS0006 error can not find the dynamic link metadata file

    Error CS0007. Net environment initialization error

    CS0008 error in the reading from the document meta-data errors

    CS0009 error can not open the document metadata

    CS0010 wrong name and type of space can not name the same name

    CS0011 cited the wrong category could not find base class

    CS0012 not invoke the wrong type of definition

    CS0013 errors in the document to the preservation of metadata An unknown error occurred

    CS0014 wrong file name refers to less than

    The wrong type of long CS0015

    CS0016 output document can not be wrong

    Error CS0017 output file a number of importers

    CS0019 binocular operator error does not apply to the designated number of operations

    0 errors CS0020 divisor

    http://www.Fix-pc-Master.com

  3. The error is probably referring to the QueryString["itemID"] clause.

    Do you actually have any itemID in the querystring...?

    eg, http://mysite.com/page1.aspx?itemID=4

    You should probably add a check to ensure the value is there before trying to pass it.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.