Hi,
Error: Object reference not set to an instance of an object.
referring to:
command.Parameters.AddWithValue("@A", Z.Text);
what causes this?
Thanks,
For Answers, see/post comments
Object not set
Subscribe to:
Post Comments (Atom)
For Answers, see/post comments
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:
command is not initialized.
remember, each trip back to the server from the browser requires a new inititialization.
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.
Hi thanks for your reply,
How do I initialize it?
Thanks again
Hi JOhn,
Initialize it at class level before pageload method......
Thanks,
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);
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.
Thanks think I have it now!
This problem occurs if the object called does not exist
or if u use find control it would have returned null
Post a Comment