Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<hi!

i am making an application in which i have to get the user id from my access data base table "users" in which fields are "User ID", "User Name"
and i have to pass "User Name" from c # to get the "User ID" of thta particular "User Name".
Posted
Comments
parmar_punit 15-Jun-11 4:48am    
:-) you can upvote my answer if you dont want to accept solution...

<br />
I think it is good tutorial for you see <a href="http://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx">this</a>[<a href="http://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx" target="_blank" title="New Window">^</a>] link
 
Share this answer
 
To Solution 3: It is the code that you want...
public void ReadMyData(string myConnString)
{
    int _userId = 0;
    string mySelectQuery = "SELECT UserId FROM users WHERE UserName=@UserName";
    OleDbConnection myConnection = new OleDbConnection(myConnString);
    OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
    myCommand.Parameters.AddWithValue("@UserName", txtUserName.Text);//pass the user name that you want to find....
    myConnection.Open();
    OleDbDataReader myReader;
    myReader = myCommand.ExecuteReader();
    if (myReader.Read())
    {
        _userId = Convert.ToInt32(myReader[0]); // here you will get id of that user
    }
    myReader.Close();
    myConnection.Close();
}
 
Share this answer
 
v2
SInce this is your homework, I won't give you code - you should do something fro yourself! And besides, it is all in your notes.

1) Create a connection to your access database. Look at an OleDbConnection[^], and get the connection string from the Server Explorer pane: click on your database and it will show in the Properties pane.
2) Create a command to the database. Look at an OleDbCommand[^] using an SQL SELECT statement. Use parametrized query to access the data:
SELECT [User ID] FROM users WHERE [User Name]=@UN

3) Get the required user name into a string, and include it as a parameter:
mySqlCommand.Parameters.AddWithValue("@UN", myStringWithTheUserNameInIt);

4) Execute the command into a dataReader. Look at OleDbDataReader[^]
5) Read the data from the reader for later.
 
Share this answer
 
<pre lang="sql">i am making an application in which i have to get the user id from my access data base table "users" in which fields are "User ID", "User Name"
 and i have to pass "User Name" from c # to get the "User ID" of thta particular "User Name".


 
Share this answer
 
Comments
parmar_punit 15-Jun-11 5:06am    
you have accepted my solution before and you have undo it, but see my next solution, i'm sure this time you will accept it.... :-) thanks
refer this:

Simple Movie Database in C# using Microsoft Access[^]

hope this helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900