Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 5 chekbox. (ChekBox1,ChekBox2,ChekBox3, ChekBox4, ChekBox5)
Only one can be cheked.
Need to do insert into CHEKED chekbox value in sql
Some help?

Value is clasic text
Column name "razlog"

Thank you

What I have tried:

using (SqlConnection openCon = new SqlConnection(cs))
            {
                string saveStaff = "INSERT into dbo.izlaznice (datumizlaska, datumpovratka, tip, razlog )" +
                                   " VALUES ('" + dateTimePicker1.Value.Date.ToString("yyyyMMddmmHH") + "', '" + dateTimePicker2.Value.Date.ToString("yyyyMMddmmHH") + "', 'Slobodan dan', <pre>razlog????)";

                using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
                {
                    querySaveStaff.Connection = openCon;
                    openCon.Open();
                    querySaveStaff.ExecuteNonQuery();
                    openCon.Close();


                }

            }
Posted
Updated 29-Oct-18 23:07pm

1 solution

For starters, never do it like that. Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
The example you show doesn't have that problem, but it has others and indicates that the rest of your app is vulnerable and needs to be fixed as soon as possible.
The code you show will fail at some point because the data is be8ing passed as a string, and it needs to be stored as a DATETIME column in SQL to be of any use to anyone. That means SQL has to "guess" what the format you passed it is, and it will get it wrong sometimes.
Pass everything as a parameter and these problems go away.

If you are using a text column in SQL to store your checkbox results, that's inefficient - use a second table to translate the text into an "enum" style number (and use JOIN when you want the string form, SQL is very good at those) You can then use the Tag field of the checkbox to store that enum value and pass that through to SQL as a parameter.
 
Share this answer
 
Comments
Stylus STYLUS 30-Oct-18 5:17am    
My dear friend, thank you.

I need solutin for c#, not sql.

Like...

if (checkbox1.Checked && !checkbox2.Checked)
{
insert into sql...
}
else if (!checkbox1.Checked && checkbox2.Checked)
{
insert into sql...
}
OriginalGriff 30-Oct-18 5:26am    
I'm suggesting that you have your DB design wrong, and you need to change it.
One of the ways to do both at the same time is to use a bitfield: the second table tarranslates number to strings (whatever your strings may be) and the Tag field of the Checkbox provides a value: 1 for the first box, 2 for the second, 4 for the third, 8 for the fourth, ...
Then a simple OR of the values gives you the combination to send to SQL without having to code for 64 possibilities as an if condition!
Stylus STYLUS 30-Oct-18 5:27am    
Thank you
OriginalGriff 30-Oct-18 5:30am    
You're welcome!

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