Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My SQL Table format

Crnt_Date     -- date
Enter_Date    -- date
Time_         -- varchar(10)
Name_         -- varchar(50)
Product_      -- varchar(50)
Count_        -- int
Amount_       -- int





To display current date
C#
DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
int day = dt.Day;
int mon = dt.Month;
int year = dt.Year;
label4.Text = System.Convert.ToString(day + "/" + mon + "/" + year);



To insert the values i use

C#
cn.Open();

cmd = new SqlCommand("insert into Entry(Crnt_Date,Enter_Date,Time_,Name_,Product_,Count_,Amount_)
values('" + Convert.ToDateTime(label4.Text)
  + "','" + Convert.ToDateTime(dateTimePicker1.Text)
  + "','" + comboBox3.Text
  + "','" + lblName.Text
  + "','" + comboBox1.Text
  + "','" + comboBox2.Text
  + "','" + textBox1.Text + "')", cn);

 cmd.ExecuteNonQuery();

 MessageBox.Show("Inserted");

 cn.Close();



But it show the error message

"String was not recognized as a valid DateTime."


How can i store the current date and datetimepicker date into the db? Help me..
Posted
Updated 28-Aug-13 21:34pm
v2

You don't want to do it that way, you'll ant to use command parameters and a parameterized sql query instead (otherwise you open your application up to SQL Injection[^] (and that is bad).

Something like this might point you in the right direction;
C#
// To display current date
DateTime dt = DateTime.Today;
label4.Text = dt.ToString("dd/MM/yyyy");

// To insert the values
cn.Open();
using (var cmd = new SqlCommand("insert into Entry(Crnt_Date, Enter_Date, Time_,Name_, Product_, Count_,Amount_) values (@Crnt_Date, @Enter_Date, @Time_,Name_, @Product_, @Count_, @Amount_)"), cn)
{
  cmd.Parameters.AddWithValue("Crnt_Date", DateTime.ParseExact(label4.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture));
  cmd.Parameters.AddWithValue("Enter_Date", dateTimePicker1.Value);
  cmd.Parameters.AddWithValue("Time_", comboBox3.Text);
  cmd.Parameters.AddWithValue("Name_", lblName.Text);
  cmd.Parameters.AddWithValue("Product_", comboBox1.Text);
  cmd.Parameters.AddWithValue("Count_", comboBox2.Text);
  cmd.Parameters.AddWithValue("Amount_", textBox1.Text);
  cmd.ExecuteNonQuery();
  MessageBox.Show("Inserted");
}
 
cn.Close();


To handle possible incorrectly typed dates in the text boxes look into DateTime.TryParseExact.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Vijaydhas 29-Aug-13 4:00am    
Oh.. Thank you for instant replay. I am very new to this command parameters and a parameterized sql query. May i know the differences? Can you explain it to me.. Please.. And now it show error in 'var' and 'cn'. and also show error in 'CultureInfo'.
Fredrik Bornander 29-Aug-13 4:13am    
You can't just copy and paste my answer, it wont work as it's based on the fragment of code you supplied in your question. If you replace var with SqlCommand that should work, but you need to initialize your own Connection (cn).

Read the wikipedia article on SQL Injection and you'll understand why you NEVER want to concatenate a bunch of strings into your sql statement (especially if those strings are entered by a user).
Vijaydhas 29-Aug-13 4:37am    
Thank you.. It's working..!! :-) .. I will follow the wiki also.. Once again thank you..
Fredrik Bornander 29-Aug-13 5:55am    
Not a problem, glad I could help.
Hi Vijaydhas,

Please try this code

VB
cmd = new MySqlCommand("insert into Entry(Crnt_Date,Enter_Date,Time_,Name_,Product_,Count_,Amount_) values('" + DateTime.Now + "','" + Convert.ToDateTime(dateTimePicker1.Text)
  + "','" + comboBox3.Text
  + "','" + lblName.Text
  + "','" + comboBox1.Text
  + "','" + comboBox2.Text
  + "','" + textBox1.Text + "')", cn);
 
Share this answer
 
Comments
Vijaydhas 29-Aug-13 4:42am    
It is also working..!! Thank you..

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