Pregunta

The problem I am having is I keep catching a SQL exception. It's happening in my 'FlexList" class. I've ran the debugger and it is the line of code "Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader" that is breaking it and throwing the error. What am I doing wrong? Any kind of help offered would be EXTREMELY helpful.

Query statement going into the class encountering errors:

Public Class MainView
    Private _thatList As New GetClass
    Private _AddEditTerms As New Add_EditWindow
    Private _listManager As New listManager
    Private myQuery As String = _thatList.getAvaliableStatuses
    Private _flexList As New FlexList(myQuery)
'cut out the rest of the code
End Class

Class encountering the errors: Imports System.Data.SqlClient Imports System.Collections.Specialized

Public Class FlexList
    Public ReadOnly rows As New List(Of Dictionary(Of String, String))
    Public ReadOnly fields() As String

    Public Sub New(ByRef myQuery As String)
    Dim myConnection As SqlConnection = DataConnection.getProperityDBConnection()
    Dim row As Dictionary(Of String, String)

    Try
        myConnection.Open()
        Dim myCommand As New SqlCommand()
        myCommand.Connection = myConnection
        myCommand.CommandText = myQuery
        Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader

        If mySqlDataReader.HasRows Then
            Dim fieldCount = mySqlDataReader.FieldCount
            ReDim fields(fieldCount - 1)

            mySqlDataReader.Read()
            For i = 0 To fieldCount - 1
                fields(i) = mySqlDataReader.GetName(i)
            Next

            Do
                row = New Dictionary(Of String, String)
                For i = 0 To fieldCount - 1
                    row.Add(mySqlDataReader.GetName(i), IIf(mySqlDataReader.IsDBNull(i), "NULL", mySqlDataReader.GetValue(i).ToString))
                Next
                rows.Add(row)
                row = Nothing
            Loop While mySqlDataReader.Read()

        End If

    Catch ex As SqlException
        MsgBox("Database Error. Please contact you system administor")
    Catch ex As DataException
        MsgBox("Connection Error. Please contact The Delevelopement Team")
    Catch ex As Exception
        MsgBox("An Unknown Error has occured. Try again and report the error if it persists: " & vbCr & ex.ToString)
    Finally
        myConnection.Close()
    End Try

End Sub

End Class

The stored procedure I am using to test this: Imports System.Data.SqlClient

Public Class Get_Avaliable_Statuses
Public Function getAvailableStatuses()
    Dim connection As SqlConnection = DataConnection.getProperityDBConnection

    Dim insertCommand As New SqlCommand("dbo.ksp_Get_Available_Statuses", connection)
    insertCommand.CommandType = CommandType.StoredProcedure

    Try
        connection.Open()
        Dim count As Integer = insertCommand.ExecuteNonQuery()
        If count > 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Throw ex
    Finally
        connection.Close()
    End Try
End Function
End Class
¿Fue útil?

Solución

There is your problem - " [*] " use " * ". Remove square brackets

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top