|
-
Sep 7th, 2009, 05:14 AM
#1
Thread Starter
Lively Member
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
-
Sep 7th, 2009, 05:26 AM
#2
Re: object reference not set to an instance of an object
On which line does the error occur?
-
Sep 7th, 2009, 05:30 AM
#3
Thread Starter
Lively Member
Re: object reference not set to an instance of an object
sorry!
cn.open()
)
-
Sep 7th, 2009, 05:34 AM
#4
PowerPoster
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")
-
Sep 7th, 2009, 05:43 AM
#5
Thread Starter
Lively Member
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.
-
Sep 7th, 2009, 06:00 AM
#6
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) ?
-
Sep 7th, 2009, 06:07 AM
#7
Thread Starter
Lively Member
Re: object reference not set to an instance of an object
ok. i have uploaded a photo here: http://twitpic.com/gu1m4
-
Sep 7th, 2009, 06:51 AM
#8
Thread Starter
Lively Member
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?
-
Sep 7th, 2009, 07:23 AM
#9
Re: object reference not set to an instance of an object
Was all that in the same project or a completely new blank project?
-
Sep 7th, 2009, 07:32 AM
#10
Thread Starter
Lively Member
Re: object reference not set to an instance of an object
the same project. should i create a new one and try it again?
-
Sep 7th, 2009, 07:43 AM
#11
Re: object reference not set to an instance of an object
Yeah just give that a go and see what happens
-
Sep 7th, 2009, 07:48 AM
#12
PowerPoster
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
-
Sep 7th, 2009, 07:56 AM
#13
Thread Starter
Lively Member
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!
-
Sep 7th, 2009, 07:57 AM
#14
Thread Starter
Lively Member
Re: object reference not set to an instance of an object
 Originally Posted by Nitesh
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?
-
Sep 7th, 2009, 07:59 AM
#15
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/ ?
-
Sep 7th, 2009, 08:00 AM
#16
Re: object reference not set to an instance of an object
 Originally Posted by mjenkinson05
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.
-
Sep 7th, 2009, 08:02 AM
#17
Thread Starter
Lively Member
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.
-
Sep 7th, 2009, 08:04 AM
#18
PowerPoster
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
-
Sep 7th, 2009, 08:05 AM
#19
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:
Dim conn As New MySqlConnection("connection string here")
-
Sep 7th, 2009, 08:10 AM
#20
Thread Starter
Lively Member
Re: object reference not set to an instance of an object
great ideas guys, but still nothing.
anything else?
-
Sep 7th, 2009, 08:12 AM
#21
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..
-
Sep 7th, 2009, 09:26 AM
#22
Thread Starter
Lively Member
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.
-
Sep 7th, 2009, 09:28 AM
#23
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?
-
Sep 7th, 2009, 09:51 AM
#24
Thread Starter
Lively Member
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.
-
Sep 7th, 2009, 09:55 AM
#25
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..
-
Sep 7th, 2009, 10:03 AM
#26
Thread Starter
Lively Member
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)
-
Sep 7th, 2009, 10:08 AM
#27
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)
-
Sep 7th, 2009, 10:10 AM
#28
Thread Starter
Lively Member
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
-
Sep 7th, 2009, 10:17 AM
#29
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|