Click here to Skip to main content
16,020,080 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;


namespace Restaurant_Application
{
    /// <summary>
    /// Interaction logic for Employe_page.xaml
    /// </summary>
    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;
            BindComboBox();
            buffercombo();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                SqlConnection con = new SqlConnection(ConnectionString);
                con.Open();
                int id = int.Parse(txtUserId.Text);
                string branch = cmboBranch.SelectedValuePath, emp_name = cmboSelEmp.Text, user_type = cmboSelUser.Text, user_name = txtUsername.Text, password = txtPassword.Password.ToString();
                SqlCommand cmd = new SqlCommand(" insert_user '" + id+"','"+user_name+"','"+password+"','"+user_type+"','"+branch+"','"+ emp_name + "'", 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);
            }

        }
      
        private void BindComboBox()
        {
            //try
            //{
            //    using (SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-89MGP64;Initial Catalog = new_restaurant_application;Integrated Security = True"))
            //    {
            //        conn.Open();
            //        SqlDataAdapter ProjectTableTableAdapter = new SqlDataAdapter("SELECT eid FROM add_employee", conn);
            //        DataSet ds = new DataSet();

            //        ProjectTableTableAdapter.Fill(ds, "add_employee");

            //        cmboSelUser.ItemsSource = ds.Tables["add_employee"].DefaultView;
            //        cmboSelUser.DisplayMemberPath = "name";
            //        cmboSelUser.SelectedValuePath = "eid";
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}
           

            SqlConnection con = new SqlConnection(ConnectionString);
            cmboSelUser.Items.Clear();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select privilege  from user_privilege";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            
            foreach (DataRow dr in dt.Rows)
            {
                cmboSelUser.Items.Add(dr["privilege"].ToString());
                cmboSelUser.ItemsSource = ((IListSource)dt).GetList();
                cmboSelUser.DisplayMemberPath = "privilege";
                cmboSelUser.SelectedValuePath = "id";
               
            }
         
            con.Close();
        }
        private void buffercombo()
        {
            SqlConnection con = new SqlConnection(ConnectionString);
            cmboSelEmp.Items.Clear();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select name  from add_employee";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                cmboSelEmp.Items.Add(dr["name"].ToString());
                cmboSelEmp.DisplayMemberPath = "name";
                cmboSelEmp.SelectedValuePath = "eid";

            }
            con.Close();
        }

    private void Button_Click1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }

}


What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;


namespace Restaurant_Application
{
    /// <summary>
    /// Interaction logic for Employe_page.xaml
    /// </summary>
    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;
            BindComboBox();
            buffercombo();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                SqlConnection con = new SqlConnection(ConnectionString);
                con.Open();
                int id = int.Parse(txtUserId.Text);
                string branch = cmboBranch.SelectedValuePath, emp_name = cmboSelEmp.Text, user_type = cmboSelUser.Text, user_name = txtUsername.Text, password = txtPassword.Password.ToString();
                SqlCommand cmd = new SqlCommand(" insert_user '" + id+"','"+user_name+"','"+password+"','"+user_type+"','"+branch+"','"+ emp_name + "'", 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);
            }

        }
      
        private void BindComboBox()
        {
            //try
            //{
            //    using (SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-89MGP64;Initial Catalog = new_restaurant_application;Integrated Security = True"))
            //    {
            //        conn.Open();
            //        SqlDataAdapter ProjectTableTableAdapter = new SqlDataAdapter("SELECT eid FROM add_employee", conn);
            //        DataSet ds = new DataSet();

            //        ProjectTableTableAdapter.Fill(ds, "add_employee");

            //        cmboSelUser.ItemsSource = ds.Tables["add_employee"].DefaultView;
            //        cmboSelUser.DisplayMemberPath = "name";
            //        cmboSelUser.SelectedValuePath = "eid";
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}
           

            SqlConnection con = new SqlConnection(ConnectionString);
            cmboSelUser.Items.Clear();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select privilege  from user_privilege";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            
            foreach (DataRow dr in dt.Rows)
            {
                cmboSelUser.Items.Add(dr["privilege"].ToString());
                cmboSelUser.ItemsSource = ((IListSource)dt).GetList();
                cmboSelUser.DisplayMemberPath = "privilege";
                cmboSelUser.SelectedValuePath = "id";
               
            }
         
            con.Close();
        }
        private void buffercombo()
        {
            SqlConnection con = new SqlConnection(ConnectionString);
            cmboSelEmp.Items.Clear();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select name  from add_employee";
            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                cmboSelEmp.Items.Add(dr["name"].ToString());
                cmboSelEmp.DisplayMemberPath = "name";
                cmboSelEmp.SelectedValuePath = "eid";

            }
            con.Close();
        }

    private void Button_Click1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

    }

}
Posted
Comments
CHill60 31-Aug-22 3:22am    
That's a lot of code and your sql is at risk of SQL Injection attack. What is your question, where is the problem in your code?
prashanth manoj 31-Aug-22 3:59am    
BindComboBox()
buffercombo()

how to data bring from databse to other table coloumn in c# wpf
prashanth manoj 31-Aug-22 4:15am    
stored procedure insert on button click,
and bind comcobox
how to retrieve data from other table
[no name] 31-Aug-22 12:26pm    
Setting the .ItemSource, repeatedly, in a for loop, is illogical.
Adding an item to the CB, then setting the .ItemSource, in a for loop, is also illogical.

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