Exception key cannot be null

abhi2823

Active member
Joined
Sep 8, 2007
Messages
32
Programming Experience
1-3
Hi there
Can any one explain the meaning of the exception
Key cannot be null

when i am trying to open mysql database with
conn.open()
 
attempt to use a hashtable or key=value pairing where the key is null. check your connection string or mysql configuration
 
Thanks for the reply
As far as connection string is concerned it is coming from the login form which I have created and if there is no problem in login then i suppose my connection string is ok
 
When you create the Connection object, it only check for syntaxic correctness (I know it check the provider, but I do not have any precision for the rest). Weither or not the connection string points to a valid server and database with valid credentials is not verified until the connection is opened. If you want to make sure the connection is valid in the login form, open and close the connection. An exception will tell you it is not.

Also, make sure that you do not have a ';' standing just before an '='. The ';' ends a statement and the '=' means the end of a key declaration. If no name is between the two, I would guess it would throw the exception you received.
 
Exception

This is the code for login screen

Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmLogin
Dim conn As MySqlConnection

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Application.Exit()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
conn = New MySqlConnection()
Dim myconnstring As String
myconnstring = "server=" & txtServer.Text & ";" _
& "user id=" & txtUserName.Text & ";" _
& "password=" & txtPassword.Text & ";" _
& "database=oil"
conn.ConnectionString = myconnstring
Try
conn.Open()
conn.Close()
Dim mainForm As New frmMain
Dim taxfrm As New frmTaxM
Dim addbrok As New frmAddBroker
mainForm.connectionString = myconnstring
taxfrm.connectionString = myconnstring
addbrok.connectionString = myconnstring
mainForm.Show()
Me.Hide()
Me.Close()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to the database : " & myerror.Message)
Finally
conn.Dispose()
End Try
End Sub

This is the code for the form in which I am geting the exception
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmTaxM
Private myconnstring As String
Public WriteOnly Property connectionString() As String
Set(ByVal value As String)
myconnstring = value
End Set
End Property

Private Sub frmTaxM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New MySqlConnection
Dim mycommand As New MySqlCommand
conn.ConnectionString = myconnstring
mycommand.Connection = conn
mycommand.CommandText = "INSERT INTO table(a,b)" _
& "values(?a,?b)"
mycommand.Parameters.Add("?a", TextBox1.Text)
mycommand.Parameters.Add("?b", TextBox2.Text)
Try
conn.Open() 'This is where I am geting the exception
mycommand.ExecuteNonQuery()
Catch myerror As MySqlException
MsgBox("there was an error saving to the database :" & myerror.Message)
End Try

End Sub
End Class

I am flowing the connection string from the login form and putting it in to write only property
 
Strange... I cannot see any logical error in the code. Are you certain that the connection string that is set to the conn object is the right one? Try to put a breakpoint and a watch on the reference. If it is, then maybe you have another connection open in your app and the MySQL options do not allow it somehow. I really can't help you there as I've never had to configure a MySQL database...

Hope you can solve this, I can't do any better for you...
 
Try
conn.Open()
conn.Close() :D
Dim mainForm As New frmMain
Dim taxfrm As New frmTaxM
Dim addbrok As New frmAddBroker
mainForm.connectionString = myconnstring
taxfrm.connectionString = myconnstring
addbrok.connectionString = myconnstring
mainForm.Show()
Me.Hide()
Me.Close()
Catch myerror As MySqlException
MessageBox.Show("Error Connecting to the database : " & myerror.Message)
Finally
conn.Close() :)
conn.Dispose()
End Try
 
Thanks and I tried But when I am putting breakpoints
looks like connection string is not flowing from the login form to the other form
Private myconnstring As String
Public WriteOnly Property connectionString() As String
Set(ByVal value As String)
myconnstring = value 'nothing is here the connection string should be here
End Set
End Property

Can somebody help
 
Set(ByVal value As String)
myconnstring = value

if u do this nothing will come. "value" doesnt have anything.
it is the same as, u write:

dim value as string
myconnstring = value :confused:

how about u make a class, that setting ur connection.
as long as the class is in the same project as ur forms. the connection are same.
 
there has to be way in which the values can be taken to the next form

Public WriteOnly Property connectionString() As String
Set(ByVal value As String)
myconnstring = value 'nothing is here the connection string should be here
End Set
End Property
 
I think I found it... When you do this :

VB.NET:
Dim mainForm As New frmMain
Dim taxfrm As New frmTaxM
Dim addbrok As New frmAddBroker

you are creating local references to these object. You set the connection string to them, then your references are lost when you exit the method. The only way you could open these forms in the mainform class with the current code is by creating new ones with the default connection string.

What I mean is that, when you open the frmTaxM or frmAddBroker forms from the main form, you are not calling the right instance of the frmTaxM and frmAddBroker classes. The shortest solution is to make the property "connectionString" and the instance variable "myconnstring" static (with the keyword shared). You should also use this code instead :

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    conn = New MySqlConnection()
    Dim myconnstring As String
    myconnstring = "server=" & txtServer.Text & ";" _
    & "user id=" & txtUserName.Text & ";" _
    & "password=" & txtPassword.Text & ";" _
    & "database=oil"
    conn.ConnectionString = myconnstring
    Try
        conn.Open()
        conn.Close()

        Dim mainForm As New frmMain
        mainForm.connectionString = myconnstring
        frmTaxM.connectionString = myconnstring ' use the shared variable
        frmAddBroker.connectionString = myconnstring ' use the shared variable

        mainForm.Show()
        Me.Hide()
        Me.Close()
    Catch myerror As MySqlException
        MessageBox.Show("Error Connecting to the database : " & myerror.Message)
    Finally
        conn.Dispose()
    End Try
End Sub
 
That's pretty straight foward ...

VB.NET:
    Private Shared myconnstring As String

    Public Shared Property connectionString() As String
        Get
            Return myconnstring
        End Get
        Set(ByVal value As String)
            myconnstring = value
        End Set
    End Property

That way, these variables are "shared" between all instances of the class you define them in. You can keep the rest of your code unchanged, you could even leave the modified code I provided you as it is (altough it is unnefficient to create the forms once here and once when you show it).

--

Just some definitions of OOP concepts that seem in order :

Class or type : Model on which to create objects. Defines a list of preoperties and methods that define teh state and behavior of each object.

Object or instance : One use of a class. When you use the New keyword, you create an instance of that class.

Instance variable : One of the property of an object. This is a reference to another object or a value that describes the current object. By changing that value/reference, you affect the state of the object.

State of an object : General term for refering to the list of all instance variables of an object, which inherently defines it. Generally speaking, two object with in same state should behave the same.

Behavior of an object : Way it interacts with other object and itselves through the use of its methods.

--

Using these definitions, an instance variable can be different between two instances of a class (hence you would set the value in the first object wihtout affecting the other). However, shared or static variables are stored at class level and are the same for every instances of the class they are defined in. They do not define the state of the object. They can be used by the class' name directly or through an instance's reference. They are not inherited (although accessing a super class' static field may be possible through a reference of the sub class') and cannot be overriden.

The latest results in the fact that using a reference of type SuperClass, I can access the static properties and methods of the SuperClass even if the type of the object (not the reference) is of type SubClass and contains static fields and methods that shadow that of SuperClass which it inherits. The type of the reference is always the same type or the type of a super class of the object it refers to (which is why casting an object to a type that is not its type of that of a super class will throw an Exception).

Many a programmer are unaware of those technicalities, but they are very important, not always for writing code but often for debugging it. Wow, I was in fire tonight ;) !
 
Back
Top