2005: Warnings

F

Fredrik Melin

I have some issues with warnings in the new 2005 RTM

For sample, the following code " If Not rsData Is Nothing Then
rsData.Close()"
Gives a warning:
Warning 223 Variable 'rsData' is used before it has been assigned a value. A
null reference exception could result at runtime.

Which is "correct", but what is the correct way to do it?


Dim dbConn As New
SqlClient.SqlConnection(DSO.ConnectionObject.sConnection_String)
Dim cmSQL As New SqlClient.SqlCommand("", dbConn)
Dim rsData As SqlClient.SqlDataReader

Try
dbConn.Open()
cmSQL.Parameters.Add("@NAME", SqlDbType.VarChar).Value = Name
cmSQL.Parameters.Add("@PARENT_ID", SqlDbType.Int).Value =
Parent_ID

cmSQL.CommandText = "INSERT INTO WEBPAGE_GROUPS(NAME, PARENT_ID)
VALUES ( @NAME, @PARENT_ID)"

If cmSQL.ExecuteNonQuery() = 0 Then
Throw New Dacsa_Exceptions.Critical("Failure to add new
group.")
End If
Me.Populate()
Return True

Catch ex As Exception
DSO.Globals.ErrReport.RegisterError(ex, , Me)
Finally
dbConn.Close()
cmSQL.Dispose()
If Not rsData Is Nothing Then rsData.Close()

End Try



Also, This code gives the warning that sPort is a unused local variable
Which I dont understand why...



Dim sPort As String = cInvoice.DSO.Configuration("address label
printer port")

For nCount = 0 To sLabels.GetUpperBound(0)
If IO.File.Exists(Environ("TEMP") & "\Address.txt") Then
IO.File.Delete(Environ("TEMP") & "\Address.txt")


FileOpen(1, Environ("TEMP") & "\Address.txt",
OpenMode.Output)
PrintLine(1, "/#")
PrintLine(1, "FP")

PrintLine(1, "01" & sLabels(nCount))
PrintLine(1, "02" & cInvoice.DSO.Configuration("posten
kundnummer"))
PrintLine(1, "0425")

PrintLine(1, "05" &
cInvoice.Active_ShipTo_Address.sDelivery_Company)
PrintLine(1, "06" &
cInvoice.Active_ShipTo_Address.sStreetAddress)
PrintLine(1, "07" &
Replace(cInvoice.Active_ShipTo_Address.sDelivery_Zip_Code, " ", ""))
PrintLine(1, "08" &
cInvoice.Active_ShipTo_Address.sDelivery_City)

PrintLine(1, "09" & Format(Now, "yyyy-MM-dd"))

If cInvoice.DSO.Configuration("dd_role") = "9" And
cInvoice.nDD_Link_ID > 0 Then
PrintLine(1, "10" &
cInvoice.RemoteConnection.Configuration("company name"))
PrintLine(1, "11" &
cInvoice.RemoteConnection.Configuration("street address"))
PrintLine(1, "12" & "Retur: " &
cInvoice.RemoteInvoice.nOrder_ID)
PrintLine(1, "13" &
Replace(cInvoice.RemoteConnection.Configuration("zip code"), " ", ""))
PrintLine(1, "14" &
cInvoice.RemoteConnection.Configuration("city"))
Else
PrintLine(1, "10" & cInvoice.DSO.Configuration("company
name"))
PrintLine(1, "11" & cInvoice.DSO.Configuration("street
address"))
PrintLine(1, "12" & "Retur: " & cInvoice.nOrder_ID)
PrintLine(1, "13" &
Replace(cInvoice.DSO.Configuration("zip code"), " ", ""))
PrintLine(1, "14" & cInvoice.DSO.Configuration("city"))
End If

PrintLine(1, "/$")
FileClose(1)
IO.File.Copy(Environ("TEMP") & "\Address.txt", sPort)
Next
Return True
 
F

Fredrik Melin

I solved the Part 2 of this

Actually had a return statement before the code that always hit, therefor
the sPort isnt used...
 
K

Ken Tucker [MVP]

Hi,

I looked at your code and I dont ever see where you use rsData. A
datareader is a forward only way to retrieve records from a database. To
create a datareader you need to use a sqlcommand.executereader. Since you
are adding records to a database you would not get any records back so
executenonquery is the right way to add records. I would just delete the
lines of code with rsData.

Ken
------------
 
H

Herfried K. Wagner [MVP]

CT said:
You could do this:

Dim rsData As SqlClient.SqlDataReader = Nothing

I don't think you should do this in general. If you are doing that in
general it's maybe easier to disable the warning at all and skip explicit
initialization. In VB, as you know, variables get initialized
automatically, so there is absolutely no need for ' = Nothing' from a
technical point of view.

Just my 2 cents...
 
C

CT

Herfried, that is so true, but if you are assignign a value in a code block,
such as a Try...Catch block, I think it makes sense to "disable" the
warning. That's my $0.02 ;-)
 

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