Click here to Skip to main content
15,868,040 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public partial class MainWindow : Window
   {
       public MainWindow()
       {
           InitializeComponent();
           WindowState = WindowState.Maximized;
           WindowStyle = WindowStyle.None;
       }




       private void Button_Click_1(object sender, RoutedEventArgs e)
       {
           textbox.Clear();
           txtpassword.Clear();
           combobox.SelectedItem = null;
       }

       private void Button_Click(object sender, RoutedEventArgs e)
       {


           if (textbox.Text == "")
           {
               MessageBox.Show("Please enter username", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
               textbox.Focus();
           }
           else if (txtpassword.Password.ToString() == "")
           {
               MessageBox.Show("Please enter the password", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
               txtpassword.Focus();

           }
           else if (combobox.Text == "")
           {
               MessageBox.Show("Please select usertype", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
               combobox.Focus();


           }

           else if (textbox.Text != "admin")
           {
               MessageBox.Show("Please check username", "Alert Message", MessageBoxButton.OK, MessageBoxImage.Information);
           }
           else if (txtpassword.Password.ToString() != "admin")
           {
               MessageBox.Show("Please check the password", "Alert Message", MessageBoxButton.OK, MessageBoxImage.Information);
           }
           else if (combobox.Text != "Admin")
           {
               MessageBox.Show("Please check the usertype", "Alert Message", MessageBoxButton.OK, MessageBoxImage.Information);
           }

           else
           {
               Employe_page m = new Employe_page();
               m.ShowDialog();


           }

       }











public partial class Employe_page : Window
   {
       string ConnectionString = @"Data Source=DESKTOP-89MGP64;Initial Catalog=new_restaurant_application;Integrated Security=True";
       public Employe_page()
       {
           InitializeComponent();
           WindowState = WindowState.Maximized;
           WindowStyle = WindowStyle.None;
       }

       private void Button_Click(object sender, RoutedEventArgs e)
       {

           try
           {
               SqlConnection con = new SqlConnection(ConnectionString);
               con.Open();
               SqlCommand cmd = new SqlCommand("user_insert", con);
               //cmd.CommandType = CommandType.StoredProcedure;
               //cmd.Parameters.AddWithValue("user_id", txtUserId.Text);
               //cmd.Parameters.AddWithValue("emp_id",  cmboSelEmp.SelectedValue);
               //cmd.Parameters.AddWithValue("emp_name",  cmboSelEmp.Text);
               //cmd.Parameters.AddWithValue("user_type",  cmboSelUser.Text);
               //cmd.Parameters.AddWithValue("privilege",  cmboSelUser.SelectedValue);
               //cmd.Parameters.AddWithValue("user_name",  txtUsername.Text);
               //cmd.Parameters.AddWithValue("password",  txtPassword.Password.ToString());
               //cmd.Parameters.AddWithValue("branch", cmboBranch.Text);
               //cmd.ExecuteNonQuery();
               //con.Close();

               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("user_id", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtUserId.Text) ? DBNull.Value : (object)txtUserId.Text;
               cmd.Parameters.AddWithValue("emp_id", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelEmp.Text) ? DBNull.Value : (object)cmboSelEmp.SelectedValue;
               cmd.Parameters.AddWithValue("emp_name", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelEmp.Text) ? DBNull.Value : (object)cmboSelEmp.Text;
               cmd.Parameters.AddWithValue("user_type", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelUser.Text) ? DBNull.Value : (object)cmboSelUser.Text;
               cmd.Parameters.AddWithValue("privilege", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelUser.Text) ? DBNull.Value : (object)cmboSelUser.SelectedValue;
               cmd.Parameters.AddWithValue("user_name", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtUsername.Text) ? DBNull.Value : (object)txtUsername.Text;
               cmd.Parameters.AddWithValue("password", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtPassword.Password) ? DBNull.Value : (object)txtPassword.Password;
               cmd.Parameters.AddWithValue("branch", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboBranch.Text) ? DBNull.Value : (object)cmboBranch.Text;
               cmd.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("Saved");


           }
           catch ( SqlException ex)
           {

               MessageBox.Show(ex.Message);
           }

       }
   }


What I have tried:

public partial class MainWindow : Window
   {
       public MainWindow()
       {
           InitializeComponent();
           WindowState = WindowState.Maximized;
           WindowStyle = WindowStyle.None;
       }




       private void Button_Click_1(object sender, RoutedEventArgs e)
       {
           textbox.Clear();
           txtpassword.Clear();
           combobox.SelectedItem = null;
       }

       private void Button_Click(object sender, RoutedEventArgs e)
       {


           if (textbox.Text == "")
           {
               MessageBox.Show("Please enter username", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
               textbox.Focus();
           }
           else if (txtpassword.Password.ToString() == "")
           {
               MessageBox.Show("Please enter the password", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
               txtpassword.Focus();

           }
           else if (combobox.Text == "")
           {
               MessageBox.Show("Please select usertype", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
               combobox.Focus();


           }

           else if (textbox.Text != "admin")
           {
               MessageBox.Show("Please check username", "Alert Message", MessageBoxButton.OK, MessageBoxImage.Information);
           }
           else if (txtpassword.Password.ToString() != "admin")
           {
               MessageBox.Show("Please check the password", "Alert Message", MessageBoxButton.OK, MessageBoxImage.Information);
           }
           else if (combobox.Text != "Admin")
           {
               MessageBox.Show("Please check the usertype", "Alert Message", MessageBoxButton.OK, MessageBoxImage.Information);
           }

           else
           {
               Employe_page m = new Employe_page();
               m.ShowDialog();


           }

       }








public partial class Employe_page : Window
   {
       string ConnectionString = @"Data Source=DESKTOP-89MGP64;Initial Catalog=new_restaurant_application;Integrated Security=True";
       public Employe_page()
       {
           InitializeComponent();
           WindowState = WindowState.Maximized;
           WindowStyle = WindowStyle.None;
       }

       private void Button_Click(object sender, RoutedEventArgs e)
       {

           try
           {
               SqlConnection con = new SqlConnection(ConnectionString);
               con.Open();
               SqlCommand cmd = new SqlCommand("user_insert", con);
               //cmd.CommandType = CommandType.StoredProcedure;
               //cmd.Parameters.AddWithValue("user_id", txtUserId.Text);
               //cmd.Parameters.AddWithValue("emp_id",  cmboSelEmp.SelectedValue);
               //cmd.Parameters.AddWithValue("emp_name",  cmboSelEmp.Text);
               //cmd.Parameters.AddWithValue("user_type",  cmboSelUser.Text);
               //cmd.Parameters.AddWithValue("privilege",  cmboSelUser.SelectedValue);
               //cmd.Parameters.AddWithValue("user_name",  txtUsername.Text);
               //cmd.Parameters.AddWithValue("password",  txtPassword.Password.ToString());
               //cmd.Parameters.AddWithValue("branch", cmboBranch.Text);
               //cmd.ExecuteNonQuery();
               //con.Close();

               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("user_id", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtUserId.Text) ? DBNull.Value : (object)txtUserId.Text;
               cmd.Parameters.AddWithValue("emp_id", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelEmp.Text) ? DBNull.Value : (object)cmboSelEmp.SelectedValue;
               cmd.Parameters.AddWithValue("emp_name", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelEmp.Text) ? DBNull.Value : (object)cmboSelEmp.Text;
               cmd.Parameters.AddWithValue("user_type", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelUser.Text) ? DBNull.Value : (object)cmboSelUser.Text;
               cmd.Parameters.AddWithValue("privilege", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelUser.Text) ? DBNull.Value : (object)cmboSelUser.SelectedValue;
               cmd.Parameters.AddWithValue("user_name", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtUsername.Text) ? DBNull.Value : (object)txtUsername.Text;
               cmd.Parameters.AddWithValue("password", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtPassword.Password) ? DBNull.Value : (object)txtPassword.Password;
               cmd.Parameters.AddWithValue("branch", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboBranch.Text) ? DBNull.Value : (object)cmboBranch.Text;
               cmd.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("Saved");


           }
           catch ( SqlException ex)
           {

               MessageBox.Show(ex.Message);
           }

       }
   }
Posted
Updated 28-Aug-22 21:47pm

1 solution

Somewhere in this code:
cmd.Parameters.AddWithValue("user_id", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtUserId.Text) ? DBNull.Value : (object)txtUserId.Text;
cmd.Parameters.AddWithValue("emp_id", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelEmp.Text) ? DBNull.Value : (object)cmboSelEmp.SelectedValue;
cmd.Parameters.AddWithValue("emp_name", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelEmp.Text) ? DBNull.Value : (object)cmboSelEmp.Text;
cmd.Parameters.AddWithValue("user_type", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelUser.Text) ? DBNull.Value : (object)cmboSelUser.Text;
cmd.Parameters.AddWithValue("privilege", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboSelUser.Text) ? DBNull.Value : (object)cmboSelUser.SelectedValue;
cmd.Parameters.AddWithValue("user_name", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtUsername.Text) ? DBNull.Value : (object)txtUsername.Text;
cmd.Parameters.AddWithValue("password", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(txtPassword.Password) ? DBNull.Value : (object)txtPassword.Password;
cmd.Parameters.AddWithValue("branch", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(cmboBranch.Text) ? DBNull.Value : (object)cmboBranch.Text;
You are passing a Control instead of a string - and since a TextBox doesn't have a Password property, our best guess is that txtPassword is not a "standard" textbox, and that the Password property returns something other than a string.

But we have no access to that, so you need to use the debugger to see exactly what it contains.

But that is a pretty trivial problem compared to what you seem to be doing: trying to store passwqrds in clear text. You should never do that - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
Share this answer
 
Comments
prashanth manoj 29-Aug-22 3:48am    
did not work...
give me other examples..
OriginalGriff 29-Aug-22 4:20am    
"it did not work" is one of the error reports we get quite often, and it's always completely useless - it tells us nothing about your problem other than "I have a problem" and we knew that because you are asking a question!

What did it do that you didn't expect, or not do that you did?
When did it do it?
Are there any error messages?
What did you do to make it do that?
What have you tried to do to find out why?
What were the results?

These are all questions we need an answer to - or we can't help you at all!

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