Results 1 to 29 of 29

Thread: object reference not set to an instance of an object

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Angry object reference not set to an instance of an object

    Im using a mysql connector to insert a record into a MYSQL Database table

    The code i know works, because ive used it in an app before,

    The poblem im getting is the error "object reference not set to an instance of an object" but i cant see what ive missed. I know it will be something so obvoius, so im hoping a fresh pair of eyes will find it.

    Code:
    Private Sub SaveNewCustomer()
    
            'Step 1 - Create SQL Command to INSERT record
            Dim cmd As MySqlCommand
            Dim cn As MySqlConnection
            cmd = New MySqlCommand("INSERT into CLIENTS (Company, Contact, Phone, Mobile, Email, Address, Website) VALUES (?Company, ?Contact, ?Phone, ?Mobile, ?Email, ?Address, ?Website)", cn)
    
            Try
    
                With cmd.Parameters
                    '.AddWithValue("?ID", txtID.Text)
                    .AddWithValue("?Company", txtCompany.Text)
                    .AddWithValue("?Contact", txtContact.Text)
                    .AddWithValue("?Phone", txtPhone.Text)
                    .AddWithValue("?Mobile", txtMobile.Text)
                    .AddWithValue("?Email", txtEmail.Text)
                    .AddWithValue("?Address", txtAddress.Text)
                    .AddWithValue("?Website", txtWWW.Text)
    
                End With
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    
            'Step 2 - Connect and Insert
            Try
                cn = New MySqlConnection()
                cn.ConnectionString = "server=" & My.Settings.HostIP & ";" & "user id=" & My.Settings.User & ";" & "password=" & My.Settings.Password & ";" & "database=clients"
                cn.Open()
                cmd.Connection = cn
                cmd.Prepare()
                cmd.ExecuteNonQuery()
                cn.Close()
                MsgBox("Successfully Added")
    
            Catch ex As Exception
                MsgBox(ex.Message)
    
            End Try
    
        End Sub
    Last edited by mjenkinson05; Sep 7th, 2009 at 05:19 AM. Reason: More Relevent Title

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: object reference not set to an instance of an object

    On which line does the error occur?
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    sorry!

    cn.open()

    )

  4. #4
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: object reference not set to an instance of an object

    ok your problem is here:

    Code:
    Dim cmd As MySqlCommand
            Dim cn As MySqlConnection
            cmd = New MySqlCommand("INSERT into CLIENTS (Company, Contact, Phone, Mobile, Email, Address, Website) VALUES (?Company, ?Contact, ?Phone, ?Mobile, ?Email, ?Address, ?Website)", cn)
    you assign a connection to the command but you set the connection properties afterwards.

    try this:

    Code:
      Dim cmd As MySqlCommand
            Dim cn As MySqlConnection
            cn = New MySqlConnection()
                cn.ConnectionString = "server=" & My.Settings.HostIP & ";" & "user id=" & My.Settings.User & ";" & "password=" & My.Settings.Password & ";" & "database=clients"
    
    cmd = New MySqlCommand("INSERT into CLIENTS (Company, Contact, Phone, Mobile, Email, Address, Website) VALUES (?Company, ?Contact, ?Phone, ?Mobile, ?Email, ?Address, ?Website)", cn)
    
                cn.Open()
                cmd.ExecuteNonQuery()
                cn.Close()
                MsgBox("Successfully Added")

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    I thought that, but no, that still creates the error.
    If I set a breakpoint, and step down through the code, the step after cn.open() is the Exception Catch, so it is deff the cn.open thats causing the issue.

    I just cant see whats causing it.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    when you step through the code, when it is just about to execute cn.Open() can you see if cn is actually Nothing (by hovering the mouse over cn in the debugger) ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    ok. i have uploaded a photo here: http://twitpic.com/gu1m4

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    ok. I decided to uncomplicate things by creating a new module, and writing the code again by hand.

    this is what i have :

    Code:
    Imports MySql.Data.MySqlClient
    Module modDBConnect
        Public conn As New MySqlConnection
    
        Public Sub ConnectDatabase()
    
            Try
                If conn.State = ConnectionState.Closed Then
                    conn.ConnectionString = "DATABASE=" & My.Settings.Database & ";SERVER=" & My.Settings.HostIP & ";user id=" & My.Settings.User & ";password=" & My.Settings.Password ' & ";charset=utf8"
                    conn.Open()
                End If
    
            Catch myerror As Exception
    
                MessageBox.Show(myerror.Message)
                End
            End Try
        End Sub
    
        Public Sub DisconnectDatabase()
            Try
                conn.Close()
            Catch myerror As MySql.Data.MySqlClient.MySqlException
    
            End Try
        End Sub
    
    End Module
    I then created a fresh new form, with 2 buttons. One calles the Connect method, the other the disconnect. no sql query commands, or anything.

    It STILL creates a "not referenced" error on the conn.open

    im starting to think this is not my fault any more.

    any ideas yet?

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    Was all that in the same project or a completely new blank project?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    the same project. should i create a new one and try it again?

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    Yeah just give that a go and see what happens
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: object reference not set to an instance of an object

    try this:

    Code:
    Using con as New MySqlConnection(strConn)
    where strConn is equal to that string that has, Database....... blah blah

    if that don't work i assume your connection string is incorrect

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    made a new program, wrote the code by hand. same error.
    this really doesnt make any sence!

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    Quote Originally Posted by Nitesh View Post
    try this:

    Code:
    Using con as New MySqlConnection(strConn)
    where strConn is equal to that string that has, Database....... blah blah

    if that don't work i assume your connection string is incorrect
    Could you elaborate?

  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    Are you using the latest version of the .NET connector from here: http://www.mysql.com/products/connector/ ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    Quote Originally Posted by mjenkinson05 View Post
    Could you elaborate?
    I think he just means instead of creating a New MySqlConnection and then setting the connection string, try setting the connection string when you create the new instance - thats assuming the MySqlConnection class has a constructor that supports this.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  17. #17

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    Yes, i was using 6.1 origionally and it didnt work. i realised it was a beta verison, so i downgraded to 6.0, but still got the problem.

  18. #18
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Re: object reference not set to an instance of an object

    Code:
    strConn="DATABASE=" & My.Settings.Database & ";SERVER=" & My.Settings.HostIP & ";user id=" & My.Settings.User & ";password=" & My.Settings.Password ' & ";charset=utf8"
    
    Using con as New MySqlConnection(strConn)
    
    cmd = New MySqlCommand("INSERT into CLIENTS (Company, Contact, Phone, Mobile, Email, Address, Website) VALUES (?Company, ?Contact, ?Phone, ?Mobile, ?Email, ?Address, ?Website)", con)
    
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
    
    End Using

  19. #19
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    Ah I see. Have you tried setting the connection string in the constructor as Nitesh suggested? Thats how I always do it when working with SQL Server, never used MySQL but I just checked on their website and the constructor does support this. So you can just do:
    vb Code:
    1. Dim conn As New MySqlConnection("connection string here")
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  20. #20

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    great ideas guys, but still nothing.

    anything else?

  21. #21
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    I just tried to install the MySQL ADO.NET connector to see if I can help you out but it wont install just says "the installation failed due to an error" but doesnt tell me anything other than that! I'll keep trying..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  22. #22

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    i really dont know what this could be! Theres nothing on the web about it anywhere.

  23. #23
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    Well I just rebooted and still cant install the MySQL .NET components so I'm afraid I cant test and help you further. The only thing I can think of is that after you downgraded from the BETA version something got messed up... Have you got another PC you can try running that code on and see if the same thing happens?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  24. #24

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    ok so i think im gunna sack off the mysql idea, is there any other way of doing it? im not up on MSSQL server etc.

  25. #25
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    MS SQL server is pretty much the same as MySql from what I understand - the main difference being MySQL is free and MS SQL is not. Having said that, the express edition of MS SQL server is also free but it does have some limits..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  26. #26

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    is there a guide anywhere i can use to walk me thru? i am gunna give the 2008 express verison a try
    maybe i might get ms products to work on an ms os. (maybe being the operative word)

  27. #27
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    A guide to do what? You connect to it and use it through VB in the same way you were doing with MySQL. Instead of Dim xx As New MySQLConnection you just do Dim xx As New SQLConnection (After importing the SqlClient namespace anyway)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  28. #28

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Re: object reference not set to an instance of an object

    yes i get that, but i dont know how to set up the initial sql server, database, permissions etc.

    MYSQL make it easy, microsoft (as usual) do not

  29. #29
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: object reference not set to an instance of an object

    There's not really much to it - I've not used 2008 but in 2005 basically you do this:
    Install it, enable remote connections on it if necessary using the SQL Server Surface Configuration app that will now be in your Start Menu (think you might have to do it in another area as well in the general Configuration program, also in the Start Menu under Microsoft SQL Server). Then if you need to setup a database etc then install the free SQL Management Studio Express software and tell it to connect to your SQL server machine, its pretty obvious once you get in there how to do it (ie, right click on Databases and go to New Database).
    I'm sure there are plenty of guides on the net as well.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width