InvalidOperationException未处理 - 数据网格视图问题

时间:2013-12-27 17:01:20

标签: c# mysql vb.net datagridview xampp

错误:commandtext属性未正确初始化。

我已经在我的一个表单中创建了一个数据网格视图,我试图用它来显示xampp的数据库详细信息。虽然当我去尝试打开表单时,我得到上面的错误,它将我引导到我的程序和变量模块到本节:

   'Procedure which executes any SQL query.
Public Sub SQL_executer()
    Call connection_checker()

    objdataadapter.SelectCommand = New MySqlCommand()
    objdataadapter.SelectCommand.Connection = objconnection
    objdataadapter.SelectCommand.CommandText = sqlstring

    objcommandbuilder = New MySqlCommandBuilder(objdataadapter)
    objdataadapter.Fill(objdataset) ----------- THIS SECTION GIVES ERROR
    objdataadapter.SelectCommand.CommandType = CommandType.Text

End Sub

'Procedure used to load data from the database for the selected table.
Public Sub initial_load()
    Call connection_checker()
    Call SQL_executer()

    objdataset = New DataSet
    objdataadapter.Fill(objdataset, tablename)
    objconnection.Close()

End Sub

这是具有数据网格视图的表单中的相关代码:

Imports MySql.Data

导入MySql.Data.MySqlClient 导入System.Drawing.Printing 进口系统 Imports System.Windows.Forms

公共类frmClientDetails     Dim form_type As Form     Dim user_table As String     Dim objconnection As New MySqlConnection(“Server = localhost; database = ba-solutions; user id = root; password =”)     Dim sqlstring As String

Private Sub frmClientDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    sqlstring = "SELECT * FROM `BA-Solutions`"
    tablename = "`Client_Details`"

    Call initial_load()
    Call bind_dataset_DGVClient()
    Call count_records()
    rowposition = 0

    DGVClient.DataSource = objdataset
    DGVClient.DataMember = tablename
End Sub

这是我的整个程序和变量模块供参考

Imports MySql.Data

导入MySql.Data.MySqlClient

模块procedures_and_variables

Public objconnection As New MySqlConnection("Server=localhost;database=ba-solutions;user id=root;password=")

Public objdataadapter As New MySqlDataAdapter
Public objdataset As New DataSet
Public objcommandbuilder As New MySqlCommandBuilder
Public objdatatable As New DataTable
Public rowposition As Integer = 0
Public sqlstring As String
Public tablename As String
Public objcommand As MySqlCommand
Public reader As MySqlDataReader
Public database_path As String = "Server=localhost;database=ba-solutions;user id=root;password="
Public path As String
Public backup As New MySqlBackup

'Procedure which checks whether or not the current connection is open and opens it, if it is closed.
Public Sub connection_checker()
    If objconnection.State = ConnectionState.Closed Then
        Try
            objconnection.Open()
        Catch ex As MySqlException
            MsgBox("Error connecting to database")
        End Try
    End If
End Sub

'Procedure which executes any SQL query.
Public Sub SQL_executer()
    Call connection_checker()

    objdataadapter.SelectCommand = New MySqlCommand()
    objdataadapter.SelectCommand.Connection = objconnection
    objdataadapter.SelectCommand.CommandText = sqlstring

    objcommandbuilder = New MySqlCommandBuilder(objdataadapter)
    objdataadapter.Fill(objdataset)
    objdataadapter.SelectCommand.CommandType = CommandType.Text

End Sub

'Procedure used to load data from the database for the selected table.
Public Sub initial_load()
    Call connection_checker()
    Call SQL_executer()

    objdataset = New DataSet
    objdataadapter.Fill(objdataset, tablename)
    objconnection.Close()

End Sub

'Procedure used to update data in a table with the changes made to the data in the datagrid.
Public Sub update_data()
    Call connection_checker()
    Try
        objdataadapter.Update(objdataset, tablename)
        MsgBox("Changes accepted", MsgBoxStyle.Information, "Update successfull")
    Catch ex As Exception
        MsgBox("Changes declined", MsgBoxStyle.Critical, "Update unsuccessfull")
    End Try
End Sub

'Procedures used to bind the relevant data to the data grid, with the correct header titles.
Public Sub bind_dataset_DGVClient()
    frmClientDetails.DGVClient.AutoGenerateColumns = True
    frmClientDetails.DGVClient.DataSource = objdataset
    frmClientDetails.DGVClient.DataMember = tablename

    frmClientDetails.DGVClient.Columns(0).HeaderText = "Company Name"
    frmClientDetails.DGVClient.Columns(1).HeaderText = "Company Type"
    frmClientDetails.DGVClient.Columns(2).HeaderText = "VAT Registration Number"
    frmClientDetails.DGVClient.Columns(3).HeaderText = "PAYE and Tax Reference"
    frmClientDetails.DGVClient.Columns(4).HeaderText = "Address Line 1"
    frmClientDetails.DGVClient.Columns(5).HeaderText = "City"
    frmClientDetails.DGVClient.Columns(6).HeaderText = "Postcode"
    frmClientDetails.DGVClient.Columns(7).HeaderText = "Email"
    frmClientDetails.DGVClient.Columns(8).HeaderText = "Phone Number"






    'NEEDS TO BE COMPLETED FOR ALL DATASETS
End Sub

结束模块

我是vb / sql的新手,并且一直在努力修复这个问题几个小时无济于事,我确信这很简单,但是我再也不是一个基本的专家了。谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

错误告诉您尚未设置objdataadapter.SelectCommand.CommandText。需要在调用Fill方法之前设置CommandText和CommandType。

objdataadapter.SelectCommand.CommandType = CommandType.Text
objdataadapter.SelectCommand.CommandText = "Select statement goes here"
objdataadapter.Fill(objdataset)

答案 1 :(得分:0)

您不在数据库上使用Select,而是在表上使用它。你可以通过设置一个可以设置为扩展表名的变量使这个更通用,这个sql字符串将加载告诉它的表。

Public Function ReloadNewTable(tablename As String) As DataTable
  Dim sqlstring = String.Format("SELECT * FROM {0}", tablename)
  'fill a datatable with new query and return the datatable.

你的Connections String告诉它使用什么Db。不要用`char来包围表名。然后使用此更正的sql字符串设置CommandText。