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();
}
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();
}
con.Close();
}