Rodel Barbin

Rodel Barbin

  • 1.6k
  • 108
  • 13.8k

Access Textbox value of login window from ViewModel

Sep 14 2023 7:52 AM

Hi Sir, Mam,

In WPF (.Net Framework) I have login account with username and password textbox.

after login it will go to Admin Window where there are Menus like Equipment.

When accessing the Equipment Window thru Menus I want to access the user_id

from ViewModel of Equipment. Unfortunately it is diplaying UserID property as blank.

Example: from login window

private string user_id;

public string UserID
{
    get
    {
        return user_id;
    }
    set
    {
        user_id = value;
    }

}

 private void btn_login_Click(object sender, RoutedEventArgs e)
 {
     string conn = "Data Source = BARBINFAMILY; Initial Catalog = Kitchen; Integrated Security = True";
     SqlConnection sqlConnection = new SqlConnection(conn);
     string commandText = "select * from [user] where user_name = @user_name and password = @password";

     SqlCommand command = new SqlCommand(commandText, sqlConnection);

     // Settings.  
     command.CommandType = CommandType.Text;
     command.Parameters.Add("@user_name", SqlDbType.VarChar);
     command.Parameters["@user_name"].Value = user_name_txt.Text;
     command.Parameters.Add("@password", SqlDbType.VarChar);
     command.Parameters["@password"].Value = password_txt.Text;

     try
     {
         sqlConnection.Open();
         int totalCount = 0;

         SqlDataReader reader = command.ExecuteReader();
         while (reader.Read())
         {
             user_id = reader["user_id"].ToString();
             totalCount++;
         }
         Console.WriteLine("RowsAffected: {0}", totalCount);
         if (totalCount > 0)
         {
             Admin objAdmin = new Admin();
             objAdmin.Show();
            
         }
         else
         {
             MessageBox.Show("Invalid Credentials");
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         MessageBox.Show(ex.Message);
     }

Example of Equipment View Model

 

public class EquipmentMaintenanceViewModel : ViewModelBase
{
    private ICommand _saveCommand;
    private ICommand _resetCommand;
    private ICommand _editCommand;
    private ICommand _deleteCommand;
    private EquipmentMaintenanceRepository _repository;
    private equipment _equipmentEntity = null;
    public EquipmentMaintenanceRecord EquipmentMaintenanceRecord { get; set; }
    public EquipmentMaintenanceEntities EquipmentMaintenanceEntities { get; set; }

 

    public string userid = null;

 public void SaveData()
 {
     _equipmentEntity.equipment_id = EquipmentMaintenanceRecord.Equipment_id;
     _equipmentEntity.serial_number = EquipmentMaintenanceRecord.Serial_number;
     _equipmentEntity.description = EquipmentMaintenanceRecord.Description;
     _equipmentEntity.condition = EquipmentMaintenanceRecord.Condition;

     if (EquipmentMaintenanceRecord != null)
     {
         MessageBox.Show(EquipmentMaintenanceRecord.Equipment_id.ToString());

         try
         {
             if (EquipmentMaintenanceRecord.Equipment_id == 0)
             {
                 Login objLogin = new Login();
                 string UserID = objLogin.UserID ;
                 MessageBox.Show(UserID);
                 //save

                 _repository.AddEquipment(_equipmentEntity);
                 MessageBox.Show("New record successfully saved.");
             }
             else
             {
                 _repository.UpdateEquipment(_equipmentEntity);
                 MessageBox.Show("Record successfully updated.");
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show("Error occured while saving. " + ex.InnerException);
         }
         finally
         {
             GetAll();
             ResetData();
         }
     }
 }

Hoping you can help me to resolved this.

Thank you.

Rodel

 


Answers (2)