Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an issue with filling a datagridview with a select statement.
Error I receive is 'cannot find table(0)'
What am I missing? My SQL query runs fine in SQL Manager. Nearly the same code works fine on another form. Only difference is I add columns on the other form.

VB
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click

    Dim sql As String
    Dim sql1 As String
    Dim sql2 As String
    Dim sql3 As String
    Dim datepicked As Date
    datepicked = DateTimePicker1.Text
    'Checkbox Check Loop
    If CheckBox1.CheckState = False And CheckBox2.CheckState = False And CheckBox3.CheckState = False Then
        MessageBox.Show("Please select a Shift")
        Exit Sub
    End If
    If CheckBox1.Checked = True And CheckBox2.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If
    If CheckBox2.Checked = True And CheckBox3.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If
    If CheckBox1.Checked = True And CheckBox3.Checked = True Then
        MessageBox.Show("Please select a One Shift Only")
        Exit Sub
    End If

    'Selection of Shift
    If CheckBox1.Checked = True Then
        sql1 = "SELECT * FROM Total_Prod WHERE SHIFT = 1"
    ElseIf CheckBox2.Checked = True Then
        sql1 = "SELECT * FROM Total_Prod WHERE SHIFT = 2"
    ElseIf CheckBox3.Checked = True Then
        sql1 = "SELECT * FROM Total_Prod WHERE SHIFT = 3"
    End If

    'Date selection string
    sql2 = " and DATE = "
    sql3 = "'"
    sql = sql1 + sql2 + sql3 + datepicked + sql3

    'proves/shows concat - Will Remove later
    MessageBox.Show(sql)

    'SQL Data connection
    Dim sqlCon1 As New SqlConnection("Data Source=SERVER1\DEV01;database=Production;uid=sa;pwd=passwordhere")
    Dim daSWC1 As New SqlDataAdapter(sql, sqlCon1)
    Dim SQLcmdBuilder1 As New SqlCommandBuilder(daSWC1)
    Dim ds1 As New DataSet

    'ERROR IS HERE!!
    DataGridView1.DataSource = ds1.Tables(0)
    daSWC1.Fill(ds1)

    DataGridView1.AllowUserToAddRows = False

End Sub
Posted

Look at your code:
VB
Dim ds1 As New DataSet
DataGridView1.DataSource = ds1.Tables(0)
daSWC1.Fill(ds1)
Perhaps if you swapped those two lines over?
VB
Dim ds1 As New DataSet
daSWC1.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
 
Share this answer
 
I had swapped it trying to fix an issue with my query, and forgot to put it back.
Thanks for the extra pair of eye to see that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900