gpt4 book ai didi

c# - InvalidOperationException 未处理 - 数据 GridView 问题

转载 作者:行者123 更新时间:2023-11-30 00:53:06 25 4
gpt4 key购买 nike

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

我在我的一个表单中创建了一个数据 GridView ,我正在尝试使用它来显示 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

这是具有数据 GridView 的表单中的相关代码:

Imports MySql.Data

导入 MySql.Data.MySqlClient导入系统.绘图.打印进口系统导入 System.Windows.Forms

公共(public)类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

模块过程和变量

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 新手,花了几个小时来解决这个问题,但没有成功,我确信这很简单,但话又说回来,我什至不是一个基本的专家。感谢您的帮助。

最佳答案

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

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

关于c# - InvalidOperationException 未处理 - 数据 GridView 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20804651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com