Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > ms access and c#
 

ms access and c#

i want to connect a login table in ms access to a login form in c# n check if user's authentic! if so go to nxt form!
i created a database usin ms access.... it has a login table and details table! so in my form i want to check this login table n if d user exist go to the details form
fathatsme  Monday, July 27, 2009 8:39 AM

Hi,

Base on my understanding, you want to check whether the login table contains user’s information.

First you need to connect to the access datasource. Then run the query to find whether there is theuser record. The following is an example. If can’t find the user record, return false. Use Try…catch�to catch the error.

public bool CheckUser(int UserID)

{

OleDbConnection connection = new OleDbConnection();

connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Database1.accdb";

OleDbCommand command = new OleDbCommand();

command.Connection = connection;

command.CommandText = "select count(*) from table1 where userid =" + UserID.ToString();

try

{

connection.Open();

int count = (int)(command.ExecuteScalar());

if (count != 0)

{

return true;

}

}

catch (Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

connection.Close();

command.Dispose();

connection.Dispose();

}

return false;

}

For more information, please refer to the following links:

Connection strings for Access

http://www.connectionstrings.com/access

Using ADO .NET - Access and OleDB

http://visualbasic.about.com/od/usingvbnet/l/aa050303c.htm

If you have further question, please feel free to tell me.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byLing WangMSFT, ModeratorTuesday, August 04, 2009 3:40 AM
  • Marked As Answer byfathatsme Thursday, July 30, 2009 8:22 AM
  • Unmarked As Answer byfathatsme Thursday, July 30, 2009 10:23 AM
  • Unmarked As Answer byfathatsme Tuesday, August 04, 2009 5:13 AM
  • Marked As Answer byfathatsme Tuesday, August 04, 2009 5:15 AM
  •  
Ling Wang  Wednesday, July 29, 2009 4:03 PM

Data Type of “Username�column in access database seems to be text type. Text should be enclosed in single quotes in the query.

For example:

Select count(*) from login where UserName = �/span>Ling�/span>

The commandtext should be:

command.CommandText = "select count(*) from login where UserName = '" + title + "'";

The following is an article about Data Access

http://msdn.microsoft.com/en-us/magazine/cc163799.aspx

Access 2007 Developer Reference. The last two items show information about access database.

http://msdn.microsoft.com/en-us/library/bb149076.aspx


Best regards,
Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Thursday, July 30, 2009 2:45 PM

{

OleDbConnection connection = new OleDbConnection();

connection.ConnectionString =

@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Fahad\Database\Database1.mdb;User Id=admin;";

connection.Open();

OleDbCommand command = new OleDbCommand("SELECT * FROM login", connection);

OleDbDataReader reader = command.ExecuteReader();

try

{

while (reader.Read())

{

u_n = reader.GetString(0);

pwd = reader.GetString(1);

if (textBox1.Text == u_n &&textBox2.Text == pwd)

{

this.Hide();

Form2 f2=new Form2();

f2.Show();

break;

}

else

MessageBox.Show("UserName/Password doesnt exist");

break;

}

}

catch (Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

connection.Close();

command.Dispose();

connection.Dispose();

}

}


this worked!!!

now i want to retrieve a row fm d database and display in text boxes...
name, address,____,dob are the fields wid corrspondin dats types
i want each f them in seperate text boxes

  • Marked As Answer byfathatsme Thursday, August 20, 2009 7:21 AM
  •  
fathatsme  Friday, July 31, 2009 6:07 AM

Hi,

Base on my understanding, you want to check whether the login table contains user’s information.

First you need to connect to the access datasource. Then run the query to find whether there is theuser record. The following is an example. If can’t find the user record, return false. Use Try…catch�to catch the error.

public bool CheckUser(int UserID)

{

OleDbConnection connection = new OleDbConnection();

connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Database1.accdb";

OleDbCommand command = new OleDbCommand();

command.Connection = connection;

command.CommandText = "select count(*) from table1 where userid =" + UserID.ToString();

try

{

connection.Open();

int count = (int)(command.ExecuteScalar());

if (count != 0)

{

return true;

}

}

catch (Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

connection.Close();

command.Dispose();

connection.Dispose();

}

return false;

}

For more information, please refer to the following links:

Connection strings for Access

http://www.connectionstrings.com/access

Using ADO .NET - Access and OleDB

http://visualbasic.about.com/od/usingvbnet/l/aa050303c.htm

If you have further question, please feel free to tell me.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byLing WangMSFT, ModeratorTuesday, August 04, 2009 3:40 AM
  • Marked As Answer byfathatsme Thursday, July 30, 2009 8:22 AM
  • Unmarked As Answer byfathatsme Thursday, July 30, 2009 10:23 AM
  • Unmarked As Answer byfathatsme Tuesday, August 04, 2009 5:13 AM
  • Marked As Answer byfathatsme Tuesday, August 04, 2009 5:15 AM
  •  
Ling Wang  Wednesday, July 29, 2009 4:03 PM
thanx a lot!
wot abt connecting mdb file?
also i wanted to search for username.... fm login tab
the code i used is..........

private void button1_Click(object sender, EventArgs e)

