简体   繁体   中英

Object reference not set to an instance of an object - datagridview

When I try add data to my datagrid which should update my database on xampp, I get the error above at: myadapter.SelectCommand.CommandText = sqlquery

I think it is something to do with null values, but I can't seem to fix it.

Here is my form code:

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

possible duplicate at Your question befor

Also you didn't set connection to your adapter :

 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

End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM