Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
string ConnectionString = @"Data Source=DESKTOP-89MGP64;Initial Catalog = new_restaurant_application; Integrated Security = True";

      public Employe_page()
      {
          InitializeComponent();
          cmbdisp();
          BindComboBox();
          CmboSelEmp_SelectionChanged_1();
      }


      public void cmbdisp()
      {
          SqlConnection con = new SqlConnection(ConnectionString);
          cmboSelEmp.Items.Clear();
          con.Open();
          SqlCommand cmd = new SqlCommand();
          cmd = con.CreateCommand();
          cmd.CommandText = "select eid,name from add_employee";
          cmd.ExecuteNonQuery();
          DataTable dt = new DataTable();
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          da.Fill(dt);

          for (int i = 0; i < dt.Rows.Count; i++)
          {
              cmboSelEmp.Items.Add(dt.Rows[i]["name"].ToString());
              cmboSelEmp.SelectedValuePath = dt.Rows[i]["eid"].ToString();
          }

      }

      private void BindComboBox()
      {
          SqlConnection con = new SqlConnection(ConnectionString);
          con.Open();
          SqlCommand cmd = new SqlCommand();
          cmd = con.CreateCommand();
          cmd.CommandType = CommandType.Text;
          cmd.CommandText = "select id,privilege from user_privilege";
          cmd.ExecuteNonQuery();
          DataTable dt = new DataTable();
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          da.Fill(dt);
          for (int i = 0; i < dt.Rows.Count; i++)
          {
              cmboSelUser.Items.Add(dt.Rows[i]["privilege"].ToString());
              cmboSelUser.SelectedValuePath = dt.Rows[i]["id"].ToString();
              //cmboSelUser.DisplayMemberPath = dt.Rows[i]["privilege"].ToString();
          }
          con.Close();
      }


      private void Button_Click_save(object sender, RoutedEventArgs e)
      {
          if (cmboBranch.Text == "")
          {
              MessageBox.Show("Please Select branch", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
              cmboBranch.Focus();
          }
          else if (cmboSelEmp.Text == "")
          {
              MessageBox.Show("Please select employee name", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
              cmboSelEmp.Focus();
          }
          else if (cmboSelUser.Text == "")
          {
              MessageBox.Show("Please Select user type", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
              cmboSelUser.Focus();
          }
          else if (txtUsername.Text == "")
          {
              MessageBox.Show("Please enter the user name", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
              txtUsername.Focus();
          }
          else if (txtPassword.Password.ToString() == "")
          {
              MessageBox.Show("Please enter the password", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
              txtPassword.Focus();
          }
          else
          {
              try
              {
                  SqlConnection con = new SqlConnection(ConnectionString);
                  con.Open();
                  SqlCommand cmd = new SqlCommand("insert_user'" + txtUserId.Text + "','" + txtUsername.Text + "','" + txtPassword.Password.ToString() + "','" +cmboSelUser.SelectedValuePath.ToString() + "','" + cmboSelUser.Text + "','" + cmboBranch.Text + "','" + cmboSelEmp.SelectedValuePath + "','" + cmboSelEmp.Text + "'", con);
                  cmd.ExecuteNonQuery();
                  con.Close();
                  MessageBox.Show("Saved");
              }
              catch (SqlException ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
      }

      private void CmboSelEmp_SelectionChanged_1()
      {
      }

      private void Button_Click_clear(object sender, RoutedEventArgs e)
      {
          cmboBranch.SelectedItem = null;
          cmboSelEmp.SelectedItem = null;
          cmboSelUser.SelectedItem = null;
          txtUserId.Clear();
          txtUsername.Clear();
          txtPassword.Clear();
      }


What I have tried:

public void cmbdisp()
       {
           SqlConnection con = new SqlConnection(ConnectionString);
           cmboSelEmp.Items.Clear();
           con.Open();
           SqlCommand cmd = new SqlCommand();
           cmd = con.CreateCommand();
           cmd.CommandText = "select eid,name from add_employee";
           cmd.ExecuteNonQuery();
           DataTable dt = new DataTable();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);

           for (int i = 0; i < dt.Rows.Count; i++)
           {
               cmboSelEmp.Items.Add(dt.Rows[i]["name"].ToString());
               cmboSelEmp.SelectedValuePath = dt.Rows[i]["eid"].ToString();
           }

       }

       private void BindComboBox()
       {
           SqlConnection con = new SqlConnection(ConnectionString);
           con.Open();
           SqlCommand cmd = new SqlCommand();
           cmd = con.CreateCommand();
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "select id,privilege from user_privilege";
           cmd.ExecuteNonQuery();
           DataTable dt = new DataTable();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt);
           for (int i = 0; i < dt.Rows.Count; i++)
           {
               cmboSelUser.Items.Add(dt.Rows[i]["privilege"].ToString());
               cmboSelUser.SelectedValuePath = dt.Rows[i]["id"].ToString();
               //cmboSelUser.DisplayMemberPath = dt.Rows[i]["privilege"].ToString();
           }
           con.Close();
       }
Posted
Updated 7-Sep-22 8:55am
Comments
[no name] 6-Sep-22 10:53am    
SelectedValuePath is a "one time" assignment of a "property name"; not some value you manipulate. Unless your CB has "data objects", versus simple strings, its use is pointless; and perhaps unpredictable.

https://www.codeproject.com/Articles/671544/Understanding-SelectedValue-SelectedValuePath-Sele

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900