Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
ConnectData()
        DisableBtn(Me)
        DisableTxt(Me)
        EnableBtn(Me)
        CmbStyle(Me)
        BtnSave.Enabled = False
        CmbFind.Hide()
        Try
            Sqlcommand.CommandText = "Select Name from State_Mast"
            Sqlcommand.ExecuteNonQuery()
            Dim Da As New MySqlDataAdapter
            Da.SelectCommand = Sqlcommand()
            Da.Fill(Ds, "state")
            DBState.DataSource = Ds.Tables("state")
            DBState.ValueMember = "Name"
            DBState.DisplayMember = "Name"
            Da.Dispose()
        Catch ex As MySql.Data.MySqlClient.MySqlException
            MessageBox.Show(ex.Message)
        End Try


the Sqlcommand.CommandText = "Select Name from State_Mast" is throwing system.nullreferenceexcepton

What I have tried:

with the code below it worked fine
VB
Try
        ConnectData()
        DisableBtn(Me)
        DisableTxt(Me)
        EnableBtn(Me)
        CmbStyle(Me)
        BtnSave.Enabled = False
        CmbFind.Hide()
        Try
            SqlQuery = "Select Name from State_Mast"
            Sqlcommand = New MySqlCommand(SqlQuery, Conn)
            Dim count As String = Sqlcommand.ExecuteScalar
            Dim Da As New MySqlDataAdapter

            Da.SelectCommand = Sqlcommand
            Da.Fill(Ds, "state")
            DBState.DataSource = Ds.Tables("state")
            DBState.ValueMember = "Name"
            DBState.DisplayMember = "Name"
            Da.Dispose()

Catch ex As MySql.Data.MySqlClient.MySqlException
            MessageBox.Show(ex.Message)
        End Try
Posted
Updated 21-Oct-19 9:47am
v2
Comments
Richard MacCutchan 21-Oct-19 12:01pm    
You forgot to add a connection object.
Member 13550326 21-Oct-19 12:39pm    
ConnectData() defines the connection.
Member 13550326 21-Oct-19 12:39pm    
connectdata() defines connection

1 solution

Quote:
system.nullreferenceexcepton
This means something is null. In this case SelectCommand is null.

Quote:
Sqlcommand.CommandText = "Select Name from State_Mast"
You try to access the CommandText property of something that is null.
 
Share this answer
 
Comments
Member 13550326 21-Oct-19 17:32pm    
I didn't understand. Please tell what is the problem and show the right code for it.
ZurdoDev 21-Oct-19 19:50pm    
SelectCommand is null. You have not initialized it.
Member 13550326 22-Oct-19 6:12am    
I'm new to programming , tell me how should i initialise it.
ZurdoDev 22-Oct-19 6:38am    
You said you have some code that works fine up above. So look at that and see where it gets initialized. Also go through examples online to learn how to use ADO.Net.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900