InvalidOperationException 未处理 - 数据网格视图问题
Posted
技术标签:
【中文标题】InvalidOperationException 未处理 - 数据网格视图问题【英文标题】:InvalidOperationException was unhandled - data grid view issue 【发布时间】:2014-01-15 06:42:08 【问题描述】:错误: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 进口系统 导入 System.Windows.Forms
公共类 frmClientDetails 将 form_type 调暗为表单 将 user_table 调暗为字符串 Dim objconnection As New MySqlConnection("Server=localhost;database=ba-solutions;user id=root;password=") 将 sqlstring 调暗为字符串
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
模块过程_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 的新手,并且一直在努力修复这个问题几个小时,但没有成功,我相信这很简单,但话说回来,我什至不是一个基本的专家。感谢您的帮助。
【问题讨论】:
【参考方案1】:错误告诉您 objdataadapter.SelectCommand.CommandText 尚未设置。需要在调用 Fill 方法之前设置 CommandText 和 CommandType。
objdataadapter.SelectCommand.CommandType = CommandType.Text
objdataadapter.SelectCommand.CommandText = "Select statement goes here"
objdataadapter.Fill(objdataset)
【讨论】:
我应该把所有东西放在哪里,因为我不能在数据库上使用 Select 语句,如果我在模块中的表上使用它,那么它会继续调用同一个表? 它将进入 SQL_execute。您是否正在尝试构建一个通用方法来执行所有执行语句?我建议不要这样做,设置命令对象并填充它并不是什么工作。 我认为第一个问题已解决,但现在在我的模块中的这一行:frmClientDetails.DGVClient.DataMember = tablename 我收到错误:未处理参数异常......字段 Client_Details 的子列表不能已创建。【参考方案2】:您不要在 DB 上使用 Select,而是在 Table 上使用它。您可以通过设置一个变量来使其更通用,您可以将其设置为 diff 表名称,并且此 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
告诉它使用什么数据库。也不要用 ` 字符包围表名。然后用这个更正的 sql 字符串设置 CommandText。
【讨论】:
我仍然遇到同样的错误,请原谅我的无知,但是我把所有东西放在哪里,我很困惑。我不想将 select 语句放在过程和变量模块中,因为我将再次对不同的表使用 Select 语句。 @Livaren,我更新了示例,以便您可以使用变量来更改表名。以上是关于InvalidOperationException 未处理 - 数据网格视图问题的主要内容,如果未能解决你的问题,请参考以下文章
InvalidOperationException:未找到名为“Bearer”的 AuthorizationPolicy
为啥这个 OdbcConnection 会抛出 System.InvalidOperationException?
System.InvalidOperationException: '绑定实例已经被关联到监听
使用 PerformanceCounters 时获取 InvalidOperationException [关闭]