Insert DateTime to Sql Database  | Member | | Join Date: Feb 2007 Location: Phnom Penh
Posts: 58
| | |
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
| | | 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.
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,608
| | | 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: -
//c# code.
-
//dt is the datetime field
-
string update = "update test set dt=@dt where id=@id";
-
OleDbCommand cmd = new OleDbCommand(update, conn);
-
DateTime dtme = DateTime.Now;
-
cmd.Parameters.AddWithValue("@dt", dtme.ToString());
-
cmd.Parameters.AddWithValue("@id", Convert.ToInt32(TextBox2.Text));
-
Then you just execute the query as you normally would.
|  | Member | | Join Date: Feb 2007 Location: Phnom Penh
Posts: 58
| | | 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
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,608
| | | 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.
|  | Member | | Join Date: Feb 2007 Location: Phnom Penh
Posts: 58
| | | re: Insert DateTime to Sql Database
ok thank you for your advise i will try to do it.
thanks
|  | Member | | Join Date: Feb 2007 Location: Phnom Penh
Posts: 58
| | | 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
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,608
| | | 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.
|  | Member | | Join Date: Feb 2007 Location: Phnom Penh
Posts: 58
| | | 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
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,608
| | | 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
| | | 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
|  | Member | | Join Date: Feb 2007 Location: Phnom Penh
Posts: 58
| | | 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
| | | 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"
|  | Member | | Join Date: Feb 2007 Location: Phnom Penh
Posts: 58
| | | re: Insert DateTime to Sql Database
Ok Sir
Now it is ok
Thank you for you advice
thank
Best Regard
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|