Home > Back-end >  fill: SelectCommand connection property has not been initialized
fill: SelectCommand connection property has not been initialized

Time:11-04

    Dim SiparisOnayi As String
    Dim SiparisDurumu As String
    Dim SiparisIli As String
    Dim SiparisOdemeYontemi As String
    Dim SiparisKargoFirmasi As String
    Dim SiparisSatisKanali As String
    Dim a1, a2, a3, a4, a5, a6, a7, soncom As String

    If ComboBox1.Text = Nothing Then
        a1 = Nothing
    Else
        SiparisOnayi = ComboBox1.Text
        a1 = " and Siparis_Onay = SiparisOnayi"
    End If


    If ComboBox2.Text = Nothing Then
        a2 = Nothing
    Else
        SiparisDurumu = ComboBox2.Text
        a2 = " and Siparis_Durumu = SiparisDurumu "
    End If


    If ComboBox3.Text = Nothing Then
        a3 = Nothing
    Else
        SiparisIli = ComboBox3.Text
        a3 = " and Musteri_IL = SiparisIli "
    End If

    If ComboBox4.Text = Nothing Then
        a4 = Nothing
    Else
        a4 = " and Kullanici_Kodu = SiparisKullanicisi"
    End If


    If ComboBox5.Text = Nothing Then
        a5 = Nothing
    Else
        SiparisOdemeYontemi = ComboBox5.Text
        a5 = " and Odeme_Yontemi = SiparisOdemeYontemi"
    End If


    If ComboBox6.Text = Nothing Then
        a6 = Nothing
    Else
        SiparisKargoFirmasi = ComboBox6.Text
        a6 = " and Kargo_Adi = SiparisKargoFirmasi"
    End If


    If ComboBox7.Text = Nothing Then
        a7 = Nothing
    Else
        SiparisSatisKanali = ComboBox7.Text
        a7 = " and Satis_Kanali = SiparisSatisKanali"
    End If
    soncom = "SELECT * FROM `Siparisler` WHERE `Siparis_Tarihi` BETWEEN @d1 and @d2" & a1 & a2 & a3 & a4 & a5 & a6 & a7 & ", connection"



    Try

        Dim command As New MySqlCommand(soncom)
        command.Parameters.Add("@d1", MySqlDbType.DateTime).Value = DateTimePicker2.Value
        command.Parameters.Add("@d2", MySqlDbType.DateTime).Value = DateTimePicker3.Value
        Dim table As New DataTable
        Dim adapter As New MySqlDataAdapter(command)
        adapter.Fill(table)
        DataGridView1.DataSource = table
        Label12.Text = "Toplam " & table.Rows.Count & " Kayıt bulundu ve gösteriliyor."
        myconnection.close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

Error : fill: SelectCommand connection property has not been initialized

How can I fix my code?

CodePudding user response:

The & ", connection" at the end of this line looks wrong to me:

soncom = "SELECT ..." & a1 & ... a7 & ", connection"

But also, you need a connection object in addition to a command and an adapter, the way the WHERE clause is currently assembled would be extremely vulnerable to sql injection issues if it worked at all, but it won't because the combobox values are never actually interpolated into the final string, and the code is vulnerable to leaving the connection hanging open if an exception is thrown during the call to Fill().

This should fix all of those issues (once you fill in the connection string):

Dim table As New DataTable
Using connection As New MySqlConnection("connection string here")
Using command As New MySqlCommand("", connection)
Using adapter As New MySqlDataAdapter(command)
    Dim sql As String = "SELECT * FROM `Siparisler` WHERE `Siparis_Tarihi` BETWEEN @d1 and @d2"
    If Not string.IsNullOrWhitespace(ComboBox1.Text) Then
        sql  = " and Siparis_Onay = @SiparisOnayi"
        command.Parameters.AddWithValue("@SiparisOnayi", ComboBox1.Text)
    End if

    If Not string.IsNullOrWhitespace(ComboBox2.Text) Then
        sql  = " and Siparis_Durumu = @SiparisDurumu"
        command.Parameters.AddWithValue("@SiparisDurumu", ComboBox2.Text)
    End If

    If  Not string.IsNullOrWhitespace(ComboBox3.Text) Then
        sql  = " and Musteri_IL = @SiparisIli"
        command.Parameters.AddWithValue("@SiparisIli", ComboBox3.Text)
    End If

    If  Not string.IsNullOrWhitespace(ComboBox4.Text) Then
        sql  " and Kullanici_Kodu = @SiparisKullanicisi"
        command.Parameters.AddWithValue("@SiparisKullanicisi", ComboBox4.Text)
    End If


    If  Not string.IsNullOrWhitespace(ComboBox5.Text) Then
        sql  = " and Odeme_Yontemi = @SiparisOdemeYontemi"
        command.Parameters.AddWithValue("@SiparisOdemeYontemi", ComboBox5.Text)
    End If


    If  Not string.IsNullOrWhitespace(ComboBox6.Text) Then
        sql  = " and Kargo_Adi = @SiparisKargoFirmasi"
        command.Parameters.AddWithValue("@SiparisKargoFirmasi", ComboBox6.Text)
    End If


    If  Not string.IsNullOrWhitespace(ComboBox7.Text) Then
        sql  = " and Satis_Kanali = @SiparisSatisKanali"
        command.Parameters.AddWithValue("@SiparisSatisKanali", ComboBox7.Text)
    End If

    command.CommandText = sql
    command.Parameters.Add("@d1", MySqlDbType.DateTime).Value = DateTimePicker2.Value
    command.Parameters.Add("@d2", MySqlDbType.DateTime).Value = DateTimePicker3.Value
    Try
        adapter.Fill(table)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Using
End Using
End Using

DataGridView1.DataSource = table
Label12.Text = $"Toplam {table.Rows.Count} Kayıt bulundu ve gösteriliyor."
  • Related