// Button with actions to find user's name (not legal name) protected void btnFind_Click(object sender, EventArgs e) { // Instance of object, dsAccounts dsAccounts dsFind; // My path is via App_Data. string tempPath = Server.MapPath("~/App_Data/Users.accdb"); clsDataLayer dataLayerObj = new clsDataLayer(tempPath); // Try-catch statement to check for user's info try { // Reference to the usernames dsFind = dataLayerObj.FindUser(Username.Text); // Another check to make sure data is present if (dsFind.tblUsers.Rows.Count > 0) { // The following variables should be there from the sample data txtUsername.Text = dsFind.tblUsers[0].Username; txtCity.Text = dsFind.tblUsers[0].City; txtState.Text = dsFind.tblUsers[0].State; txtFavLang.Text = dsFind.tblUsers[0].FavLang; txtWorstLang.Text = dsFind.tblUsers[0].WorstLang; txtLastProgram.Text = dsFind.tblUsers[0].LastProgram; // Default error text Master.UserFeedBack.Text = "Record Found"; } else { // Error text as a complement to above Master.UserFeedBack.Text = "No records were found!"; } } catch (Exception error) { // Hopefuly the stack can trace what went wrong. string message = "Something went wrong - "; Master.UserFeedBack.Text = message + error.Message; } }
// find user by last name protected void btnFindUser_Click(object sender, EventArgs e) { dsProgramaholics dsFindLastName; // database path string tempPath = Server.MapPath("~/App_Data/dsProgramaholics.mdb"); clsDataLayer dataLayerObj = new clsDataLayer(tempPath); // try comparing try { dsFindLastName = dataLayerObj.FindUser(txtLastName.Text); // if matching record if (dsFindLastName.tblProgramaholics.Rows.Count > 0) { // fill with info txtFirstName.Text = dsFindLastName.tblProgramaholics[0].FName; txtLastName.Text = dsFindLastName.tblProgramaholics[0].LName; txtStreet.Text = dsFindLastName.tblProgramaholics[0].Street; txtCity.Text = dsFindLastName.tblProgramaholics[0].City; txtState.Text = dsFindLastName.tblProgramaholics[0].State; txtUsername.Text = dsFindLastName.tblProgramaholics[0].Email; txtPassword.Text = dsFindLastName.tblProgramaholics[0].Password; txtFavLanguage.Text = dsFindLastName.tblProgramaholics[0].FavLang; txtLeastFavLang.Text = dsFindLastName.tblProgramaholics[0].LeastFavLang; txtDateCompleted.Text = dsFindLastName.tblProgramaholics[0].DateComplete.ToString(); userID.Text = dsFindLastName.tblProgramaholics[0].ID.ToString(); Master.UserFeedBack.Text = "Record Found!"; }//end if else { Master.UserFeedBack.Text = "No Records Found!"; } //end else } // end try catch (Exception error) { // error message string message = "Something went wrong..."; Master.UserFeedBack.Text = message + " " + error.Message; }// end catch }
/// findUser method // find user in database public dsProgramaholics FindUser(string LastName) { // find user dsProgramaholics dsFoundUser = myDataLayer.FindUser(LastName); // go through database fields if (dsFoundUser.tblUserDetails.Rows.Count > 0) { // go through records System.Data.DataRow userRecord = dsFoundUser.tblUserDetails.Rows[0]; if (userRecord["FirstName"] == DBNull.Value) { userRecord["FirstName"] = string.Empty; } if (userRecord["LastName"] == DBNull.Value) { userRecord["LastName"] = string.Empty; } if (userRecord["Username"] == DBNull.Value) { userRecord["Username"] = string.Empty; } if (userRecord["Password"] == DBNull.Value) { userRecord["Password"] = string.Empty; } if (userRecord["Street"] == DBNull.Value) { userRecord["Street"] = string.Empty; } if (userRecord["City"] == DBNull.Value) { userRecord["City"] = string.Empty; } if (userRecord["State"] == DBNull.Value) { userRecord["State"] = string.Empty; } } return(dsFoundUser); }
/// find user method // finds the user by their last name. protected void btnFind_Click(object sender, EventArgs e) { // find user in database dsProgramaholics dsFindLastName; // data path string tempPath = Server.MapPath("~/App_Data/Programaholics.mdb"); clsDataLayer dataLayerObj = new clsDataLayer(tempPath); try { dsFindLastName = dataLayerObj.FindUser(txtLastName.Text); // if you find user return success message if (dsFindLastName.tblUserDetails.Rows.Count > 0) { // searching rows txtFirstName.Text = dsFindLastName.tblUserDetails[0].FName; txtLastName.Text = dsFindLastName.tblUserDetails[0].LName; txtEmail.Text = dsFindLastName.tblUserDetails[0].Email; txtPassword.Text = dsFindLastName.tblUserDetails[0].Pwd; txtStreet.Text = dsFindLastName.tblUserDetails[0].Street; txtCity.Text = dsFindLastName.tblUserDetails[0].City; txtState.Text = dsFindLastName.tblUserDetails[0].State; Master.UserFeedBack.Text = "Record Found! "; } else { Master.UserFeedBack.Text = "No Records were found! "; } }// end try catch (Exception error) { // unsuccessful message string message = "Something went wrong -- "; Master.UserFeedBack.Text = message + error.Message; } }