How to fix "SelectedCommand property has not been initialized . . " error?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dougancil
    Contributor
    • Apr 2010
    • 347

    How to fix "SelectedCommand property has not been initialized . . " error?

    I have the following code:

    Code:
    Public Class Payrollfinal
        Private Sub Payrollfinal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            exportfileButton.Enabled = False
            Dim oCmd As System.Data.SqlClient.SqlCommand
            Dim oDr As System.Data.SqlClient.SqlDataReader
            oCmd = New System.Data.SqlClient.SqlCommand
            Dim _CMD As SqlCommand = New SqlCommand
            Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
            Dim ds As New DataSet
            Try
                adapter.Fill(ds)
                With oCmd
                    .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
                    .Connection.Open()
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.AddWithValue("@payperiodstartdate", payperiodstartdate)
                    .Parameters.AddWithValue("@payperiodenddate", payperiodenddate)
                    .CommandText = "sp_allsum"
                    oDr = .ExecuteReader()
                    oCmd.Connection.Close()
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
            Try
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                oCmd.Connection.Close()
            End Try
            exportfileButton.Enabled = True
        End Sub
    and when I press the button that this event is connected to, I get the error:

    The SelectCommand Property has not been initialized before calling 'Fill'

    and I'm thinking that because on my other page, where I'm trying to run multiple queries with this code;
    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Main.payrollButton.Enabled = True
            Main.exceptionsButton.Enabled = False
            Dim oCmd As System.Data.SqlClient.SqlCommand
            oCmd = New System.Data.SqlClient.SqlCommand
            Dim adapter As System.Data.SqlClient.SqlDataAdapter = New System.Data.SqlClient.SqlDataAdapter
            Dim ds As New DataSet
            Try
                With oCmd
                    .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
                    .Connection.Open()
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "exec sp_opsum @payperiodstartdate = " + payperiodstartdate + ",@payperiodenddate=" + payperiodenddate + ";" + _
    "exec sp_opintcheck @payperiodstartdate = " + payperiodstartdate + ",@payperiodenddate=" + payperiodenddate + ";" + _
    "exec sp_opexceptsum @payperiodstartdate = " + payperiodstartdate + ",@payperiodenddate=" + payperiodenddate + ";" + _
    "exec sp_empsum @payperiodstartdate = " + payperiodstartdate + ",@payperiodenddate=" + payperiodenddate + ";" + _
    "exec sp_empexceptsum @payperiodstartdate = " + payperiodstartdate + ",@payperiodenddate=" + payperiodenddate + ";"
                    oCmd.Connection.Close()
                End With
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
            Try
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                oCmd.Connection.Close()
            End Try
            Me.Close()
        End Sub
Working...