Home All Groups Group Topic Archive Search About

specified cast is not valid

Author
6 Jan 2006 8:40 AM
jonefer
I know the connection works, but maybe it's the fact that I am referrring to
a View for this DataAdapter?

Does anyone know what would cause the following code to generate a:
"Specified Cast is not valid"?

        Dim sMemberCon As String = ConfigurationSettings.AppSettings("SQLConn")
                    cnSQL.ConnectionString = sMemberCon
            cnSQL.Open()

                    'daMembers_SQL.SelectCommand.CommandType =
CommandType.StoredProcedure

                    Dim cmdFillMemberSelect As New
SqlCommand("qMemberSelect", cnSQL)
                    cmdFillMemberSelect.CommandType =
CommandType.StoredProcedure

                    Dim pMRN As New SqlParameter("@MRN", SqlDbType.Int)
                    Dim pMemName As New SqlParameter("@MemNAME",
SqlDbType.NVarChar, 40)
                    Dim pDOB As New SqlParameter("@DOB", SqlDbType.DateTime)
                    Dim pSSN As New SqlParameter("@SSN", SqlDbType.NVarChar,
9)
                    Dim pSEX As New SqlParameter("@SEX", SqlDbType.NVarChar,
1)




                    'Add the parameters only if the textbox value was filled
in
                    If Session("ssMRN") <> 0 Then
                        pMRN = Session("ssMRN")
                        cmdFillMemberSelect.Parameters.Add(pMRN)
                    End If

                    If Len(Session("ssNAME")) <> 0 Then
                        pMemName = Session("ssNAME")
                        cmdFillMemberSelect.Parameters.Add(pMemName)
                    End If

                    If Len(Session("ssDOB")) <> 0 Then
                        pDOB = Session("ssDOB")
                        cmdFillMemberSelect.Parameters.Add(pDOB)
                    End If

                    If Len(Session("ssSSN")) <> 0 Then
                        pSSN = Session("ssSSN")
                        cmdFillMemberSelect.Parameters.Add(pSSN)
                    End If

                    If Len(Session("ssSEX")) <> 0 Then
                        pSEX = Session("ssSEX")
                        cmdFillMemberSelect.Parameters.Add(pSEX)
                    End If

                    'Fill the datatable with the filtered selection
                    'daMembers_SQL.Fill(dsMembers, "qMemberSelect")
                    cmdFillMemberSelect.ExecuteNonQuery()

                    daMemberResults.Fill(DsMemberResults1, "vMemberResults")
                    DGMembers.DataMember = "vMemberResults"
                    DGMembers.DataSource =
DsMemberResults1.Tables("vMemberResults").DefaultView

Author
6 Jan 2006 9:37 AM
Cor Ligthert [MVP]
Jonefer,

I have the idea that you get a better response that you tell what code is
throwing this error.

Cor
Author
12 Jan 2006 5:57 PM
Milosz Skalecki
Lo,

I almost sure the exception is thrown in one of these statments,
> If Session("ssMRN") <> 0 Then
>        pMRN = Session("ssMRN")
>        cmdFillMemberSelect.Parameters.Add(pMRN)
> End If

Correct to:

if Session("ssMRN") <> 0 then
     pMRN.Value = Session("ssMRN")
     cmdFillMemberSelect.Parameters.Add(pMRN)
end if

Milosz Skalecki
MCP, MCAD

Show quote
"jonefer" wrote:

> I know the connection works, but maybe it's the fact that I am referrring to
> a View for this DataAdapter?
>
> Does anyone know what would cause the following code to generate a:
> "Specified Cast is not valid"?
>
>         Dim sMemberCon As String = ConfigurationSettings.AppSettings("SQLConn")
>                     cnSQL.ConnectionString = sMemberCon
>             cnSQL.Open()
>
>                     'daMembers_SQL.SelectCommand.CommandType =
> CommandType.StoredProcedure
>
>                     Dim cmdFillMemberSelect As New
> SqlCommand("qMemberSelect", cnSQL)
>                     cmdFillMemberSelect.CommandType =
> CommandType.StoredProcedure
>
>                     Dim pMRN As New SqlParameter("@MRN", SqlDbType.Int)
>                     Dim pMemName As New SqlParameter("@MemNAME",
> SqlDbType.NVarChar, 40)
>                     Dim pDOB As New SqlParameter("@DOB", SqlDbType.DateTime)
>                     Dim pSSN As New SqlParameter("@SSN", SqlDbType.NVarChar,
> 9)
>                     Dim pSEX As New SqlParameter("@SEX", SqlDbType.NVarChar,
> 1)
>
>
>                    
>
>                     'Add the parameters only if the textbox value was filled
> in
>                     If Session("ssMRN") <> 0 Then
>                         pMRN = Session("ssMRN")
>                         cmdFillMemberSelect.Parameters.Add(pMRN)
>                     End If
>
>                     If Len(Session("ssNAME")) <> 0 Then
>                         pMemName = Session("ssNAME")
>                         cmdFillMemberSelect.Parameters.Add(pMemName)
>                     End If
>
>                     If Len(Session("ssDOB")) <> 0 Then
>                         pDOB = Session("ssDOB")
>                         cmdFillMemberSelect.Parameters.Add(pDOB)
>                     End If
>
>                     If Len(Session("ssSSN")) <> 0 Then
>                         pSSN = Session("ssSSN")
>                         cmdFillMemberSelect.Parameters.Add(pSSN)
>                     End If
>
>                     If Len(Session("ssSEX")) <> 0 Then
>                         pSEX = Session("ssSEX")
>                         cmdFillMemberSelect.Parameters.Add(pSEX)
>                     End If
>
>                     'Fill the datatable with the filtered selection
>                     'daMembers_SQL.Fill(dsMembers, "qMemberSelect")
>                     cmdFillMemberSelect.ExecuteNonQuery()
>
>                     daMemberResults.Fill(DsMemberResults1, "vMemberResults")
>                     DGMembers.DataMember = "vMemberResults"
>                     DGMembers.DataSource =
> DsMemberResults1.Tables("vMemberResults").DefaultView

AddThis Social Bookmark Button