Object null reference

R

Ricardo Luceac

I'm geting a object null reference in this method

SqlConnection cnn = new
SqlConnection("server=(local);database=varired;trusted_connection=true")
;
SqlCommand cmm = new SqlCommand();
cmm.Connection=cnn;
cnn.Open();
cmm.CommandText="Delete from estados where
id_estado="+Convert.ToInt32(lstEsts.SelectedItem.Value);

It gives the error in the last line, I don't know what's happening...
 
R

rk

try to get the actual command text during the runtime (thru a
debugger). My guess is that lstEsts.SelectedItem.Value might be
returning the null reference you are getting. So, you might want to
assign the value to a variable and see what you get there.

Good luck!

--rk
 
G

Guest

Hi Ricardo,

My guess it that the lstEsts contol is supplying the Convert.ToInt32 with a
null value and is throwing a NullReferenceException. If this is what is
happening just make sure that the lstEsts is supplying a value before you
call the Convert method on it. If this is not correct please reply to this
post with more detail of the issue.

I hope this helps.
-----------------------------
 
G

Guest

Hi again!

Looking at this more closely you will see another problem when you do get
the control to send you a good value. You will need to use a string or you
will probably get a format exception when you try to concatenate an integer
with a string. I don't know what you are passing the Convert method so
either you will have to call ToString() on it or just forgo the Convert
method completely.

I hope this helps.
 
R

Ricardo Luceac

I think it's not returning the selected item... How can this happen???

[]s...
 
G

Guest

Hi Ricardo,

It is hard to tell without seeing your code. I can only speculate to what
the cause may be. Can you post the code or at least some sample code so the
group may diagnose the problem better?

One quick suggestion. I would check to make sure that the control exists and
is bound before you call its SelectedItem.Value property. Other than that I
would like to see the code.

I hope this helps.
-------------------------
 
R

Ricardo Luceac

I find the problem.. In the page load event I put the code to populate
the listbox, but forgot the IsPostBack...
So everytime the listbox was populated again, and so no item is
selected...

Thanks to all of you...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top