For Answers, see/post comments

Object not set

Hi,
Error: Object reference not set to an instance of an object.
referring to:
command.Parameters.AddWithValue("@A", Z.Text);
what causes this?
Thanks,

8 comments:

Anonymous said...

command is not initialized.



remember, each trip back to the server from the browser requires a new inititialization.

Anonymous said...

You usually get that error if you're trying to use an object that's set to null. In your case it could either be your (textbox?) Z or your command object. Did you use FindControl to get a reference to Z maybe? It's difficult to tell the exact cause of your problem without seeing how Z and command are declared.

Anonymous said...

Hi thanks for your reply,

How do I initialize it?

Thanks again

Anonymous said...

Hi JOhn,

Initialize it at class level before pageload method......



Thanks,

Anonymous said...

Hi Ramblor. This might help:


Label Z = (Label)FormView1.FindControl("Label13");

command.CommandText = "IF EXISTS(SELECT A FROM table WHERE A = @A) UPDATE table SET number = number + 1 WHERE proid = @proid ELSE INSERT INTO table (proid, number) VALUES(@A, 1)";


command.Parameters.AddWithValue("@A", Z.Text);

Anonymous said...

It could be one of two things. As another posted said (sorry, can't remember who) you might not have initialized your command object, e.g.

SqlCommand command = new SqlCommand();
command.CommandText = "Your SQL Query";

or as I suggested, Z is set to NULL as it can't be found in the FormView. I've not used the FormView much but I assume that it only looks for controls in the template for the CurrentMode. Try debugging and check that FormView1.FindControl("Label13") is actually returning a reference to a control instead of NULL.

Anonymous said...

Thanks think I have it now!

Anonymous said...

This problem occurs if the object called does not exist

or if u use find control it would have returned null