locked
REG loading details from db to textboxes Db values are passed through data transfer object RRS feed

  • Question

  • User-80362235 posted

    HI

    Need Explanation

     public UserDto GetUserByID(int UserId)
            {
                try
                {
                    UserDto getUser = null;
                    Command = DataBase.GetStoredProcCommand("GetUserByID");
                    DataBase.AddInParameter(Command, "@UserId", DbType.Int32, UserId);
                    DataSet resultDataSet = ExecuteDataSet();
                    if (resultDataSet != null && resultDataSet.Tables.Count > 0 && resultDataSet.Tables[0].Rows.Count > 0)
                    {
                        getUser = TranslateUserDataRowToEntity(resultDataSet.Tables[0].Rows[0]);
                    }
                    return getUser;
                }
                catch
                {
                    throw;
                }
            }

            private UserDto TranslateUserDataRowToEntity(DataRow dataRow)
            {
                UserDto getUser = new UserDto();
                getUser.ID = Convert.ToInt32(dataRow["id"]);
                getUser.Username = Convert.ToString(dataRow["username"]);
                getUser.FirstName = Convert.ToString(dataRow["first_name"]);
                getUser.LastName = Convert.ToString(dataRow["last_name"]);
                getUser.Password = Convert.ToString(dataRow["password"]);
                getUser.Email = Convert.ToString(dataRow["email"]);
             
                return getUser;
            }

    Tuesday, May 7, 2013 12:03 AM

All replies

  • User283571144 posted

    Hi Pepin,

    Need Explanation

    According to your description and codes, I couldn't understand your requirement clearly.

    Do you mean you want to know how this code works?

    I  think this codes is used to search user from database according to userID.

    But it has a Translate method translate the table's row value into a UserDto object.

    Let's see this codes:

    1.

       UserDto getUser = null;
                    Command = DataBase.GetStoredProcCommand("GetUserByID");
                    DataBase.AddInParameter(Command, "@UserId", DbType.Int32, UserId);
                    DataSet resultDataSet = ExecuteDataSet();

    This codes is used to search user form database and fill the result into a dataset.

    After this codes executing completely, the dataset will have value.

    For example:

    Result like below:

    id username first_name last_name password email
    1  aaa      bbb        ccc       ddd      eee@google.com

    2.

    if (resultDataSet != null && resultDataSet.Tables.Count > 0 && resultDataSet.Tables[0].Rows.Count > 0)
                    {
                        getUser = TranslateUserDataRowToEntity(resultDataSet.Tables[0].Rows[0]);
                    }

    This codes firstly adjust the dataset has the value, if the dataset has value then call TranslateUserDataRowToEntity method.

    Note:Since you just search one user, so the datatabls will just have one row.

    3.

    private UserDto TranslateUserDataRowToEntity(DataRow dataRow)
            {
                UserDto getUser = new UserDto();
                getUser.ID = Convert.ToInt32(dataRow["id"]);
                getUser.Username = Convert.ToString(dataRow["username"]);
                getUser.FirstName = Convert.ToString(dataRow["first_name"]);
                getUser.LastName = Convert.ToString(dataRow["last_name"]);
                getUser.Password = Convert.ToString(dataRow["password"]);
                getUser.Email = Convert.ToString(dataRow["email"]);
              
                return getUser;
            }

    This method is used to fill the datatable's row value into an obejct and will return the object.

    So after this code executing completely, the result will be.

    getUser object
    
    This object's property has the row's value.
    id = 1
    username = aaa
    ...

    At last, we will return this object to somewhere to execute other codes according to this object.

    Best Regards,

    Brando

    Tuesday, October 25, 2016 11:58 AM