samedi 2 mai 2015

getting values from checkboxes and save all data to Sql server in one column

I am trying to develop a windows form application using Visual studio 2013 and Sql server 2014.

My question is about getting values of chechboxes which are checked and save all data to one row in sql server.

Example:

MY DATABASE : TBL_EXAMPLE ID int PK not null, NAME nvarchar(max) null

I have 3 checkboxes in my program:

checkbox1 - name : chkbx1 checked checkbox2 - name : chkbx2 checked checkbox3 - name : chkbx3 not checked

I need to add values of checkboxes to one column called name in sqlserver

after adding sql server content of NAME should be like this:

 NAME
 checkbox1 checkbox2

I tried to write like this:

foreach (Control cont in this.groupBox1.Controls) { if (cont is CheckBox)

            {

                if (((CheckBox)cont).Checked == true)
                {
                    sqlcon.Open();
                    SqlCommand sqlcmd = new SqlCommand("INSERT INTO TBL_EXAMPLE VALUES ( '" + ((CheckBox)cont).Text.ToString() + "')", sqlcon);
                    sqlcmd.ExecuteNonQuery();
                    sqlcon.Close();
                    MessageBox.Show("OK");
                }


            }
        }

It gives me result like this:

ID       NAME
1         checkbox1
2         checkbox2

I want to save like this :

ID        NAME
1         checkbox1 checkbox2

please advice




Aucun commentaire:

Enregistrer un commentaire