Click here to Skip to main content
15,884,811 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am inserting datetime from C# to SqlServer. But format is not compatible.

I tried using DateTime.Now. But it is giving error.

Please help?
Posted
Updated 19-Oct-21 19:26pm
Comments
Sandeep Mewara 15-Jul-10 15:43pm    
What error? What format?
Member 7217874 15-Jul-10 15:52pm    
showing Incorrect Syntax error near 12.
12 is the hours.
OriginalGriff 16-Jul-10 4:25am    
Answer updated.

Insert it with a parametrised query, and it will sort itself out - as well as being safer (see SQL Injection attack). Google SQLCommand.AddWithValue and use that instead of including the DateTime.Now in your SQL command string.

Member 7217874 wrote:
Can you give an example with entering more than one parameter insertion takes place.


SqlConnection conn = new SqlConnection("your connection string");
SqlCommand cmd = new SqlCommand("INSERT INTO table_name (username, logindate) VALUES (@USERNAME, @DATE)", conn);
cmd.Parameters.AddWithValue("@USERNAME", tbUserName.Text);
cmd.Parameters.AddWithValue("@DATE", DateTime.Now);
int i = cmd.ExecuteNonQuery();
 
Share this answer
 
v2
Comments
Member 7217874 15-Jul-10 15:59pm    
Can you give an example with entering more than one parameter insertion takes place.
ok try this
................
Sql
.............
abc datetime ,.......
abc is a columns
,......... ............
C#
......................

C#
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string a = DateTime.Now.ToShortDateString();
                string Query = "Insert into ab values('" + a + "')";
                string con = "Data source =abc; initial catalog=ex;            integrated security= true";
                SqlConnection cn = new SqlConnection(con);
                cn.Open();
                SqlCommand cmd = new SqlCommand( Query ,cn);
                cmd.ExecuteNonQuery();
               
            }
            catch (Exception g)
            {
                label1.Text = g.Message;
            }
        }
 
Share this answer
 
v2
Use GetDate() function in Sql it inserts current date
 
Share this answer
 
Comments
CHill60 20-Oct-21 3:43am    
Eleven years ago the OP wanted to pass a date from C# to SQL. GetDate() only inserts the current date - what if they are trying to store someone's date of birth? So your solution, whilst accurate, is off-topic and goes nowhere near answering the (already answered) question.
When answering questions in this forum it's best to
- stick to newer questions where the OP still needs help
- review previous solutions to make sure you're not repeating someone else's answers
- make sure you are answering the question that was asked
Sean Ewington 2-Nov-21 15:26pm    
Just a reminder, answering eleven year-old questions is perfectly allowed. Unusual. Perhaps even baffling. But allowed.
CHill60 2-Nov-21 15:28pm    
I do try, but thanks for the reminder
TheRealSteveJudge 20-Oct-21 5:24am    
"Answering" an eleven years old question makes no sense at all.
Sean Ewington 2-Nov-21 15:26pm    
Just a reminder, answering eleven year-old questions is perfectly allowed. Unusual. Perhaps even baffling. But allowed.

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