Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello
I have a problem with dateTimePicker.
I want,
- In case 1: I chose date in dateTimePicker, it will add this date to MySQL;
- And In case 2 : I not chose date in dateTimePicker, it will add null to MySQL.
I write the code, but in case 2, I not chose date, it still add current day to MySQL not null.
Please me this problem.
Here is my code
C#
string date;
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePicker1.Checked == true)
            {
               date = this.dateTimePicker1.Value.ToString("dd-MM-yyyy");
                
            }
            else
            {
                date = null;
            }
        }
private void btn_add_Click(object sender, EventArgs e)
        {
            connect_data();
            string Query = "INSERT into `"+ txt_table.Text+"` (ID,Date) values ('" + txt_stt.Text + "','" +date+ "');";
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDatabase);
            MySqlDataReader myReader;
            try
            {
                conDatabase.Open();
                myReader = cmdDataBase.ExecuteReader();
                MessageBox.Show("SAVE");
                while (myReader.Read())
                {
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Posted
Updated 1-Aug-15 21:48pm
v2

:sigh:
Do not 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. Use Parametrized queries instead.

And when you do that, don't convert to a string first - either send the DateTime value itself directly, or DbNull.Value instead.
 
Share this answer
 
1. For executing INSERT, UPDATE & DELETE queries it's better to use ExecuteNonQuery() instead of ExecuteReader().
2. Your query is vulnerable to SQLInjection. Use parameterized query or stored procedure to prevent SQLInection.
3. You have set null value to a string variable and in your query you are appending that with pair of single quotes which will result in just a pair of single quotes and not 'null'. This should result in exception but if it is saving current date against it then only guess is make sure that you have not set a default value for this field in the database. (I am not sure why it is not throwing exception, which it should)

For now ignoring all the best practices suggestions, you can check by applying the following changes-
C#
if (dateTimePicker1.Checked == true)
{
     date ="'"+ this.dateTimePicker1.Value.ToString("dd-MM-yyyy") +"'";
}
else
{
    date="NULL";
}

and
C#
string Query = "INSERT into `"+ txt_table.Text+"` (ID,Date) values ('" + txt_stt.Text + "'," +date+ ");"

Note that I've removed the single quotes from around date.

Hope, it helps :)
 
Share this answer
 
v2
Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA
 
Share this answer
 

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