Specified cast is not valid

G

Guest

I am experiencing the following error:

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 143: Dim drChange As Changes.tblChangesRow
Line 144: Dim iCurrentRow As Integer
Line 145: drChange = dsChange.Tables(0).Rows(iCurrentRow)
Line 146: If drChange.IsChangeDescriptionNull Then
Line 147: Else

Source File: c:\inetpub\wwwroot\ITApplications\UpdateChangeRequest.aspx.vb
Line: 145

My code that I am using is:

Dim dsChange As New DataSet
Dim cmdChange As New SqlCommand

Dim sSelect As String = "SELECT ChangeID, ChangeDescription, ChangeRisk,
ChangePlan, WhoAffected, EstCompDate, ActCompDate, AssignedTo, Notes, Status
FROM tblChanges WHERE ChangeID = " & Integer.Parse(Request.QueryString("ID"))

cmdChange.CommandText = sSelect
cmdChange.Connection = SqlConnection1

Dim daChange As New SqlDataAdapter
daChange.SelectCommand = cmdChange
daChange.MissingSchemaAction = MissingSchemaAction.AddWithKey
daChange.Fill(dsChange)

Dim chg As New ChangesDP
Dim drChange As Changes.tblChangesRow
Dim iCurrentRow As Integer
drChange = dsChange.Tables(0).Rows(iCurrentRow)

Why is this an invalid cast? Any help is appreciated.
 
M

Mike Labosh

Exception Details: System.InvalidCastException: Specified cast is not
valid.

Source Error:

Line 143: Dim drChange As Changes.tblChangesRow
Line 144: Dim iCurrentRow As Integer
Line 145: drChange = dsChange.Tables(0).Rows(iCurrentRow)
Line 146: If drChange.IsChangeDescriptionNull Then
Line 147: Else

You have to assign something to iCurrentRow. If you Dim any kind of numeric
variable without assigning it, it's default value is zero. If the DataTable
does not have any DataRows in it, then there is no .Rows(0) available, so
..Rows(0) returns Nothing, and I don't think you can assign Nothing to a
DataRow that you just Dim'd on line 143.
--
Peace & happy computing,

Mike Labosh, MCSD

"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane
 
G

Guest

I have used this technique in several other instances that works fine. I
have used the select statement in query analyzer to make sure that it does
return 1 record. I am still unclear as to why this is happening.
 

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

Members online

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top