Hey all I’ve been trying to find some resources on how to go about doing this but am unable to find anything since, really, i do not know really what it would be called in the first place.
The code in question is this:
Dim updateCmd As New SqlCommand(sql, myCONN)
There are a few places that i need to use that over that’s inside an IF THEN structure.
However, when i dim it outside the IF THEN structure then it tells me (inside the IF THEN) that its not declared.
So the full example is this:
If alreadyCreatedSettingsTable = False Then
Dim updateCmd As New SqlCommand(sql, myCONN)
updateCmd.Parameters.Add("@urlLink", SqlDbType.VarChar)
End If
rssItems = rssDoc.SelectNodes("rss/channel/item")
For i = 0 To rssItems.Count - 1
rssDetail = rssItems.Item(i).SelectSingleNode("link")
If rssDetail.Equals(Nothing) = False Then
rssLink = rssDetail.InnerText.Trim
updateCmd.Parameters("@urlLink").Value = rssLink
End If
etc....
The If alreadyCreatedSettingsTable = False Then dim works just fine for the updateCmd.Parameters.Add but when its inside the For i = 0 To rssItems.Count – 1 that is where it tells me its not decalired.
How can i use that anywhere no matter what and reDim it?
I’ve tried
Dim updateCmd
But i then do not know how to set As New SqlCommand(sql, myCONN) since its already been Dim’ed already?
Dimdeclares a variable.As Newis just syntactic sugar; the scope of the variable is inside thatIfblock. Declare it outside and initialize it inside:Also, please never write
= False; useNotinstead. Or, in the case ofrssDetail.Equals(Nothing) = False, you almost certainly wantrssDetail IsNot Nothinginstead.Finally, as written, this is pointless. If
alreadyCreatedSettingsTableisTrue, you’ll get aNullReferenceExceptionwhen you useupdateCmd.Parameters("@urlLink"). It seems to me that it’s something that should be declared outside of a loop, or at class level – though that’s just a guess.P.S. Why are you overwriting
updateCmd.Parameters("@urlLink")every time? That’s wasteful.