Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting error like this.

NullReference Exception was unhandled by user code.<br />
object reference not set to an instance of an object


my code is:

C#
System.Data.SqlClient.SqlDataReader rdr = null;
conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["smartwaydbConnectionString"].ConnectionString);

           if (context.Request.QueryString["pageType"] == "tour")
           {
               if (context.Request.QueryString["type"] == "Type1")
               {
                   selcmd = new System.Data.SqlClient.SqlCommand("select * from tour where tourid='" + context.Request.QueryString["tourid"] + "'", conn);
               }
               else if (context.Request.QueryString["type"] == "Type2")
               {

                   selcmd = new System.Data.SqlClient.SqlCommand("select * from tour where tourid='" + context.Request.QueryString["tourid"] + "'", conn);
               }
               else if (context.Request.QueryString["type"] == "Type3")
               {


                   selcmd = new System.Data.SqlClient.SqlCommand("select * from tour where popularimage IS NOT NULL and tourid = '" + context.Request.QueryString["tourid"] + "'", conn);

               }

MIDL
conn.Open();
           rdr = selcmd.ExecuteReader();


i am getting the above error here rdr=selcmd.ExecuteReader();

thanks in advance.......
Posted
Updated 10-Mar-11 6:33am
v5
Comments
Sergey Alexandrovich Kryukov 10-Mar-11 4:41am    
In what line of code it happens?
--SA
Dalek Dave 10-Mar-11 4:43am    
Edited for Readability and Code Block.
Richard MacCutchan 10-Mar-11 4:47am    
What is the value of context.Request.QueryString["type"]? If it does not match one of the 3 options then presumably selcmd will not have any value.
leelavathikuna 10-Mar-11 4:53am    
the code is with in the try catch blocks only.
and i have declared the rdr also
Richard MacCutchan 10-Mar-11 5:54am    
This information does not mean anything; it would be more helpful (to you) if you answered my question.


Have you declared rdr?

Alternatively throw an exception, Try-Catch should do it.
 
Share this answer
 
Comments
Richard MacCutchan 10-Mar-11 5:58am    
The problem occurs because selcmd is null. If rdr was not declared then he would get a compiler error rather than a run time exception.
Dalek Dave 10-Mar-11 6:13am    
I do not know why that didn't occur to me!
Richard MacCutchan 10-Mar-11 9:17am    
Probably the same reason I miss stuff that others then point out to me - senility, old chap! :(
When you get an error like that, always step-through your code to see what is going on. Put a breakpoint before the error occurs and then step-through it.

In this case, I think you would find that either context.Request.QueryString["pageType"] does not equal "tour" or that context.Request.QueryString["type"] does not equal "Type1", "Type2", or "Type3" which means that selcmd is never being set.

So, you would then want to figure out what QueryString["type"] or QueryString["pageType"] is being set to and go from there.

Also, as an FYI to how you're creating your commands, I would have a look here: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx[^]. You want to avoid setting a command using a straight variable as you have here:
"select * from tour where tourid='" + context.Request.QueryString["tourid"] + "'".
 
Share this answer
 
v2
Your code looks like a Script-Kiddies dream.. ;)
Please (as stated in answers before) NEVER use QueryString variables directly in a SQL Query!

About your Problem, I don't see why the Exception occures, but it seems that some parts of your source is missing.

Anyway, I tried to clean the code up a little bit, could you try it this way (perhaps you have to modify my source a bit according your surroundings.

public IList<Tour> GetTourData(HttpContext context)
{
	// Init output
	var tourOutput = new List<Tour>();

	// Get all input variables out of the QueryString
	string pageType = context.Request.QueryString["pageType"];
	string type = context.Request.QueryString["type"];
	string tourId = context.Request.QueryString["tourid"];

	// Get the connection string
	string connectionString = ConfigurationManager.ConnectionStrings["smartwaydbConnectionString"].ConnectionString;

	// Initialize the SQL Connection and define the SqlCommand and SqlDataReader
	SqlConnection conn = new SqlConnection(connectionString);
	SqlCommand selcmd = null;
	SqlDataReader rdr = null;
			
	if (pageType == "tour")
	{
		// Build the SqlCommand according the type (use sql variables for the input! => SQL Injection!)
		switch (type)
		{
			case "Type1":
			case "Type2":
				selcmd = new SqlCommand("select * from tour where tourid = @tourid", conn);
				break;
			case "Type3":
				selcmd = new SqlCommand("select * from tour where popularimage IS NOT NULL and tourid = @tourid", conn);
				break;
		}
	}

	// Proceed if the command was correctly assigned
	if (selcmd != null)
	{
		// Add the @tourid parameter
		selcmd.Parameters.Add("@tourid", SqlDbType.Int).Value = Convert.ToInt32(tourId);

		conn.Open();

		using (rdr = selcmd.ExecuteReader())
		{
			if (rdr.HasRows)
			{
				while (rdr.Read())
				{
					// Create a new Tour object
					var tour = new Tour();

					// Add all data from the SqlDataReader rdr
					// ...

					// Add the new Tour object to the output list (it is returning more than one result, right?)
					tourOutput.Add(tour);
				}
			}
		}

		conn.Close();
	}

	return tourOutput;
}


As explanation:
The Tour Class is a class which holds the tour data you get from the reader. If you want to process it in a DataTable you have to change the object-reading and the output parameter to make this working.

Hope this helps to get you forward.

And once again, PLEASE NEVER use QueryString Parameters directly in a SQL Statement!! Encapsulate them at least in a Sql Parameter attached to the query (better use a Stored Procedure directly on the SQL Server).

Best regards and have a nice day,
Stops
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900