Re: C# and Access connection
Hi,
u can not call the open mehode of oledbconnection here
Instead u should try this
Code:
public class dbFunctions
{
string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
OleDbConnection conn;
public void openConnection()
{
conn = new OleDbConnection(connString);
conn.Open();
}
}
Re: C# and Access connection
You cannot have code in a class without having it in a method or in the Constructor.
Quote:
Originally Posted by preetham.n
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data; // State variables
using System.Data.ADO; // Database
using System.Globalization; // Date
namespace Ticket_Order
{
public class DbFunctions {
string connString = string.Empty
//string connString = @"provider = microsoft.jet.oledb.4.0;data source = Employee.mdb;";
// the constructor
public DBFunctions(){
connString = Properties.Settigs.ConnString;
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
}
}
}
Here I have shown you how to do this in the Constructor of your DbFunctions class.
For getting this working do the Connectionstring in the settings of your program as programm defined setting.
This has good reasons regarding using your program on different machines or different adresses of your database without needing to recompile your code. ( Settings can be changed from outside compiled code Look to MSDN help regarding 'Settings'. I also would recommand you to reread 'naming conventions in C# as public classes should begin with a big letter and the name Functions is no good name for a class. We are not in VB 6.0 where you created code moduls. If you want to have a class that only provides methods then why not using static methods so you can use them without creating instances of your class ? But IMHO you simple want to build a sort of DatabaseLayer class which allows you to connect to your database and contains all methods needed to read, write, delete data in your database, isn't it ?
BTW in futuer I would not answer to posts not using forum habits like code tags. ( look to the bottom of my post how to do it.)
Re: C# and Access connection
Thanks guys, and Jonny will keep in mind all your suggestions .Thanks again.
Re: C# and Access connection
Quote:
Originally Posted by preetham.n
Thanks guys, and Jonny will keep in mind all your suggestions .Thanks again.
Look in my code again There was an error in the constructor. Its corrected now