简体   繁体   中英

How to save multiple values from various controls in sql server database?

I am designing windows application using 3-tier architecture. One of my forms contain dynamically generated controls. My question is that How can I save multiple values from controls in my database. I have used return statement but since it returns only one value my database stored only one value from each of the control. Basically I am creating BILL FORM.

I have created 8 methods one for each control and pass their values in business logic layer's function, but instead of saving multiple values it saved only one value of each control as mentioned above. I tried to use Tuples also but then it gave me "CONVERSION EXCEPTION"

One of my 8 methods is:

 public string combo_box_1()
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is ComboBox)
                {
                    if (ctrl.Name.Equals("combo_box_1"))
                    {
                        string main_category = ctrl.Text;
                        string[] arr1 = { main_category };
                        foreach (string alph in arr1)
                        {
                            MessageBox.Show(alph);
                            return alph;
                        }
                        return main_category;

                    }
                }
            }
            return null;
        }

It's Tuple version:

public Tuple<string> combo_box_1()
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl is ComboBox)
                {
                    if (ctrl.Name.Equals("combo_box_1"))
                    {
                        string main_category = ctrl.Text;
                        Tuple<string> txt2 = new Tuple<string>(main_category);
                        return txt2;
                    }
                }
            }
            return null;
        }

My business layer function(Tuple Version):

public bool save_bill(Tuple<string> CB1, Tuple<string> CB2, Tuple<string> CB3, Tuple<string> CB4, Tuple<string> NUD1, Tuple<string> CB5, Tuple<string> TB1, Tuple<string> TB2)
        {
            try
            {
                DAL obj = new DAL();
                obj.OpenConnection();
                obj.LoadSpParameters("save_bill", CB1, CB2, CB3, CB4, NUD1, CB5, TB1, TB2);
                obj.ExecuteQuery();
                obj.UnLoadSpParameters();
                obj.CloseConnection();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

Programming behind my save button(Tuple Version):

private void save_button_Click(object sender, EventArgs e)
        {
            BLL obj = new BLL();
            bool obj2 = obj.save_bill(combo_box_1(), combo_box_2(), combo_box_3(), combo_box_4(), numeric_up_down_1(), combo_box_5(), txt_box_1(), txt_box_2());
            if (obj2 == true)
            {
                MessageBox.Show("bill saved");
            }

            else
            {
                MessageBox.Show("bill cannot be saved");
            }

        }

I must admit I'm having trouble understanding your code but I think this could help: change save_bill method to accept string parameters, no need for Tuples

public bool save_bill(Tuple<string> CB1, Tuple<string> CB2, Tuple<string> CB3
    , Tuple<string> CB4, Tuple<string> NUD1, Tuple<string> CB5, Tuple<string> TB1
    , Tuple<string> TB2)

to

public bool save_bill(string CB1, string CB2, string CB3, string CB4
, string NUD1, string CB5, string TB1, string TB2)

and instead of using 8 (strange) methods for each control

bool obj2 = obj.save_bill(combo_box_1(), combo_box_2(), combo_box_3()
, combo_box_4(), numeric_up_down_1(), combo_box_5(), txt_box_1(), txt_box_2());

use this and access their values directly

bool obj2 = obj.save_bill(combo_box_1.Text, combo_box_2.Text
, combo_box_3.Text, combo_box_4.Text, numeric_up_down_1.Value.ToString()
, combo_box_5.Text, txt_box_1.Text, txt_box_2.Text);

There are some questions like do you really need ComboBox.Text instead of SelectedID or if the controls are dynamically created then you shouldn't reference them by their name and so on, but hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM