locked
Taking data from access database and putting it into multiple strings RRS feed

  • Question

  • User-1131105293 posted

    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

    Sunday, December 2, 2012 9:53 AM

Answers

  • User-1199946673 posted

    You need to call the Read Method of the reader to go the first record

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 3, 2012 9:07 AM

All replies

  • User3866881 posted

    Hi,

    I'm unclear about what you wanna achieve?

    Sunday, December 2, 2012 11:45 PM
  • User-1131105293 posted

    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.

     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?

    Monday, December 3, 2012 8:02 AM
  • User-1199946673 posted

    You need to call the Read Method of the reader to go the first record

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 3, 2012 9:07 AM