1

Hello and thanks for reading.

I'm trying to insert the current date into my table, but I can't figure out how to write it correctly.

Here is my C# code:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
conn.Open();

string Comment = UserWriteComment.Text;
string ID = DetailedID.Text;
string Name = DetailedName.Text;
string UniqueID = lblID.Text;


string query = "INSERT INTO Comment(TicketID, Name, Comments, UserID, Date)" + "Values('" + ID + "', '" + Name + "', '" + Comment + "', '" + UniqueID + "', '" + Date + "')";
using (SqlCommand com = new SqlCommand(query, conn))
{
    com.ExecuteNonQuery();
    UserWriteComment.Text = "";
}

In the Query, There is a value called Date. This is here I like the Function to pass the current date into my Table.

I hope you can help me because I didnt managed to find the answer anywere.

Thanks:)

6
  • What is the problem? did you get any error or unexpected result? Commented Feb 12, 2014 at 22:10
  • Yes I got an error but that is most likely because I cant write the string thats needed correctly. Commented Feb 12, 2014 at 22:11
  • Just use a GetDate() without the quotes around it. Commented Feb 12, 2014 at 22:11
  • If I could get a bit of code, I would be so happy. Commented Feb 12, 2014 at 22:13
  • What is the text of the error message? Commented Feb 12, 2014 at 22:14

4 Answers 4

7

Use DateTime.Now or (in the database via sql) GetDate(). But more important, use sql-parameters to prevent sql-injection and conversion/localization issues:

string insertSql = @"INSERT INTO Comment(TicketID, Name, Comments, UserID, Date)
                     Values(@ID, @Name, @Comment, @UniqueID, @Date)";
using (var conn = new SqlConnection("...."))
using (var com = new SqlCommand(insertSql, conn))
{
    com.Parameters.AddWithValue("@ID", ID);
    com.Parameters.AddWithValue("@Name", Name);
    com.Parameters.AddWithValue("@Comment", Comment);
    com.Parameters.AddWithValue("@UniqueID", UniqueID);
    com.Parameters.AddWithValue("@Date", DateTime.Now);
    conn.Open();
    com.ExecuteNonQuery();
}

The using-statement ensures that unmanaged resources like the connection will be disposed/closed even in case of an error.

Sign up to request clarification or add additional context in comments.

Comments

5

Use DateTime.Now instead of Date. i.e. update the INSERT line to the following.

string query = "INSERT INTO Comment(TicketID, Name, Comments, UserID, Date)" 
              + "Values('" + ID + "', '" + Name + "', '" + Comment + "', '" 
              + UniqueID + "', '" + DateTime.Now + "')";

P.S: You really should be using Parameterize statements to avoid a Bobby Tables situation.

enter image description here

To fix this, implement it as shown by @Tim in his answer:

3 Comments

This also a valid option,
+1 for the comical approach to recommending SqlParameter usage :)
Awesome Thanks, didnt thought it was that "easy" as just using datatime.now in the Values parameter
0

Instead of Date, try using the following

DateTime.Now

Another function that can help you is

 GETDATE()

1 Comment

you could also consider using getdate() as the default field value in your table, unless you'll have timezone problems between appserver and database server.
0

Date inserts for SQL Server is best used via :

GetDate() 

or

Convert(Varchar, GetDate(), 101)

Note: converting the GetDate() value to varchar type 101 shortens the value to just the date w/o time stamp.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.