locked
bind combobox to one table and save the values in other table in winform RRS feed

  • Question

  • Hello all , 

    I am very new to c# and visual studio

    In my winform application , i have two table as data source :

    1. Entry form

    2. Master rejection reason

    Now i need to fetch the rejection reason from one table and save the same on the entry form table ,Now when i bind these combo box to the rejreason table by creating a binding source , all the three combo boxes are updating with same value , and irrespective of the ID number where my entry is doing it is starting to save from the first row only , for example if Idon't enter the rej reason for first two rows and if i enter the rej reason for third row , the data is getting saved for 1st row only , kindly help me how to solve this , and kindly ask me if anything else is needed..

    the design can be seen in attached images:

    vendredi 11 décembre 2020 09:21

Réponses

  • Hi

     You can  past my code for working combobox fill and insert valus  to other tables.

    public partial class Frm_Add_Product : Form
        {

    BL.Cls_Products cPrd = new BL.Cls_Products();

            public Frm_Add_Product()
            {
                InitializeComponent();
                cmbCategories.DataSource = cPrd.Get_All_CATEGORIES();
                cmbCategories.DisplayMember = "Description_Cat";
                cmbCategories.ValueMember = "Id_Cat";
            }
    }

    //
     class Cls_Products
        {
            public DataTable Get_All_CATEGORIES()
            {
                DAL.DataAccessLaye DAL = new DAL.DataAccessLaye();
                
                //DAL.open();

                DataTable Dt = new DataTable();
                Dt = DAL.SelectData("dbo.Get_All_Categories", null);
                DAL.close();
                return Dt;
            }
    }

    class DataAccessLaye
        {
            static SqlConnection sqlconnection;
            // The connection object.
            public DataAccessLaye()
            {
                string myconn;
                myconn = PL.Frm_Main.srvnamecnn;
                
                sqlconnection = new SqlConnection("Server=" + myconn + ";DataBase=Product_DB;integrated security=true");

            }
            //Method to open the connection.
            public void open()
            {
                if (sqlconnection.State != ConnectionState.Open)
                {
                    sqlconnection.Open();
                }
            }
            //Method to close connection
            public void close()
            {
                if (sqlconnection.State != ConnectionState.Closed)
                {
                    sqlconnection.Close();
                }
            }
            //Method to read data from database
            public DataTable SelectData(string stored_procedure, SqlParameter[] param)
            {
                SqlCommand sqlcmd = new SqlCommand(stored_procedure,sqlconnection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Connection = sqlconnection;
                if (param != null)
                {
                    for (int i = 0; i < param.Length; i++)
                    {
                        sqlcmd.Parameters.Add(param[i]);
                    }
                }
                        SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
                        DataTable dt=new DataTable();
                        da.Fill(dt);
                    return dt;
              }

     
    }

    Best Regards.

    hakim

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

    vendredi 11 décembre 2020 09:42
  • Hi

    you can call one function and fill three combobox at one time

    cmbReason1.DataSource = cPrd.Get_All_CATEGORIES();

    cmbReason2.DataSource = cPrd.Get_All_CATEGORIES();

    cmbReason3.DataSource = cPrd.Get_All_CATEGORIES();

    Messagebox.show(cmbReason1.ValueMember+":"+cmbReason2.ValueMember+":"+cmbReason3.ValueMember);

    Thanks.

    reason

    vendredi 11 décembre 2020 11:21

Toutes les réponses

  • Hi

     You can  past my code for working combobox fill and insert valus  to other tables.

    public partial class Frm_Add_Product : Form
        {

    BL.Cls_Products cPrd = new BL.Cls_Products();

            public Frm_Add_Product()
            {
                InitializeComponent();
                cmbCategories.DataSource = cPrd.Get_All_CATEGORIES();
                cmbCategories.DisplayMember = "Description_Cat";
                cmbCategories.ValueMember = "Id_Cat";
            }
    }

    //
     class Cls_Products
        {
            public DataTable Get_All_CATEGORIES()
            {
                DAL.DataAccessLaye DAL = new DAL.DataAccessLaye();
                
                //DAL.open();

                DataTable Dt = new DataTable();
                Dt = DAL.SelectData("dbo.Get_All_Categories", null);
                DAL.close();
                return Dt;
            }
    }

    class DataAccessLaye
        {
            static SqlConnection sqlconnection;
            // The connection object.
            public DataAccessLaye()
            {
                string myconn;
                myconn = PL.Frm_Main.srvnamecnn;
                
                sqlconnection = new SqlConnection("Server=" + myconn + ";DataBase=Product_DB;integrated security=true");

            }
            //Method to open the connection.
            public void open()
            {
                if (sqlconnection.State != ConnectionState.Open)
                {
                    sqlconnection.Open();
                }
            }
            //Method to close connection
            public void close()
            {
                if (sqlconnection.State != ConnectionState.Closed)
                {
                    sqlconnection.Close();
                }
            }
            //Method to read data from database
            public DataTable SelectData(string stored_procedure, SqlParameter[] param)
            {
                SqlCommand sqlcmd = new SqlCommand(stored_procedure,sqlconnection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Connection = sqlconnection;
                if (param != null)
                {
                    for (int i = 0; i < param.Length; i++)
                    {
                        sqlcmd.Parameters.Add(param[i]);
                    }
                }
                        SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
                        DataTable dt=new DataTable();
                        da.Fill(dt);
                    return dt;
              }

     
    }

    Best Regards.

    hakim

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

    vendredi 11 décembre 2020 09:42
  • Hi thanks for the code , but my problem is i have to populate three combo boxes from same column of the table , could you lease show me by modifying your code?
    vendredi 11 décembre 2020 10:04
  • Hi

    you can call one function and fill three combobox at one time

    cmbReason1.DataSource = cPrd.Get_All_CATEGORIES();

    cmbReason2.DataSource = cPrd.Get_All_CATEGORIES();

    cmbReason3.DataSource = cPrd.Get_All_CATEGORIES();

    Messagebox.show(cmbReason1.ValueMember+":"+cmbReason2.ValueMember+":"+cmbReason3.ValueMember);

    Thanks.

    reason

    vendredi 11 décembre 2020 11:21