对象引用未设置为对象的实例 - datagridview

时间:2013-12-28 07:09:19

标签: mysql vb.net datagridview

当我尝试将数据添加到我应该在xampp上更新我的数据库的数据网格时,我得到上面的错误:myadapter.SelectCommand.CommandText = sqlquery

我认为这与空值有关,但我似乎无法修复它。

这是我的表单代码:

Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports System.Drawing.Printing
Imports System
Imports System.Windows.Forms

Public Class 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
    Dim reader As MySqlDataReader
    Dim objcommand As New MySqlCommand
    Dim myadapter As New MySqlDataAdapter
    Dim dataset As DataSet

    Private Sub frmClientDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DGVClient.Columns.Clear()
        Dim dt As New DataTable
        objdataadapter.SelectCommand = New MySqlCommand()
        objdataadapter.SelectCommand.Connection = objconnection
        objdataadapter.SelectCommand.CommandType = CommandType.Text
        objdataadapter.SelectCommand.CommandText = "SELECT * FROM Client_Details"
        objdataadapter.Fill(dt)


        rowposition = 0

        DGVClient.DataSource = dt

    End Sub
    Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
        frmMainMenu.Show()
        Me.Hide()
    End Sub

    Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
        txtCompanyName.Clear()
        cbxCompanyType.Items.Clear()
        txtVAT.Clear()
        txtPAYE.Clear()
        txtAddressLine.Clear()
        txtCity.Clear()
        txtPostcode.Clear()
        txtEmail.Clear()
        txtPhoneNumber.Clear()
    End Sub


    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        objconnection.Open()
        Dim sqlquery As String
        sqlquery = "Insert into Client_Details (CompanyName , CompanyType , VATRegistrationNumber , PAYEandTaxReference , AddressLine1 , City , Postcode , Email , PhoneNumber) Values ('" &
         txtCompanyName.Text & "','" & cbxCompanyType.Text & "' , '" & txtVAT.Text & "','" & txtPAYE.Text & "' , '" & txtAddressLine.Text & "' , '" & txtCity.Text & "' , '" & txtPostcode.Text & "' , '" &
         txtEmail.Text & "' , '" & txtPhoneNumber.Text & "')"
        myadapter.SelectCommand.CommandText = sqlquery
        myadapter.SelectCommand.CommandType = CommandType.Text

        Try
            dataset = New DataSet
            myadapter.Fill(dataset, "client_details")
            MsgBox("info added")
        Catch
            MsgBox("not added")
        End Try
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

可能在Your question befor

重复

此外,您没有设置与适配器的连接:

 Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    Dim sqlquery As String
    sqlquery = "Insert into Client_Details (CompanyName , CompanyType , VATRegistrationNumber , PAYEandTaxReference , AddressLine1 , City , Postcode , Email , PhoneNumber) Values ('" &
     txtCompanyName.Text & "','" & cbxCompanyType.Text & "' , '" & txtVAT.Text & "','" & txtPAYE.Text & "' , '" & txtAddressLine.Text & "' , '" & txtCity.Text & "' , '" & txtPostcode.Text & "' , '" &
     txtEmail.Text & "' , '" & txtPhoneNumber.Text & "')"

    Try
         If objconnection.State = ConnectionState.Open Then
            objconnection.Close()
        End If
        objconnection.Open()

        Dim objcommand As New MySqlCommand(sqlquery,objconnection)     
        objcommand.ExecuteNonQuery()
        MsgBox("info added")
    Catch
        MsgBox("not added")
    End Try
End Sub

结束班