|
-
Feb 13th, 2008, 11:17 AM
#1
Thread Starter
Hyperactive Member
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.
-
Feb 13th, 2008, 11:34 AM
#2
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.
-
Feb 13th, 2008, 11:49 AM
#3
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)
-
Feb 13th, 2008, 12:03 PM
#4
Thread Starter
Hyperactive Member
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.
-
Feb 13th, 2008, 12:28 PM
#5
Re: Dates
Dim ds as Dataset
ds.Clear
-
Feb 13th, 2008, 07:56 PM
#6
Re: Dates
 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.
-
May 1st, 2008, 02:32 PM
#7
Thread Starter
Hyperactive Member
Re: Dates
How do I get the date function to only show the time
-
May 1st, 2008, 02:36 PM
#8
Re: Dates
String.Format("The time is: {0:hh:mm:ss}", Now() )
-
May 1st, 2008, 02:39 PM
#9
Thread Starter
Hyperactive Member
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
-
May 1st, 2008, 06:35 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|