Hi, i have an application where the user will go to there details page where there first name and last name,, username and email will be cast back from the access database. How do i select them from the database and put them into their own strings?
When the page loads i wnat the usernames details to be taken from the database and added to textboxes on the page. This is the code i have tried to use.
txtbUserName.Text = "aido222";
string conn_string = @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Users\user\Documents\College Work\3rd Year\web Programming\House100DataBase.accdb;
Persist Security Info=False;";
using (OleDbConnection connection1 = new OleDbConnection(conn_string))
{
//query selects users from user table where username equals inputted name
string query1 = "select * from user_tbl where userName = @Username";
OleDbCommand mySQLCommand1 = new OleDbCommand(query1, connection1);
OleDbDataReader myDataReader;
mySQLCommand1.Parameters.AddWithValue("@UserName", txtbUserName.Text);
connection1.Open();
mySQLCommand1.ExecuteNonQuery();
myDataReader = mySQLCommand1.ExecuteReader();
//if query returns data(If the username exists) do this..
string FName;
string SName;
string Email;
if (myDataReader.GetValue(0) != DBNull.Value)
{
FName = myDataReader["fName"].ToString();
}
else
{
FName = "e";
}
if (myDataReader["sName"] != DBNull.Value)
SName = myDataReader["sName"].ToString();
else
{
SName = "s";
}
if (myDataReader["email"] != DBNull.Value)
Email = myDataReader["email"].ToString();
else
{
Email = "e";
}
txtbFirstName.Text = FName.ToString();
When i run it it gives an error at the first if statement saying there is no data for the column/row but there is in the database?
Aido222
Member
2 Points
8 Posts
Taking data from access database and putting it into multiple strings
Dec 02, 2012 01:53 PM|LINK
Hi, i have an application where the user will go to there details page where there first name and last name,, username and email will be cast back from the access database. How do i select them from the database and put them into their own strings?
Thanks
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Taking data from access database and putting it into multiple strings
Dec 03, 2012 03:45 AM|LINK
Hi,
I'm unclear about what you wanna achieve?
Aido222
Member
2 Points
8 Posts
Re: Taking data from access database and putting it into multiple strings
Dec 03, 2012 12:02 PM|LINK
Hi Decker,
When the page loads i wnat the usernames details to be taken from the database and added to textboxes on the page. This is the code i have tried to use.
string conn_string = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\user\Documents\College Work\3rd Year\web Programming\House100DataBase.accdb; Persist Security Info=False;"; using (OleDbConnection connection1 = new OleDbConnection(conn_string)) { //query selects users from user table where username equals inputted name string query1 = "select * from user_tbl where userName = @Username"; OleDbCommand mySQLCommand1 = new OleDbCommand(query1, connection1); OleDbDataReader myDataReader; mySQLCommand1.Parameters.AddWithValue("@UserName", txtbUserName.Text); connection1.Open(); mySQLCommand1.ExecuteNonQuery(); myDataReader = mySQLCommand1.ExecuteReader(); //if query returns data(If the username exists) do this.. string FName; string SName; string Email; if (myDataReader.GetValue(0) != DBNull.Value) { FName = myDataReader["fName"].ToString(); } else { FName = "e"; } if (myDataReader["sName"] != DBNull.Value) SName = myDataReader["sName"].ToString(); else { SName = "s"; } if (myDataReader["email"] != DBNull.Value) Email = myDataReader["email"].ToString(); else { Email = "e"; } txtbFirstName.Text = FName.ToString();When i run it it gives an error at the first if statement saying there is no data for the column/row but there is in the database?
hans_v
All-Star
35986 Points
6550 Posts
Re: Taking data from access database and putting it into multiple strings
Dec 03, 2012 01:07 PM|LINK
You need to call the Read Method of the reader to go the first record