Click here to Skip to main content
15,889,702 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is procedure I want to call

Private Sub Display()
       TxtName.Text = Ds.Tables("LEDG").Rows(Rc)("Name").ToString
       CmbGrp.Text = Ds.Tables("LEDG").Rows(Rc)("Grp").ToString
       TxtAdd1.Text = Ds.Tables("LEDG").Rows(Rc)("Add1").ToString
       TxtAdd2.Text = Ds.Tables("LEDG").Rows(Rc)("Add2").ToString
       CmbCity.Text = Ds.Tables("LEDG").Rows(Rc)("City").ToString
       TxtDist.Text = Ds.Tables("LEDG").Rows(Rc)("Dist").ToString
       TxtPIN.Text = Ds.Tables("LEDG").Rows(Rc)("Pincode").ToString
       DBState.Text = Ds.Tables("LEDG").Rows(Rc)("State").ToString
       TxtEmail.Text = Ds.Tables("LEDG").Rows(Rc)("Email").ToString
       TxtPhone.Text = Ds.Tables("LEDG").Rows(Rc)("Landline").ToString
       TxtMob.Text = Ds.Tables("LEDG").Rows(Rc)("Mobile").ToString
       TxtVAT.Text = Ds.Tables("LEDG").Rows(Rc)("VATNO").ToString
       TxtCST.Text = Ds.Tables("LEDG").Rows(Rc)("CSTNO").ToString
       TxtGST.Text = Ds.Tables("LEDG").Rows(Rc)("GSTNO").ToString
       TxtPAN.Text = Ds.Tables("LEDG").Rows(Rc)("PAN").ToString
   End Sub


this is where I call it

Private Sub BtnFirst_Click_1(sender As Object, e As EventArgs) Handles BtnFirst.Click
        Dim Da As New MySqlDataAdapter
        Dim Ds As New DataSet
        SqlQuery = "Select * from Ledgers"
        Sqlcommand = New MySqlCommand(SqlQuery, Conn)
        count = Sqlcommand.ExecuteScalar
        Da.SelectCommand = Sqlcommand
        Da.Fill(Ds, "LEDG")
        Rc = 0
        Display()
End Sub


getting error " object reference not set to an instance of an object".

What I have tried:

When i try

Private Sub BtnFirst_Click_1(sender As Object, e As EventArgs) Handles BtnFirst.Click
        Dim Da As New MySqlDataAdapter
        Dim Ds As New DataSet
        SqlQuery = "Select * from Ledgers"
        Sqlcommand = New MySqlCommand(SqlQuery, Conn)
        count = Sqlcommand.ExecuteScalar
        Da.SelectCommand = Sqlcommand
        Da.Fill(Ds, "LEDG")
        Rc = 0
        TxtName.Text = Ds.Tables("LEDG").Rows(Rc)("Name").ToString
        CmbGrp.Text = Ds.Tables("LEDG").Rows(Rc)("Grp").ToString
        TxtAdd1.Text = Ds.Tables("LEDG").Rows(Rc)("Add1").ToString
        TxtAdd2.Text = Ds.Tables("LEDG").Rows(Rc)("Add2").ToString
        CmbCity.Text = Ds.Tables("LEDG").Rows(Rc)("City").ToString
        TxtDist.Text = Ds.Tables("LEDG").Rows(Rc)("Dist").ToString
        TxtPIN.Text = Ds.Tables("LEDG").Rows(Rc)("Pincode").ToString
        DBState.Text = Ds.Tables("LEDG").Rows(Rc)("State").ToString
        TxtEmail.Text = Ds.Tables("LEDG").Rows(Rc)("Email").ToString
        TxtPhone.Text = Ds.Tables("LEDG").Rows(Rc)("Landline").ToString
        TxtMob.Text = Ds.Tables("LEDG").Rows(Rc)("Mobile").ToString
        TxtVAT.Text = Ds.Tables("LEDG").Rows(Rc)("VATNO").ToString
        TxtCST.Text = Ds.Tables("LEDG").Rows(Rc)("CSTNO").ToString
        TxtGST.Text = Ds.Tables("LEDG").Rows(Rc)("GSTNO").ToString
        TxtPAN.Text = Ds.Tables("LEDG").Rows(Rc)("PAN").ToString
