Connecting Tech Pros Worldwide Forums | Help | Site Map

Insert DateTime to Sql Database

Kosal's Avatar
Member
 
Join Date: Feb 2007
Location: Phnom Penh
Posts: 58
#1: Jul 16 '08
Dear All

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

Please kindly help.

thanks

Needs Regular Fix
 
Join Date: Jul 2006
Location: India,Hyderabad
Posts: 367
#2: Jul 16 '08

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.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#3: Jul 16 '08

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));
  8.  
Then you just execute the query as you normally would.
Kosal's Avatar
Member
 
Join Date: Feb 2007
Location: Phnom Penh
Posts: 58
#4: Jul 17 '08

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
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#5: Jul 17 '08

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.
Kosal's Avatar
Member
 
Join Date: Feb 2007
Location: Phnom Penh
Posts: 58
#6: Jul 17 '08

re: Insert DateTime to Sql Database


ok thank you for your advise i will try to do it.
thanks
Kosal's Avatar
Member
 
Join Date: Feb 2007
Location: Phnom Penh
Posts: 58
#7: Jul 17 '08

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
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#8: Jul 17 '08

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.
Kosal's Avatar
Member
 
Join Date: Feb 2007
Location: Phnom Penh
Posts: 58
#9: Jul 17 '08

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
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#10: Jul 17 '08

re: Insert DateTime to Sql Database


If you tell us what error message you get, we might actually be able to help you.
Member
 
Join Date: Jul 2008
Posts: 41
#11: Jul 20 '08

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
Kosal's Avatar
Member
 
Join Date: Feb 2007
Location: Phnom Penh
Posts: 58
#12: Jul 22 '08

re: Insert DateTime to Sql Database


Hi All
thank you for your help
now I resolve this problem already.

thanks
Newbie
 
Join Date: Feb 2008
Posts: 4
#13: Aug 13 '08

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"
Kosal's Avatar
Member
 
Join Date: Feb 2007
Location: Phnom Penh
Posts: 58
#14: Sep 8 '08

re: Insert DateTime to Sql Database


Ok Sir

Now it is ok
Thank you for you advice

thank
Best Regard
Reply