473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

insert date into SQL server at Visual basic

Hi, i am now doing a Car rental system web project. Well the interface is very similiar to http://www.avis.com.sg/, which i have extra modelname dropdownlist.

Here is my code.Label 7 is just a Label to indicate Booking_ID number. MY Table consist of Booking_ID,StartDate,EndDate,Modelname,branch_ID,c ustomer_ID respectively.

I just couldnt insert the data into my SQL database even though i have insert the datas in all my of the dropdownlist and click submit. The Data is still not inserted with no error encountered. I suspect is the date problem.
Please assit.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
  2.  
  3. Dim strcon As String = "Data Source=BISHAN\MICROSOFTSQL;Integrated Security=True;Pooling=False;Initial Catalog='AbelProject'"
  4. Dim con As SqlConnection = New SqlConnection(strcon)
  5.  
  6.  Dim cmd As New SqlCommand
  7. Dim cmd2 As New SqlCommand
  8. Dim cmd3 As New SqlCommand
  9.  
  10.  con.Open()
  11.  
  12. cmd3.Connection = con
  13. Label5.Text = cmd3.ExecuteScalar()
  14.  
  15.  
  16. If Not Page.IsPostBack Then
  17. FillCarmodelList()
  18. FillbranchList()
  19. FillHourlist()
  20. FillminuteList()
  21. FilldayList()
  22. Fillmonthlist()
  23.  
  24.  
  25. End If
  26.  
  27. Dim i As Integer
  28.  
  29. cmd.CommandText = "select count(*) from Rent_Record "
  30.  
  31. cmd.Connection = con
  32.  
  33.  
  34. i = cmd.ExecuteScalar()
  35.  
  36. Label7.Text = i + 1
  37. 'If i <= 0 Then
  38.  
  39. 'cmd.CommandText() = "Insert into Rent_Record values"
  40. Dim insertsql As String
  41.  
  42. insertsql = "INTO Rent_Record ('Booking_ID')"
  43.  
  44. insertsql &= "values('" + Label7.Text + "')"
  45.  
  46. 'ElseIf i > 0 Then
  47.  
  48. 'Dim insertsql As String
  49. 'insertsql = "INTO Rent_Record ("
  50. 'insertsql &= "Booking_ID)"
  51. 'insertsql &= "values('" + Label7.Text + "')"
  52.  
  53. 'insertsql &= "values('"
  54. 'insertsql &= "i + 1 '")
  55.  
  56. 'End If
  57. con.Close()
  58. con.Dispose()
  59. End Sub
  60.  
  61. Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
  62.  
  63. Dim strcon As String = "Data Source=BISHAN\MICROSOFTSQL;Integrated Security=True;Pooling=False;Initial Catalog='AbelProject'"
  64. Dim con As SqlConnection = New SqlConnection(strcon)
  65.  
  66. Dim startdate As DateTime
  67. Dim enddate As DateTime
  68. 'Create SQL command object
  69. Dim cmd As New SqlCommand
  70. con = New SqlConnection(strcon)
  71. 'connect command to connection
  72. cmd.Connection = con
  73.  
  74. startdate.Hour.Equals(MonthList1.SelectedItem.Text)
  75. 'startdate.Hour.Equals(MonthList1.SelectedItem.Text)
  76. 'startdate.Day.Equals(DayList1.SelectedItem.Text)
  77. 'startdate.Minute.Equals(MinuteList1.SelectedItem.Text)
  78.  
  79. enddate.Hour.Equals(MonthList2.SelectedItem.Text)
  80. enddate.Day.Equals(Daylist2.SelectedItem.Text)
  81. enddate.Minute.Equals(MinuteList2.SelectedItem.Text)
  82.  
  83.  
  84. con.Open()
  85.  
  86.  
  87. ' cmd.CommandText() = "Insert into Customer values ('" + _
  88. 'Label7.Text(+"','" + _
  89. 'startdate.Date + "','" + _
  90. 'enddate.Date + "','" + _
  91. 'CarModelList.SelectedItem.Text + "','" + _
  92. 'BranchList.SelectedItem.Text + "','" + _
  93. 'Session("customerID") + "')")
  94.  
  95. cmd.CommandText() = "update rent_record set startDate = '" + startdate.Date + _
  96. "', enddate = '" + enddate.Date + _
  97. "', modelname = '" + CarModelList.SelectedItem.Text + _
  98. "',Branch_ID = '" + BranchList.SelectedItem.Text + _
  99. "',Customer_ID= '" + Session("customerID") + _
  100. "'" + _
  101. " Where Booking_ID = '" + Label7.Text + "'"
  102.  
  103.  
  104. cmd.ExecuteNonQuery()
  105.  
  106. con.Close()
  107. con.Dispose()
  108. Response.Redirect("BookingConfirm.aspx")
  109. End Sub
