Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Insert DateTime to Sql Database

Question posted by: Kosal (Member) on July 16th, 2008 09:58 AM
Dear All

I would like you to help to insert and update datetime to sql database.

Please kindly help.

thanks
nmsreddi's Avatar
nmsreddi
Needs Regular Fix
365 Posts
July 16th, 2008
10:55 AM
#2

Re: Insert DateTime to Sql Database
Hello

you can use normal sql query as usual like other fields how you are inserting ,

have you tried it ,did you get any error,

mostly you will get format conversion errors ,by default sqlserver will take MM/dd/yyyy format ,make sure that your input is in that format other you can conver that to your custom format with the existing convert functions.

Reply
insertAlias's Avatar
insertAlias
Moderator
1,460 Posts
July 16th, 2008
02:05 PM
#3

Re: Insert DateTime to Sql Database
Do you know how to insert/update things other than datetimes? What I'm trying to get at here is: do you have a problem specifically with datetimes, or do you just not know how to use a DB in your program? We have a few articles on how to use databases. I'd suggest you check them out in the Howto section. Otherwise, if it is a datetime problem, you can use a parameterized query and not have to worry about it.

Here's a code sample on using a parameterized query:
Expand|Select|Wrap|Line Numbers
  1. //c# code.
  2. //dt is the datetime field
  3. string update = "update test set dt=@dt where id=@id";
  4. OleDbCommand cmd = new OleDbCommand(update, conn);
  5. DateTime dtme = DateTime.Now;
  6. cmd.Parameters.AddWithValue("@dt", dtme.ToString());
  7. cmd.Parameters.AddWithValue("@id", Convert.ToInt32(TextBox2.Text));


Then you just execute the query as you normally would.

Reply
Kosal's Avatar
Kosal
Member
46 Posts
July 17th, 2008
04:40 AM
#4

Re: Insert DateTime to Sql Database
Dear All

thank you for your help, but I need as bellow:
I add DateTimePicker and i want to insert like
strSQL = "INSERT INTO tblBooking(bPNR, bPaxName, bIssureDate, bTravelDate, bDeparting, bArriving, bFlyType, bStatus, bBy, bNetPrice, bSalePrice)" & "VALUES('" & txtPNR.Text & "', '" & txtPaxName.Text & "','" & DateTimePicker1.Text & "','" & DateTimePicker2.Text & "','" & txtDeparting.Text & "','" & txtArriving.Text & "','" & cboFlyType.Text & "','" & cboStatus.Text & "','" & cboBy.Text & "'," & txtNetPrice.Text & "," & txtSalePrice.Text & ")"

please advise

thanks

Reply
insertAlias's Avatar
insertAlias
Moderator
1,460 Posts
July 17th, 2008
04:48 AM
#5

Re: Insert DateTime to Sql Database
Well, if you'll read my reply, I show you how to use a parameterized query. That's the easiest way to do this. Change your values clause to "VALUES (@bPNR, @bPaxName, @bIssureDate ... " and so forth. Then add the parameter values. My example was in C#, but it shouldn't be that hard to translate to vb.

Reply
Kosal's Avatar
Kosal
Member
46 Posts
July 17th, 2008
04:52 AM
#6

Re: Insert DateTime to Sql Database
ok thank you for your advise i will try to do it.
thanks

Reply
Kosal's Avatar
Kosal
Member
46 Posts
July 17th, 2008
04:59 AM
#7

Re: Insert DateTime to Sql Database
Dear all

I try to do it but still error can you give me the full code for this problem in vb.net

I am waiting for your advise

thanks
Best Regard

Reply
insertAlias's Avatar
insertAlias
Moderator
1,460 Posts
July 17th, 2008
05:03 AM
#8

Re: Insert DateTime to Sql Database
We don't do your assignment for you here. Please don't ask for a full code solution.

MODERATOR

Now, if you post what you've tried and the error message, we can get down to business trying to help you correct your code.

Reply
Kosal's Avatar
Kosal
Member
46 Posts
July 17th, 2008
06:57 AM
#9

Re: Insert DateTime to Sql Database
ok thanks you for your help, but i still cannot do anything yet with
insert datetimepicker to database.

thanks

Reply
insertAlias's Avatar
insertAlias
Moderator
1,460 Posts
July 17th, 2008
12:19 PM
#10

Re: Insert DateTime to Sql Database
If you tell us what error message you get, we might actually be able to help you.

Reply
ganeshvkl's Avatar
ganeshvkl
Newbie
31 Posts
July 20th, 2008
06:04 AM
#11

Re: Insert DateTime to Sql Database
hi Kosal,
datepicker1.value instead of datepicker1.text , it is working in VB.net 2003
with sql server
Try it out...

ganesh

Reply
Kosal's Avatar
Kosal
Member
46 Posts
July 22nd, 2008
02:55 AM
#12

Re: Insert DateTime to Sql Database
Hi All
thank you for your help
now I resolve this problem already.

thanks

Reply
karthik kaliyaperumal's Avatar
karthik kaliyaperumal
Newbie
4 Posts
August 13th, 2008
09:27 AM
#13

Re: Insert DateTime to Sql Database
HI,

You can store in one variable like = datatime.now (This is vill give current datatime.then you can pass to database.

Otherwise change the format like "DD/MM/YYYY"

Reply
Kosal's Avatar
Kosal
Member
46 Posts
September 8th, 2008
07:02 AM
#14

Re: Insert DateTime to Sql Database
Ok Sir

Now it is ok
Thank you for you advice

thank
Best Regard

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,283 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top .NET Forum Contributors