Rajamohan Anguchamy
Rajamohan Anguchamy

Reputation: 1736

Object reference not set to an instance of an object for loading User controls in vb.net

my code is

Public Function resload()
    Dim conn As MySqlConnection = New MySqlConnection(ConfigurationManager.ConnectionStrings("connectionrms").ConnectionString) ''line 18
    Dim comm As MySqlCommand = New MySqlCommand()
    Dim dr As MySqlDataReader
    conn.Open()
    comm = New MySqlCommand("Select distinct name from restaurant", conn)
    dr = comm.ExecuteReader()
    While dr.Read() <> Nothing
        headresnamecombo.Items.Add(dr(0).ToString())
    End While
    dr.Close()
    headresnamecombo.SelectedIndex = 0
    conn.Close()
    Return Nothing
End Function

My Error

at Rest.billingBody.resload() in C:\Users\Azinova7\Documents\Visual Studio 2008\Projects\Rest\Rest\billingBody.vb:line 18

at Rest.billingBody.billingBody_Load(Object sender, EventArgs e) in C:\Users\Azinova7\Documents\Visual Studio 2008\Projects\Rest\Rest\billingBody.vb:line 229

at System.Windows.Forms.UserControl.OnLoad(EventArgs e)

at System.Windows.Forms.UserControl.OnCreateControl()

at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

at System.Windows.Forms.Control.CreateControl()

at System.Windows.Forms.Control.ControlCollection.Add(Control value)

at System.Windows.Forms.Form.ControlCollection.Add(Control value)

at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)

after that all is same connection codes

Upvotes: 0

Views: 1160

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500495

I strongly suspect that this is the problem:

ConfigurationManager.ConnectionStrings("connectionrms").ConnectionString

I suspect you don't have a connection string named "connectionrms" in your configuration.

The ConnectionStringSettingsCollection indexer will return Nothing if the given connection string isn't found.

That's only one problem with your code though. Additionally, you should be using Using statements for the connection and reader, and your While loop condition should just be While dr.Read(). Oh, and I'd declare variables at the point of first use rather than declaring everything at the top.

Upvotes: 3

Related Questions