Results 1 to 10 of 10

Thread: Dates

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Posts
    308

    Dates

    Hi. I have one question for you. I have a database and an application
    and i am entering data into a database. At the moment I am using it wher the use enters the date but rather that that i want it to automatically enter the current date on their time thing.

    SqlDataSource2.InsertCommand = "Insert Into Comments_tbl(CommentTitle, BlogId, Comment, CommentDate, CommentName, CommentEmail)Values('" & TextBox1.Text & "'," & Request.QueryString("Id") & ",'" & TextBox2.Text & "','" & TextBox5.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "') "

    How would I do it. Any help would be appriciated.

  2. #2
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: Dates

    I assume that TextBox5.Text is the date you're using. You'll probably want to rename your textboxes to something meaningful to make your code easier to understand.

    There are two quick ways to replace this with the current date.

    First is:

    SqlDataSource2.InsertCommand = "Insert Into Comments_tbl(CommentTitle, BlogId, Comment, CommentDate, CommentName, CommentEmail)Values('" & TextBox1.Text & "'," & Request.QueryString("Id") & ",'" & TextBox2.Text & "', GETDATE(), '" & TextBox3.Text & "','" & TextBox4.Text & "') "

    That will have SQL Server insert the current date into the table.

    The second way is:

    SqlDataSource2.InsertCommand = "Insert Into Comments_tbl(CommentTitle, BlogId, Comment, CommentDate, CommentName, CommentEmail)Values('" & TextBox1.Text & "'," & Request.QueryString("Id") & ",'" & TextBox2.Text & "','" & DateTime.Now & "','" & TextBox3.Text & "','" & TextBox4.Text & "') "

    That will have VB get the date and add it to the string.

    Either way should work.

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Dates

    A sidenote just because I see this often and it's a good trick:

    When working with strings, especially SQL strings, the String.Format() function is your friend for simple, easy to read code:

    Code:
    SqlDataSource2.InsertCommand = String.Format("Insert Into Comments_tbl(CommentTitle, BlogId, Comment, CommentDate, CommentName, CommentEmail) Values('{0}',{1},'{2}', GETDATE(), '{3}','{4}') ", TextBox1.Text, Request.QueryString("Id"), TextBox2.Text, TextBox3.Text, TextBox4.Text)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Posts
    308

    Re: Dates

    Hi just one more question. How do i reset a dataset so that if i have entered data into a dataset that it resets.

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Dates

    Dim ds as Dataset

    ds.Clear

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dates

    Quote Originally Posted by Jenner
    A sidenote just because I see this often and it's a good trick:

    When working with strings, especially SQL strings, the String.Format() function is your friend for simple, easy to read code:

    Code:
    SqlDataSource2.InsertCommand = String.Format("Insert Into Comments_tbl(CommentTitle, BlogId, Comment, CommentDate, CommentName, CommentEmail) Values('{0}',{1},'{2}', GETDATE(), '{3}','{4}') ", TextBox1.Text, Request.QueryString("Id"), TextBox2.Text, TextBox3.Text, TextBox4.Text)
    It's not an especially good trick. It does make your code more readable and, thus, less error-prone but it doesn't get rid of all the issues. EVERYONE should be using parameters to insert values into SQL code EVERY time. Doing so will make your code even more readable than using String.Format, plus it negates any issues with single quotes in text, date formatting and also immunises you against SQL injection attacks. Follow the Data Access link in my signature for some ADO.NET examples that include the use of parameters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Posts
    308

    Re: Dates

    How do I get the date function to only show the time

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Dates

    String.Format("The time is: {0:hh:mm:ss}", Now() )
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Posts
    308

    Re: Dates

    I did it a dif way I did this. Sorry about posting like that.
    This was my code

    dim time
    time = DateTime.Now.Hour & ":" & DateTime.Now.Minute

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Dates

    OK, all this talk of formats is, as I said, really a bad idea. You should NOT have to worry about date format at all because you should NOT be converting dates to strings. If you do this properly it's never an issue.

    Also, even if you do remove the date portion when inserting the value into your SQL code, the default date will simply be added back again when you insert the value into the database. If you don't want to use the date then just ignore the date. When you get the value back from the database just use the time portion. This can be done in all sorts of ways, like getting the TimeOfDay property, setting the appropriate Format and CustomFormat of a DateTimePicker or accling ToShortDateString. You're trying to find workarounds to problems that you're creating yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width