Feb 4 '09 #1
4 7072
Frinavale
9,735 Expert Mod 8TB
Instead of doing the following:
insertsql = "INTO Rent_Record ('Booking_ID')"
insertsql &= "values('" + Label7.Text + "')"

Why don't you just use "i+1"?


You aren't actually executing the SQL Insert command anywhere in code...could this be your problem?
Feb 4 '09 #2
i suspect either of the 2 reasons or both.

1) theres problem inserting with Booking_ID without inserting other fields under the REnt_REcord Table

2_ The insert of codes from 3 dropdownlist to variable StartDate and EndDate Variables at Line 74- 77 consist of error.

Please Assist.
Thanks Frinavale, i will check it out.
Feb 5 '09 #3
Frinavale
9,735 Expert Mod 8TB
I have added a bunch of comments to your code.
You're code is confusing to me and you are going to have to explain things to me before I can continue to help you.

One thing I'd recommend is that you try your SQL queries in something like Query Analyzer to make sure that they work before attempting to use them in code.


Expand|Select|Wrap|Line Numbers
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
  2.  
  3.     Dim strcon As String = "Data Source=BISHAN\MICROSOFTSQL;Integrated Security=True;Pooling=False;Initial Catalog='AbelProject'"
  4.     Dim con As SqlConnection = New SqlConnection(strcon)
  5.  
  6.     Dim cmd As New SqlCommand
  7.     Dim cmd2 As New SqlCommand
  8.     Dim cmd3 As New SqlCommand
  9.  
  10.     con.Open()
  11. 'I really don't understand what you are doing here
  12. 'You haven't specified any SQL query/command and yet
  13. 'you're calling the ExecuteScalar method to return you
  14. 'the first cell in the result set returned by the query ...
  15. 'What are you doing here?????
  16.     cmd3.Connection = con
  17.     Label5.Text = cmd3.ExecuteScalar()
  18.  
  19.  
  20.     If Not Page.IsPostBack Then
  21.         FillCarmodelList()
  22.         FillbranchList()
  23.         FillHourlist()
  24.         FillminuteList()
  25.         FilldayList()
  26.         Fillmonthlist()
  27.      End If
  28.  
  29.  
  30. 'Now this makes sense: you're supplying the command 
  31. 'with an SQL query to execute.  You're retrieving the
  32. 'first cell that results from the query ....
  33. 'but you're storing that value incremented by one
  34. 'in a label instead of using it in any programming logic.
  35. 'I think you are doing this in the wrong place...
  36. 'you should probably be doing this were you need to use the
  37. 'value returned....Please explain.
  38.     Dim i As Integer
  39.     cmd.CommandText = "select count(*) from Rent_Record "
  40.     cmd.Connection = con
  41.      i = cmd.ExecuteScalar()
  42.  
  43.     Label7.Text = i + 1
  44.  
  45.  
  46.  
  47.                     'If i <= 0 Then
  48.                     'cmd.CommandText() = "Insert into Rent_Record values"
  49.  
  50. 'Ok now here comes the most confusing part.
  51. 'You're declaring a String that contains an SQL query 
  52. '(built on the value you stored in the label...instead of just using i+1...)
  53. 'But you never actually Use this query anywhere....
  54. 'You're just declaring it and aren't executing it.
  55. 'Even if you did execute this, and it worked properly....
  56. 'Why are you doing this in the Page Load event? It's going
  57. 'to be executed every time the page posts back.
  58.     Dim insertsql As String
  59.      insertsql = "INTO Rent_Record ('Booking_ID')"
  60.      insertsql &= "values('" + Label7.Text + "')"
  61.  
  62.                     'ElseIf i > 0 Then
  63.                     'Dim insertsql As String
  64.                     'insertsql = "INTO Rent_Record ("
  65.                     'insertsql &= "Booking_ID)"
  66.                     'insertsql &= "values('" + Label7.Text + "')"
  67.                     'insertsql &= "values('"
  68.                     'insertsql &= "i + 1 '")
  69.                      'End If
  70.     con.Close()
  71.     con.Dispose()
  72. End Sub
  73.  
  74. Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
  75.  
  76.     Dim strcon As String = "Data Source=BISHAN\MICROSOFTSQL;Integrated Security=True;Pooling=False;Initial Catalog='AbelProject'"
  77.     Dim con As SqlConnection = New SqlConnection(strcon)
  78.  
  79.     Dim startdate As DateTime
  80.     Dim enddate As DateTime
  81.  
  82.     'Create SQL command object
  83.     Dim cmd As New SqlCommand
  84.     con = New SqlConnection(strcon)
  85.  
  86.     'connect command to connection
  87.     cmd.Connection = con
  88.  
  89. 'Are you crashing here??
  90. 'Is this where you're having problems?
  91. 'Have you tried stepping through this and checking if this is set correctly?
  92.  
  93.     startdate.Hour.Equals(MonthList1.SelectedItem.Text)
  94.     'startdate.Hour.Equals(MonthList1.SelectedItem.Text)
  95.     'startdate.Day.Equals(DayList1.SelectedItem.Text)
  96.     'startdate.Minute.Equals(MinuteList1.SelectedItem.Text)
  97.  
  98.     enddate.Hour.Equals(MonthList2.SelectedItem.Text)
  99.     enddate.Day.Equals(Daylist2.SelectedItem.Text)
  100.     enddate.Minute.Equals(MinuteList2.SelectedItem.Text)
  101.  
  102.  
  103.     con.Open()
  104.  
  105.  
  106.             ' cmd.CommandText() = "Insert into Customer values ('" + _
  107.             'Label7.Text(+"','" + _
  108.             'startdate.Date + "','" + _
  109.             'enddate.Date + "','" + _
  110.             'CarModelList.SelectedItem.Text + "','" + _
  111.             'BranchList.SelectedItem.Text + "','" + _
  112.             'Session("customerID") + "')")
  113.  
  114. 'I think this is where you want to create a new entry in the Rent_Record table.
  115. 'First, grab the next available ID for the record and then insert a new record....
  116. 'instead of updating some record that you created during the page post back.
  117.     cmd.CommandText() = "update rent_record set startDate = '" + startdate.Date + _
  118.     "', enddate = '" + enddate.Date + _
  119.     "', modelname = '" + CarModelList.SelectedItem.Text + _
  120.     "',Branch_ID = '" + BranchList.SelectedItem.Text + _
  121.     "',Customer_ID= '" + Session("customerID") + _
  122.     "'" + _
  123.     " Where Booking_ID = '" + Label7.Text + "'"
  124.  
  125.  
  126.     cmd.ExecuteNonQuery()
  127.  
  128.     con.Close()
  129.     con.Dispose()
  130.     Response.Redirect("BookingConfirm.aspx")
  131. End Sub
