SqlCeParameter Issues NullReferenceExeception

B

Brett Miller

Hi,

I have created a SQLServerCE database table with 22 fields, I have a
parameterised INSERT Query, that looks something like this

CDisplay.CommandText = "INSERT INTO CDisplay ( " &
_"Status,LstTiPNo,CC,LastVisitDate
,CUniq,Indics,CallKey,SeqNo,TelNo,TelExtn," &
_"NIndic,BPO,D1,LegalName,SBLKey,Comment,Contact1,StNo,DspAddr,Descr1,Descr2
,PrvComp1) " & _"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

The Parameters are setup like this...

CDisplay.Parameters.Add(New SqlCeParameter("Status", SqlDbType.Int))

CDisplay.Parameters(0).IsNullable = True

This is done for each field...

I read records from a BinaryStream, and populate the parameters with the
respective values, sometime I do not get values for every field before I
reach the next record.

When I call

CDisplay.ExecuteNonQuery()

I get a NullReferenceExeception from the topmost Parameter that I did'nt
have a value for...

Why is this happening???? isn't IsNullable enough???

Thanks 4 u're Help.

BM
 
W

William Ryan eMVP

If you are getting a NullReferenceException, that's different from a Null
value in the sense of IsNullable. You can have this code:
string s;
--CDisplay.Parameters(0).IsNullable = True
but try CDisplay.Parameters(0).Value = s.Length;
and you'll get a NullReferenceException b/c s is nothing at this point.
That's different from a DBNull. I'm not sure what you mean by TopMost
Parameter but you are probably referencing something for one of those params
that's nothing. All that the isNullable does is allow you to not specify a
value for that parameter but that's totally different than trying to assign
it something that is nothing.

I'd trap the Null ReferenceException (probably a good idea in general) and
see what line is causing it. You'll need to make sure it's initialized or
you'll need to take preemptive action b/c no matter what you do, if you try
setting anything to s.Length when s isn't initialized, you are going to get
that exception. The same holds for any other object.

HTH,

Bill
--

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
 
Top