locked
How to fetch details. RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    Nowadays I am working on a web application to extend some features. Thsi web application is already developed by someone.

    Here I want to update the user profile. I already have a class name userinfo where I have defined the properties of user. I found the method to validate login in the application's data access layer. See the code below.

     public Result<UserInfo> ValidateUserLogin(UserCredentials userCredentials)
            {
                Result<UserInfo> UserInfo = new Result<UserInfo>();
                try
                {
                    using (SqlConnection conn = new SqlConnection(ConnectionDataContext.SetConnection()))
                    {
                       // some code goes here
    
                        SqlDataReader dataReader = cmd.ExecuteReader();
    
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())
                            {
                                UserInfo.ResultObject.UserId = dataReader["User_Id"].ToString().ToInt64();
                                UserInfo.ResultObject.UserloginId = dataReader["UserLod_Id"].ToString().ToInt64();
                                UserInfo.ResultObject.UserStatus = dataReader["Status"].ToString().ToInt32();
                                UserInfo.ResultObject.UserRole = (EUserRole)dataReader["User_Type"].ToString().ToInt32();
                                UserInfo.ResultObject.UserName = dataReader["User_Name"].ToString().ScriptRemovel();
                               
                            }
                        }
                        dataReader.Close();
                        UserInfo.Status.ReturnMessage = cmd.Parameters["@p_UserStatus"].Value.ToString();
                        UserInfo.Status.ProcedureStatus = true;
                        dataReader.Dispose();
                        dataReader = null;
                        conn.Close();
                        cmd.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    UserInfo.Status.CallStatus = false;
                    ex.FillandWriteError(System.Reflection.MethodBase.GetCurrentMethod());
                }
                return UserInfo;
            }

    If user validate the login method, then how can I access the user's properties in my code file. Please let me know. Suppose I want to access User_Name data field in my code file to fill the text box so how I should to

    txtUser_Name.Text = ??

    Monday, February 13, 2017 6:13 AM

All replies

  • User-1838255255 posted

    Hi demoninside9,

    demoninside9

    SqlDataReader dataReader = cmd.ExecuteReader(); if (dataReader.HasRows) { while (dataReader.Read()) { UserInfo.ResultObject.UserId = dataReader["User_Id"].ToString().ToInt64(); UserInfo.ResultObject.UserloginId = dataReader["UserLod_Id"].ToString().ToInt64(); UserInfo.ResultObject.UserStatus = dataReader["Status"].ToString().ToInt32(); UserInfo.ResultObject.UserRole = (EUserRole)dataReader["User_Type"].ToString().ToInt32(); UserInfo.ResultObject.UserName = dataReader["User_Name"].ToString().ScriptRemovel(); } }

    According to your description, as far as I know, in your code, you have got user properties value, it returna all properties value of a user. 

    demoninside9

    Please let me know. Suppose I want to access User_Name data field in my code file to fill the text box so how I should to

    So I think you could assign a value to txtUser_Name.Text from the returned data directly.

    Like this: txtUser_Name.Text = dataReader["User_Name"].ToString().

    If my understanding is wrong, please correct me!

    Best Regards,

    Eric Du

    Tuesday, February 14, 2017 9:03 AM
  • User1052662409 posted

    txtUser_Name.Text = dataReader["User_Name"].ToString().

    This lines comes in data access layer which is not good if we use it in UI layer's code file.

    If we do so then what is the use of data access layer.

    I have data access layer file like below. there is a class UserDataContex

    namespace BEE.DataLayer.Common
    {
        public class UserDataContex
        {
            /// <summary>
            /// Used to validate username and passord
            /// </summary>
            /// <param name="userCredentials">User credetials</param>
            /// <returns></returns>
            public Result<UserInfo> ValidateUserLogin(UserCredentials userCredentials)
            {
                Result<UserInfo> UserInfo = new Result<UserInfo>();
                try
                {
                    using (SqlConnection conn = new SqlConnection(ConnectionDataContext.SetConnection()))
                    {
                        conn.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = conn;
                        cmd.CommandText = "[dbo].[sp_BEE_Validate_UserLogin]";
                        cmd.CommandType = CommandType.StoredProcedure;
                        SqlParameter[] SqlParmeter = new SqlParameter[4];
                        SqlParmeter[0] = new SqlParameter("@p_UserName", userCredentials.Username.ScriptRemovel());
                        SqlParmeter[0].SqlDbType = SqlDbType.VarChar;
                        SqlParmeter[1] = new SqlParameter("@p_UserPassword", userCredentials.Password.ScriptRemovel());
                        SqlParmeter[1].SqlDbType = SqlDbType.VarChar;
                        SqlParmeter[2] = new SqlParameter("@p_UserIPAddress", userCredentials.IpAddress.ScriptRemovel());
                        SqlParmeter[2].SqlDbType = SqlDbType.VarChar;
                        SqlParmeter[3] = new SqlParameter("@p_UserStatus", SqlDbType.Int);
                        SqlParmeter[3].Direction = ParameterDirection.Output;
                        SqlParmeter.LoadTo(cmd);
    
                        SqlDataReader dataReader = cmd.ExecuteReader();
    
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())
                            {
                                UserInfo.ResultObject.UserId = dataReader["User_Id"].ToString().ToInt64();
                                UserInfo.ResultObject.UserloginId = dataReader["UserLod_Id"].ToString().ToInt64();
                                UserInfo.ResultObject.UserStatus = dataReader["Status"].ToString().ToInt32();
                                UserInfo.ResultObject.UserRole = (EUserRole)dataReader["User_Type"].ToString().ToInt32();
                                UserInfo.ResultObject.UserName = dataReader["User_Name"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.Name = dataReader["Login_Name"].ToString().ScriptRemovel();
    
                                //edited by Gaurav Kumar
                                UserInfo.ResultObject.Address1 = dataReader["Address1"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.Address2 = dataReader["Address2"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.City = dataReader["City"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.Pincode = dataReader["Pincode"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.State = dataReader["State"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.FirstName = dataReader["First_Name"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.LastName = dataReader["Last_Name"].ToString().ScriptRemovel();
                                UserInfo.ResultObject.ContactNumber = dataReader["Contact_No"].ToString().ScriptRemovel();
                                //editing done
                                
                            }
                        }
                        dataReader.Close();
                        UserInfo.Status.ReturnMessage = cmd.Parameters["@p_UserStatus"].Value.ToString();
                        UserInfo.Status.ProcedureStatus = true;
                        dataReader.Dispose();
                        dataReader = null;
                        conn.Close();
                        cmd.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    UserInfo.Status.CallStatus = false;
                    ex.FillandWriteError(System.Reflection.MethodBase.GetCurrentMethod());
                }
                return UserInfo;
            }

    But I dont know how to use it to retrieve and doing other operation with database.

    Thanks

    Tuesday, February 14, 2017 9:51 AM