【发布时间】: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 的新手,并且一直在努力修复这个问题几个小时,但没有成功,我相信这很简单,但话说回来,我什至不是一个基本的专家。感谢您的帮助。
【问题讨论】:
标签: c# mysql vb.net datagridview xampp