{

string title = textBox1.Text.ToString();

OleDbConnection connection = new OleDbConnection();

connection.ConnectionString =

@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Fahad\Database\Database1.mdb;User Id=admin;";

OleDbCommand command = new OleDbCommand();

command.Connection = connection;

command.CommandText =

"select count(*) from login where UserName ="+title;

//command.CommandText = "select count(*) from login where UserName !="+ textBox1.Text.ToString();

try

{

connection.Open();

//OleDbDataReader a = command.ExecuteReader();

count = (

int)(command.ExecuteNonQuery());

if (count != 0)

{

MessageBox.Show("done!!!!");

}

else

MessageBox.Show("UserName doesnt exist :P");

}

catch (Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

connection.Close();

command.Dispose();

connection.Dispose();

}

}


but i'm gettin an error!"No value given for one or more required parameters."

pls help me on this!!!

  • Edited byfathatsme Thursday, July 30, 2009 10:06 AMwant to add more details
  •  
fathatsme  Thursday, July 30, 2009 8:39 AM

Data Type of “Username�column in access database seems to be text type. Text should be enclosed in single quotes in the query.

For example:

Select count(*) from login where UserName = �/span>Ling�/span>

The commandtext should be:

command.CommandText = "select count(*) from login where UserName = '" + title + "'";

The following is an article about Data Access

http://msdn.microsoft.com/en-us/magazine/cc163799.aspx

Access 2007 Developer Reference. The last two items show information about access database.

http://msdn.microsoft.com/en-us/library/bb149076.aspx


Best regards,
Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Thursday, July 30, 2009 2:45 PM

{

OleDbConnection connection = new OleDbConnection();

connection.ConnectionString =

@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Fahad\Database\Database1.mdb;User Id=admin;";

connection.Open();

OleDbCommand command = new OleDbCommand("SELECT * FROM login", connection);

OleDbDataReader reader = command.ExecuteReader();

try

{

while (reader.Read())

{

u_n = reader.GetString(0);

pwd = reader.GetString(1);

if (textBox1.Text == u_n &&textBox2.Text == pwd)

{

this.Hide();

Form2 f2=new Form2();

f2.Show();

break;

}

else

MessageBox.Show("UserName/Password doesnt exist");

break;

}

}

catch (Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

connection.Close();

command.Dispose();

connection.Dispose();

}

}


this worked!!!

now i want to retrieve a row fm d database and display in text boxes...
name, address,____,dob are the fields wid corrspondin dats types
i want each f them in seperate text boxes

  • Marked As Answer byfathatsme Thursday, August 20, 2009 7:21 AM
  •  
fathatsme  Friday, July 31, 2009 6:07 AM

You can use a datatable or dataset to get the data. Then use databinding to show the text in textbox.

OleDbConnection connection = new OleDbConnection();

connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Fahad\Database\Database1.mdb;User Id=admin;";

OleDbCommand command = new OleDbCommand("SELECT * FROM login", connection);

OleDbDataAdapter adapter = new OleDbDataAdapter(command);

DataTable dt01 = new DataTable();

adapter.Fill(dt01);

//use the databinding to show the data in textbox

textBox1.DataBindings.Add("Text", dt01, "UserID", true);

textBox1.DataBindings.Add("Text", dt01, "UserName", true);

//or

//textBox1.Text = dt01.Rows[0]["UserID"].ToString();

//textBox1.Text = dt01.Rows[0][1].ToString();

You can also display data on the form through Data component of Visual Studio.

Walkthrough: Displaying Data on a Form in a Windows Application:

http://msdn.microsoft.com/en-us/library/wwh8ka92(VS.80).aspx

More information:

OleDbDataAdapter Class:

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.aspx

Data Binding and Windows Forms

http://msdn.microsoft.com/en-us/library/c8aebh9k.aspx

Getting Started with Data Access

http://msdn.microsoft.com/en-us/library/ms171883(VS.80).aspx

Displaying Data on Forms in Windows Applications

http://msdn.microsoft.com/en-us/library/ms171923(VS.80).aspx

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Monday, August 03, 2009 12:25 PM
thanx a lot!!

i need to kno more?
wil dat b too much f help i'm askin?

if so srry!

now dat d login module's alrit

i want to updat the database wit d values of
text box --- for address
date n time picker --- for DOB
radio button ---- gender!!!

also on click of veiwbutton retrieve fm data base!
thanx!
fathatsme  Tuesday, August 04, 2009 3:52 AM
Thank You !!!!!!!!


It just solved my problem.Now I can complete my application.
I was looking how to get data from my MsAccess Table into a textBox or CheckBox.


while (reader.Read())

}



textBox1.Text = u_n;

such a simple solution, & no one had ,i really looked around

Thakns a lot

{

u_n = reader.GetString(0);

pwd = reader.GetString(1);

  • Marked As Answer byfathatsme Thursday, August 20, 2009 7:21 AM
  • Unmarked As Answer byfathatsme Thursday, August 20, 2009 7:21 AM
  •  
Sahil Ansari  Thursday, August 20, 2009 5:47 AM

:) pls vote as helpful if it did help!!! thanx

fathatsme  Thursday, August 20, 2009 7:21 AM

You can use google to search for other answers

Custom Search

More Threads

• Log Shipping
• Multiple Tables Query and Update Problems
• Populating dataset
• BindingSource , DataBindings and my own private hell!
• Downloading of form control from HTTPS
• ListBox selection automatically resets when datasource is updated
• Data bindings problem
• Bindingnavigator save button does not save changes to current record
• help me: Add row with identity column in datagridview
• i want a example/concept of editing in datagridview