End Sub


everything is working OK
Please Help
Posted
Updated 14-Jan-18 8:58am
Comments
itsmypassion 4-Jan-18 0:48am    
You can make it Public procedure instead of private.

You should start by reading these articles:
Variable and Method Scope in Microsoft .NET[^]
Scope in Visual Basic | Microsoft Docs[^]

In a short: in your case object reference not set to an instance of an object error message says that DS variable inside Display procedure is inaccesible, because has been created and initiated inside tnFirst_Click_1 procedure.

You may resolve this by passing dataset as a parameter into Display procedure (recommended) or by defining DS variable outside tnFirst_Click_1 procedure (not recommended).

VB
Private Sub BtnFirst_Click_1(sender As Object, e As EventArgs) Handles BtnFirst.Click
    Dim Da As New MySqlDataAdapter
    Dim Ds As New DataSet
    'the part of code where Ds variable is initiated    
    '...
    'finally:
    Display(Ds)
End Sub

Private Sub Display(ByRef ds As DataSet)
    'your code here
End Sub


For further details, please see:
How to: Control the Scope of a Variable (Visual Basic) | Microsoft Docs[^]
Understanding the Scope of Variables[^]
Procedures in Visual Basic | Microsoft Docs[^]
Passing Arguments by Value and by Reference (Visual Basic) | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 13550326 4-Jan-18 21:04pm    
Thanks for your reply.
Already tried it, but getting error "Arugument not specified for parameters 'Ds' of private sub Display.
Maciej Los 5-Jan-18 1:49am    
Well, probably you did not read my answer carefully...
Hi Declare
Dim Ds As New DataSet
at start of class
When you declare Dataset at function level then you cant use that dataset out side of the function or you can use like this basic programing rule scope of a variable

Private Sub Display(byVal Ds as DataSet)
       TxtName.Text = Ds.Tables("LEDG").Rows(Rc)("Name").ToString
       CmbGrp.Text = Ds.Tables("LEDG").Rows(Rc)("Grp").ToString
       TxtAdd1.Text = Ds.Tables("LEDG").Rows(Rc)("Add1").ToString
       TxtAdd2.Text = Ds.Tables("LEDG").Rows(Rc)("Add2").ToString
       CmbCity.Text = Ds.Tables("LEDG").Rows(Rc)("City").ToString
       TxtDist.Text = Ds.Tables("LEDG").Rows(Rc)("Dist").ToString
       TxtPIN.Text = Ds.Tables("LEDG").Rows(Rc)("Pincode").ToString
       DBState.Text = Ds.Tables("LEDG").Rows(Rc)("State").ToString
       TxtEmail.Text = Ds.Tables("LEDG").Rows(Rc)("Email").ToString
       TxtPhone.Text = Ds.Tables("LEDG").Rows(Rc)("Landline").ToString
       TxtMob.Text = Ds.Tables("LEDG").Rows(Rc)("Mobile").ToString
       TxtVAT.Text = Ds.Tables("LEDG").Rows(Rc)("VATNO").ToString
       TxtCST.Text = Ds.Tables("LEDG").Rows(Rc)("CSTNO").ToString
       TxtGST.Text = Ds.Tables("LEDG").Rows(Rc)("GSTNO").ToString
       TxtPAN.Text = Ds.Tables("LEDG").Rows(Rc)("PAN").ToString
   End Sub


Private Sub BtnFirst_Click_1(sender As Object, e As EventArgs) Handles BtnFirst.Click
        Dim Da As New MySqlDataAdapter
        Dim Ds As New DataSet
        SqlQuery = "Select * from Ledgers"
        Sqlcommand = New MySqlCommand(SqlQuery, Conn)
        count = Sqlcommand.ExecuteScalar
        Da.SelectCommand = Sqlcommand
        Da.Fill(Ds, "LEDG")
        Rc = 0
        Display(Ds)
End Sub
 
Share this answer
 
v2

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