C# and MS Access connection and access in windows application

hello,

I need a simple code to connect my program to a db.  And another to retrieve data from a single cell.

So far the only thing I've done is include System.Data.OleDb  and inserted the following code after
InitializeComponent();

            OleDbConnection clsConnection;
            clsConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Projects\app1\app1\\DB1.mdb;Persist Security Info=False");
            clsConnection.Open();

I don't know why I keep receiving a runtime error:

Format of the initialization string does not conform to specification starting at index 56.
LVL 1
SerphAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
you are missing some \ to be duplicated.
also, the Persist Security info is invalid...
 
            OleDbConnection clsConnection;
            clsConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Projects\\app1\\app1\\DB1.mdb;");
            clsConnection.Open();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
0
kaylanreilorCommented:
What happen if you write "Data Source=E:\Projects\app1\app1\DB1.mdb"
Let's also have a look to this website: http://www.connectionstring.com/
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

SerphAuthor Commented:
Thank you angelIII, it works!

But can you tell me how to retrieve a single cell and store it in a variable? I just need to know how the statement should be.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
a single cell? you mean, from 1 table, 1 specific row, 1 specific column? ...

you "simply" need to put the necessary SQL as CommandText into a OleDbCommand object, and use ExecuteScalar() method, which will return that value...
0
SerphAuthor Commented:
I'm a little confused, Is this correct?  I'm kind of clueless here!?
tt="SELECT * FROM gramm_rules WHERE Rule_ID='1'";
OleDbCommand cmd=new OleDbCommand(tt,clsConnection);
cmd.CommandText="SELECT * FROM gramm_rules WHERE Rule_ID='1'";
p = Convert.ToInt32(cmd.ExecuteScalar());

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
don't use *, but the column name you want to retrieve.
0
SerphAuthor Commented:
My bad, I changed it but I keep getting a runtime error: Type Mismatch, even though I convert it from object to string.
Thank you for your help, my knowledge in C# is very limited.

st=Convert.ToString(cmd.ExecuteScalar());

Open in new window

0
Syed Irtaza AliLead Software ArchitectCommented:
Normally the code is done like this...

 

DataSet dataSet = new DataSet();
daRules = new OleDbDataAdapter();
tt="SELECT * FROM gramm_rules WHERE Rule_ID='1'";
daRules.SelectCommand = new OleDbCommand(tt, conn);
try {
if (conn.State == ConnectionState.Closed)
    conn.Open();
daRules.Fill(dataSet, "RulesTable");
conn.Close();
 
DataView dvRules = dataSet.Tables["RulesTable"].DefaultView;
DataRow dr = dvRules[0].Row;
txtValue1 = dr["Column1"].ToString();
 
}
catch (Exception ex)
{
   MessageBox.Show(ex.ToString());
}

Open in new window

0
SerphAuthor Commented:
Thank you for your help, I'm now taking the time to expand my knowledge in C#.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.