madara pitigala
madara pitigala

Reputation: 11

How to update 2 tables in c#

I have 2 tables

  1. tbl_employer(Emp_num,Designation,Fname,Lname,Phone_no)
  2. tbl_system_users(Emp_num,User_name,Password)

those two tables are filled by using one c# form(employer.cs).Only few employers have permission to access the system called system users. there are 2 text boxes for username and password in employer.cs. username and password text box should not null to save data to tbl_system_users and if both text boxes are null, it means they are only employers and not system users. Emp_num of tbl_employer is auto increment field and a foreign key to tbl_system_users.

how can I update a employer detail?

public void update_employers(DTOUsers Users)
{
    SqlConnection con = new SqlConnection(ConnectionString);

    SqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "UPDATE tbl_employer SET Designation = '" + Users.Designation 
         + "',,Employer_first_name = '" + Users.EmpFirstName + "',Employer_last_name = '"
         + Users.EmpLastName + "',, Phone_Num = '" + Users.PhoneNum + "' WHERE Emp_Num ='" + Users.EmpNum + "'";
    con.Open();
    cmd.ExecuteNonQuery();

    if (Users.Password != "" && Users.UserName != "")
    {
        cmd.CommandText = "UPDATE tbl_system_users set User_name='" + Users.UserName + "',Password='" + Users.Password + "' where Emp_Num ='" + Users.EmpNum + "'";
       
        cmd.ExecuteNonQuery();
         
    }
    con.Close();

in employer.cs

private void btn_update_Click(object sender, EventArgs e)
{
    string emp_num = txt_emp_num.Text;
    string designation = cmb_designation.Text;                   
    string fname = txt_emp_fname.Text;
    string lname = txt_emp_lname.Text;
    string user_name = txt_user_name.Text;
    string pw = txt_pw.Text;
    string phno = txt_phn_num.Text;

    DTOUsers emp = new DTOUsers();

    emp.EmpNum = Convert.ToInt16(emp_num);
    emp.Designation = designation;                   
    emp.EmpFirstName = fname;
    emp.EmpLastName = lname;                    
    emp.UserName = user_name;
    emp.Password = strh.Encrypt(pw);
    emp.PhoneNum = phno;

    dbh.update_employers(emp);
 }

Upvotes: 1

Views: 95

Answers (1)

persian-theme
persian-theme

Reputation: 6638

public void update_employers(DTOUsers Users)
{
     bool firstStep = true;
     SqlConnection con = new SqlConnection(ConnectionString);
     SqlCommand cmd = new SqlCommand(con);


     cmd.CommandText = "UPDATE tbl_employer SET Designation = @Designation,Employer_first_name = @EmpFirstName,Employer_last_name = @EmpLastName,Phone_Num = @PhoneNum WHERE Emp_Num = @PhoneNum";

     //SqlDbType.VarChar Adjust according to the database values
     cmd.Parameters.Add("@Designation", SqlDbType.VarChar, 30).Value = Users.Designation;
     cmd.Parameters.Add("@EmpFirstName", SqlDbType.VarChar, 30).Value = Users.EmpFirstName;
     cmd.Parameters.Add("@EmpLastName", SqlDbType.VarChar, 30).Value = Users.EmpLastName;
     cmd.Parameters.Add("@PhoneNum", SqlDbType.VarChar, 30).Value = Users.PhoneNum;


    
     try
     {
         con.Open();
         cmd.ExecuteNonQuery();
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error: " + ex.Message);
         firstStep = false;
     }

     if (firstStep)
     {
         if (!string.IsNullOrEmpty(Users.Password) && !string.IsNullOrEmpty(Users.UserName))
         {
             cmd = new SqlCommand(con);
             cmd.CommandText = "UPDATE tbl_system_users set User_name = @UserName,Password = @Password where Emp_Num = @PhoneNum";
             cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 30).Value = Users.UserName;
             cmd.Parameters.Add("@Password", SqlDbType.VarChar, 30).Value = Users.Password;
             cmd.Parameters.Add("@PhoneNum", SqlDbType.VarChar, 30).Value = Users.PhoneNum;
             

             try
             {
                 cmd.ExecuteNonQuery();
             }
             catch (Exception ex)
             {
                 Console.WriteLine("Error: " + ex.Message);
             }
             finally
             {
                 con.Close();
             }
          }
     }
}

Upvotes: 1

Related Questions