Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object reference not set to an instance of an object.
Posted

This error means that you are trying to use a null object.
Solution is to identify the line of code where the error was generated, then to debug your project and find out what object you forgot to init.
 
Share this answer
 
What object, where is your code.
We can't help you with this little information, you should be more specific.

In your code there is some object which is NULL, and you will be trying to read some property/value of that object.
This is the reason for the Object reference not set to an instance of an object.

-KR
 
Share this answer
 
Comments
Member 10651903 22-Mar-14 11:33am    
SqlConnection sc = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["hospitalmanagementdb"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from doct_medicine where medicine_id= " + Request.QueryString["id"].ToString();
cmd.Connection = sc;
sc.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
txtname.Text = sdr[1].ToString();
txtcname.Text = sdr[2].ToString();
txtrate.Text = sdr[3].ToString();
Krunal Rohit 22-Mar-14 11:36am    
It seems to be having NULL reference in Request.QueryString.
As your code looks to be promising.

So first check that , you're getting the value in query string or not.

-KR
PIEBALDconsult 22-Mar-14 12:18pm    
Or it could be in "ConnectionStrings["hospitalmanagementdb"].ToString());" -- if the config entry doesn't exist, and remember that XML is case-sensitive.
We can't solve this.
Particularly for that complete absence of information.

But you can...

What it is saying is that somewhere in your code (and the error message will specify exactly where) you have done the equivalent of this:
C#
SomeClass variable = null;
variable.Property = value;

Or
C#
SomeClass variable = null;
variable.Method();

And because the variable contains no object instance you can't access it's properties or methods.
It a bit like a car: if you don't specify which car ("This car", "That car", "My car", or "Your car") you can't access it's glove box (property) or drive it (method).

When you know which variable is null - and this may mean using the debugger if it isn't a simple line of code - then you can use the debugger to find out exactly why it's null.
But we can't - remember that we can't see your screen, access your HDD, or read your mind!

[edit]Typos - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Member 10651903 22-Mar-14 11:33am    
SqlConnection sc = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["hospitalmanagementdb"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from doct_medicine where medicine_id= " + Request.QueryString["id"].ToString();
cmd.Connection = sc;
sc.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
txtname.Text = sdr[1].ToString();
txtcname.Text = sdr[2].ToString();
txtrate.Text = sdr[3].ToString();
OriginalGriff 22-Mar-14 11:35am    
And?
Which line?
Which value?
Member 10651903 22-Mar-14 11:37am    
cmd.CommandText = "select * from doct_medicine where medicine_id= " + Request.QueryString["id"].ToString();



in this line
OriginalGriff 22-Mar-14 11:48am    
OK - there are two parts to the line, which could be generating the problem: the left and right side of the equals sign. Now, I can't access your system, but, the LHS looks ok, because you assign a value to cmd in the previous line. So it has to be the RHS:
"select * from doct_medicine where medicine_id= " + Request.QueryString["id"].ToString();
Well, a string constant can't be null, so it must be:
Request.QueryString["id"].ToString();
Request is unlikely to be null - unless you aren't in a web page, or you specifically overwrote it - so we can probably ignore that, and the same for the actual QueryString collection object.
So...we are left with the object returned from the query string collection.
And the most likely reason for that is there isn't an entry in URL that is a query string parameter called "id".
Which I can't look at!

So, look at the actual URL that addressed the page: did it have any query string? If it did, did it have an ID value?
Do you need to check if there is one and take appropriate action if there isn't? (HINT: Yes. You do, even if it isn't the problem this time).

And then you need to get rid of that code, before someone comes along and destroys your database via SQL Injection from your query string - and that is really, really, easy to do! So use Parameterized queries, and never, ever concatenate strings to form an SQL command!
Member 10651903 22-Mar-14 11:55am    
ok i will check it
This error occurs when you generally access a property of an object that is null.
Try debugging your source code. Check the line that throws an error.
 
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