Error: Value cannot be null. Name of parameter: datatable. VB.NET

Asked

Viewed 1,058 times

2

I’m trying to make an appointment at the bank and display it on a datagrid, I have not so much experience, I do technical course and is my first question here, the code I am using is this:

Public Sub PreencheDataGrid(ByRef x As DataGridView, ByVal op1 As Integer, ByVal op2 As Integer)
    Dim SDA As New MySqlDataAdapter
    Dim bSource As New BindingSource

    conectaBanco()

    Dim query As String

    query = "SELECT * FROM vw_tb_clientes"

    objcmd = New MySqlCommand(query, con)
    SDA.SelectCommand = objcmd
    SDA.Fill(dbDataSet)
    bSource.DataSource = dbDataSet

    'Deixa o DataGridView limpo
    x.DataSource = Nothing

    x.DataSource = bSource
    SDA.Update(dbDataSet)

    fechaBanco()
End Sub

From what I understand with mine debug, the error is in the code SDA.Fill(dbDataSet), but I don’t know what’s happening since I have another datagridview with almost the same code and functional, someone could help me?

1 answer

2


Failed to specify the table in your DataSet.
See the implementations below to fix the issue:

Public Sub PreencheDataGrid(ByRef x As DataGridView, ByVal op1 As Integer, ByVal op2 As Integer)
    Dim SDA As New MySqlDataAdapter
    Dim bSource As New BindingSource

    conectaBanco()

    Dim query As String

    query = "SELECT * FROM vw_tb_clientes"

    objcmd = New MySqlCommand(query, con)
    SDA.SelectCommand = objcmd

    'Declarei aqui para mostrar o uso do parâmetro
    Dim dbDataSetAs New DataSet("Tab") 'Tab será o nome do nosso dataset e da nossa tabela
    '---
    SDA.Fill(dbDataSet, "Tab") 'Preenche os dados na Tab
    bSource.DataSource = dbDataSet
    bSource.DataMember = "Tab" 'Indica que é para puxar da Tab os dados

    'Deixa o DataGridView limpo
    x.DataSource = Nothing

    x.DataSource = bSource
    SDA.Update(dbDataSet, "Tab")

    fechaBanco()
End Sub
  • I forgot to put the variable creations, they are outside the sub: Public objcmd As Mysqlcommand Public dbDataSet As Datatable That is, dbDataSet is a Datatable

  • If using the DataTable, then missed the New. Note that in my example, with the DataSet also needs.

  • Now something else has happened, it is normally in datagridview, but the columns "Business Name" and "Fantasy Name" are bringing all the lines with the contents of the column title, I’m using the same code to filter only changes the query, that when you filter by name it looks like this: query = "SELECT 'Business Name', 'Name Fantasia', UF, Municipality, Neighborhood, Street, Complement, CEP, Branch FROM vw_tb_clients"

  • If you use quotes, you will "create" a column with the value in quotes. First put the column name, then the description.

  • SELECT nome_empresarial 'Nome Empresarial', nome_fantasia 'Nome Fantasia', UF, Municipio, Bairro, Logradouro, Complemento, CEP, Filial FROM vw_tb_clientes

  • this I would have to modify in view?

  • It worked @Matheusmenegatte?

  • Now it’s worked out, thank you very much.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.