Datagridview填充错误 - 找不到表(0) [英] Datagridview Fill error - cannot find table(0)

查看:87
本文介绍了Datagridview填充错误 - 找不到表(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用select语句填充datagridview时遇到问题。

我收到的错误是'找不到表(0)'

我缺少什么?我的SQL查询在SQL Manager中运行良好。几乎相同的代码在另一种形式上工作正常。唯一的区别是我在另一个表单上添加了列。



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.

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

推荐答案

查看你的代码:

Look at your code:
Dim ds1 As New DataSet
DataGridView1.DataSource = ds1.Tables(0)
daSWC1.Fill(ds1)

也许你换了这两行?

Perhaps if you swapped those two lines over?

Dim ds1 As New DataSet
daSWC1.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)


我换了它试图解决我的查询问题,并忘了把它放回去。

感谢额外的一双眼睛看到它。
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.


这篇关于Datagridview填充错误 - 找不到表(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