Mysql Odbc Conncection C#


Recommended Posts

I have this class

/*
* Created by SharpDevelop.
* User: huskeyw
* Date: 10/26/2006
* Time: 6:53 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
// DbConnect MySql connectionclass, this class builds all the connections and basic sql commands.
// Copyright (C) 2006 William S. Huskey
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


using System;
using System.Collections.Generic;
using System.Data.Odbc;

namespace Time_Admin
{
/// <summary>
/// MYSQL connector through ODBC
/// </summary>
public class MyConnect
{
#region vairables
private string server;

public string Server {
get {
return server;
}
set {
server = value;
}
}


private string dataBase;

public string DataBase {
get {
return dataBase;
}
set {
dataBase = value;
}
}

private string uID;

public string UID {
get {
return uID;
}
set {
uID = value;
}
}


private string passWord;

public string PassWord {
get {
return passWord;
}
set {
passWord = value;
}
}

private string option;

public string Option {
get {
return option;
}
set {
option = value;
}
}



private string Driver = "DRIVER={MySQL ODBC 3.51 Driver};";

public string mySqlConnect;

public string MySqlConnect {
get {
return mySqlConnect;
}
set {
mySqlConnect = value;
}
}

private OdbcConnection MySqlConnection;
public OdbcCommand MySqlCommand;
public OdbcDataReader myDataReader;

#endregion
#region construtor
//Construtor MyConnect
//"SERVER=localhost;" +
//"DATABASE=test;" +
//"UID=root;" +
//"PASSWORD=1ccaros12;" +
//"OPTION=3";
/// <summary>
///
/// </summary>
/// <param name="srv"></param>
/// <param name="dataBase"></param>
/// <param name="User"></param>
/// <param name="passwd"></param>
/// <param name="Opt"></param>
public MyConnect (string srv , string dataBase , string User , string passwd, string Opt)
{
this.Server = srv;
this.DataBase = dataBase;
this.UID = User;
this.PassWord = passwd;
this.Option = Opt;
this.MySqlConnect = this.Driver + this.Server + ";" +
this.DataBase + ";" + this.UID + ";" + this.PassWord + ";" + this.Option + ";";
try
{
this.MySqlConnection = new OdbcConnection(MySqlConnect);
this.MySqlConnection.Open();
this.MySqlCommand = new OdbcCommand("USE "+ this.DataBase, MySqlConnection);
this.MySqlCommand.ExecuteNonQuery();


}
catch
{

}
}
#endregion

#region methods
public void Insert (string table, string[] Values)
{
string values = "";
for (int x = 0; x < Values.Length; x++)
{

values += "'" + Values[x] + "',";
}
values = values.Substring(0, values.Length -1);

this.MySqlCommand.Connection = MySqlConnection;
this.MySqlCommand.CommandText = "INSERT INTO " + table + " VALUES("+values+")";
this.MySqlCommand.ExecuteNonQuery();
}

public void Modify (string condetions,string table,string where)
{

MySqlCommand.CommandText = "SELECT " + condetions + " FROM " + table + " WHERE " + where;
MySqlCommand.ExecuteNonQuery();

}


#endregion

}
}

but when I call insert() it tells me that

Exception System.NullReferenceException was thrown in debuggee:

Object reference not set to an instance of an object.

and points to this line

this.MySqlCommand.Connection = MySqlConnection;

thanks..

Link to post
Share on other sites

to add this is where this is called

/*
* Created by SharpDevelop.
* User: huskeyw
* Date: 11/7/2006
* Time: 4:43 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
// ADDUSER component of the of Time Keeper software
// Copyright (C) 2006 William S. Huskey
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


using System;
using System.Drawing;
using System.Windows.Forms;



namespace Time_Admin
{
/// <summary>
/// Description of addUser.
/// </summary>
public partial class addUser
{
Employee employee;

public addUser()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}



void ExitToolStripMenuItemClick(object sender, System.EventArgs e)
{
this.Close();
}

void Button1Click(object sender, System.EventArgs e)
{

//try
// {


this.employee = new Employee(tbFname.Text,tbLname.Text,Convert.ToInt32(tbEmpID.Text),
Convert.ToInt32(tbEmptype.Text),tbCity.Text,tbStreet.Text,
tbStreet2.Text,tbState.Text,Convert.ToInt32(tbZip.Text),tbPassword.Text);
//lbPassword.Text = this.employee.Password(tbPassword.Text);
MyConnect database = new MyConnect("localhost","timekeeper","timeadm","password","3");
database.Insert("empID",employee.buildTable);


// }
// catch
// {
// MessageBox.Show("you need to enter at least\nLast Name \nEmployee number \nEmployee type \nZipCode");

// }


}
}
}

here is the lines that call it

MyConnect database = new MyConnect("localhost","timekeeper","timeadm","password","3");

database.Insert("empID",employee.buildTable);

Link to post
Share on other sites
			try 
{
this.MySqlConnection = new OdbcConnection(MySqlConnect);
this.MySqlConnection.Open();
this.MySqlCommand = new OdbcCommand("USE "+ this.DataBase, MySqlConnection);
this.MySqlCommand.ExecuteNonQuery();


}
catch
{

}

That could be the problem. If an exception is thrown before MySqlCommand is assigned it will be left null and the MyConnect object will be unusable. Either the ctor should throw an exception (easy solution: remove the try-catch) or Insert() should check whether MySqlConnection and MySqlCommand are null and either create a new connection or throw.

Edited by jcl
Link to post
Share on other sites

Thanks, the try was hiding the problem

the connection string should look like this

this.mySqlConnect = this.Driver +";SERVER=" + this.Server + ";DATABASE=" + this.DataBase + ";UID=" + this.UID + ";PASSWORD=" + this.PassWord + ";OPTION=" + this.Option + ";" ;

I had it in my comments from my test program but when I created the class I missed carring over the SERVER= parts

Thank you for pointing me in the right direction.. I don't feel as dumb as I did before..

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...