Feb 5 '09 #4
hey thanks~ i working out the SQL part.

Ok my final question. As u can see i got 2 set of 3Dropdownlists.
StartDate: Minute,Hour, Month
Enddate Minute Hour,Month

How do i get the data from drop downlist and insert to data. my Line 93-100 seems stupid.

Thank u so much

How to get Date
Feb 5 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ray | last post by:
Date problem. Using non American date formats with Microsoft Access and Visual Basic. I set my regional settings to English(Ireland) or English(United Kingdom) and my date appears in Access as...
0
by: rodneybauer1 | last post by:
hello: thanks in advance for any responses. i dont have a isp at home so have to use public library computer. will check this every couple days though. im using visual basic.Net windows XP with...
1
by: tshad | last post by:
I can't seem to get insert into a DataGrid to work. I am using an example from the net and made changes to work on my computer. The grid comes up fine. The edit boxes in the footer are there. ...
9
by: Bob Achgill | last post by:
I would like to use the timestamp on files to manage the currency of support files for my VB windows application. In this case I would only put the timestamp of the file in the management database...
2
by: http://www.visual-basic-data-mining.net/forum | last post by:
All, I am attempting to insert a new row into an MS Access database using VB.NET. This is an existing access database that I did not create, so I do not know all of the field properties. I have...
0
by: hemant123 | last post by:
Hi I Am Trying To Insert Null Value In Oracle Date Column Using Visual Basic 6 Textbox. I Tried Various Options But Unable To Insert. Please Advise Me A Solution. Thanks In Advance Hemant
10
by: sandraz444 | last post by:
I have an expression in the query under my form to autofill the date under a certain condition but it wont write to the underlying table?? The date shows in the form but not the table. Does anyone...
1
by: veer | last post by:
hello expert i want to insert the date in one column of excel sheet but the date should be one which i enter in vb form e,g i have a column name Date and i want to show the date which i select...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.