Saving and displaying data on datagrid!

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 30
Reputation: kn2790 is an unknown quantity at this point 
Solved Threads: 0
kn2790 kn2790 is offline Offline
Light Poster

Saving and displaying data on datagrid!

 
0
  #1
Apr 19th, 2009
ok i understand some basics of vb but am still relatively a new...

my problem.. code is for saving values into a database and displaying on the same form on a datagrid which is connected to the database...

  1. Imports System.Data.OleDb
  2. Public Class Addbooking
  3. Inherits System.Windows.Forms.Form
  4.  
  5. Public cN As OleDbConnection
  6. Public myOleDbCommand As OleDbCommand
  7. Dim Da As New OleDbDataAdapter()
  8. Dim dataS As New DataSet()
  9. Dim Nextb As Long
  10. Dim PrevB As Long
  11. Dim TotalC As Long
  12.  
  13.  
  14. Private Sub Addbooking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  15. 'TODO: This line of code loads data into the 'LVuserdbDataSet1.Bookings' table. You can move, or remove it, as needed.
  16. Me.BookingsTableAdapter.Fill(Me.LVuserdbDataSet1.Bookings)
  17.  
  18. End Sub
  19.  
  20. Private Sub connecttome()
  21. Nextb = 0
  22. PrevB = 0
  23. Dim STrPath = System.IO.Directory.GetCurrentDirectory & "\mydb.mdb"
  24.  
  25. Try
  26. cN = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & STrPath & "';Jet OLEDB:Database Password=1")
  27. myOleDbCommand = New OleDbCommand("Select * from bookings where StaffID<>null order by name asc")
  28.  
  29. cN.Open()
  30. Catch ex As Exception
  31. MsgBox(ex.Message, MsgBoxStyle.Information, "")
  32. End
  33. End Try
  34. myOleDbCommand.Connection = cN
  35.  
  36. Da = New OleDb.OleDbDataAdapter("Select * from bookings where StaffID<>null order by name asc", cN)
  37. dataS = New DataSet("MS")
  38. Da.Fill(dataS, "MS")
  39. DataGridView1.DataSource = dataS
  40. DataGridView1.DataMember = "MS"
  41.  
  42. Dim x = dataS.Tables("ms").Rows.Count()
  43.  
  44.  
  45. TotalC = x - 1
  46.  
  47.  
  48.  
  49. FILL(0)
  50.  
  51. Dim Dr As OleDb.OleDbDataReader = myOleDbCommand.ExecuteReader
  52. cN.Close()
  53. Button11()
  54. End Sub
  55.  
  56. Private Sub FILL(ByVal X As Long)
  57. If dataS Is Nothing Then Return
  58. Try
  59. With dataS.Tables("ms").Rows(X)
  60. stafid.Text = IIf(IsDBNull(.Item("Staff ID")), "NULL", .Item("Staff ID"))
  61. rooid.Text = IIf(IsDBNull(.Item("Room ID")), "NULL", .Item("Room ID"))
  62. date1.Text = IIf(IsDBNull(.Item("Date Required")), "NULL", .Item("Date Required"))
  63. time1.Text = IIf(IsDBNull(.Item("Time Required")), "NULL", .Item("Time Required"))
  64. areq.Text = IIf(IsDBNull(.Item("Additional requirements")), "NULL", .Item("Additional requirements"))
  65. End With
  66. Catch err As Exception
  67.  
  68. End Try
  69. Me.Text = X
  70.  
  71. End Sub
  72.  
  73. Private Sub Button11()
  74. btnaddbook.Enabled = True
  75. If TotalC <= 0 Then
  76. btnaddbook.Enabled = True
  77. Else
  78. btnaddbook.Enabled = False
  79. End If
  80. End Sub
  81.  
  82. Private Sub btnaddbook_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddbook.Click
  83.  
  84. If rooid.Text = "" Then
  85. MsgBox("Invalid StaffID!", MsgBoxStyle.Information, "")
  86. rooid.Focus()
  87. Exit Sub
  88. End If
  89. If date1.Text = "" Then date1.Text = " "
  90. If time1.Text = "" Then time1.Text = " "
  91.  
  92.  
  93. If stafid.Text = "" Then
  94. SAVE()
  95. Else
  96. UPDATE1()
  97. End If
  98.  
  99.  
  100. End Sub
  101.  
  102. Private Sub UPDATE1()
  103.  
  104. cN.Open()
  105. Dim SAZ As String
  106. SAZ = "Update booking set StaffID='" & rooid.Text & "', name='" & date1.Text & "', " _
  107. & "address='" & time1.Text & "' where StaffID='" & stafid.Text & "'"
  108. 'MsgBox(SQL)
  109. Dim nwol As New OleDbCommand(SAZ, cN)
  110. nwol.ExecuteNonQuery()
  111. cN.Close()
  112. connecttome()
  113. End Sub
  114.  
  115. Private Sub SAVE()
  116.  
  117. Dim DC1 As New OleDb.OleDbCommand()
  118.  
  119. Dim SQL As String
  120.  
  121. cN.Open()
  122. 'Stop
  123. Dim Ad As New OleDbDataAdapter("select * from bookings where StaffID='" & rooid.Text & "'", cN)
  124. Dim Check_dataset As DataSet
  125. Check_dataset = New DataSet("check")
  126. Ad.Fill(Check_dataset, "check")
  127. If Check_dataset.Tables("check").Rows.Count <> 0 Then
  128. MsgBox("Item StaffID already exists!", MsgBoxStyle.Information, "")
  129. rooid.SelectionStart = 0
  130. rooid.SelectionLength = Len(rooid.Text)
  131. rooid.Focus()
  132. cN.Close()
  133. Exit Sub
  134. End If
  135.  
  136.  
  137. SQL = "Insert into Bookings(StaffID,RoomID,Date required,Time required,Additional requirements) values(" _
  138. & "'" & stafid.Text & "', " _
  139. & "'" & rooid.Text & "', " _
  140. & "'" & date1.Text & "', " _
  141. & "'" & time1.Text & "', " _
  142. & "'" & areq.Text & "')"
  143.  
  144. 'MsgBox(SQL)
  145.  
  146. 'SQL = "Insert into booking(name) values('x')"
  147. DC1 = New OleDbCommand(SQL, cN)
  148. DC1.ExecuteNonQuery()
  149. ' MsgBox("Save", MsgBoxStyle.OKOnly, "")
  150.  
  151. cN.Close()
  152. connecttome()
  153. End Sub
  154.  
  155. End Class

its runs fine until this bit where the error occurs..

[code]Private Sub UPDATE1()

cN.Open()
Dim SAZ As String
SAZ = "Update booking set StaffID='" & rooid.Text & "', name='" & date1.Text & "', " _
& "address='" & time1.Text & "' where StaffID='" & stafid.Text & "'"
'MsgBox(SQL)
Dim nwol As New OleDbCommand(SAZ, cN)
nwol.ExecuteNonQuery()
cN.Close()
connecttome()
End Sub[code]

Object reference not set to an instance of an object. at cn.open bit... from what i can see my connection seem fine and i cant see the problem


Note: this code isnt inch perfect i was jus hopin to display any data on the datagrid for a start...

Can anyone suffice the problem?

help need urgentlyyyy
Many thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: c0deFr3aK is an unknown quantity at this point 
Solved Threads: 10
c0deFr3aK's Avatar
c0deFr3aK c0deFr3aK is offline Offline
Junior Poster in Training

Re: Saving and displaying data on datagrid!

 
0
  #2
Apr 19th, 2009
can you specify what LINE gives you that exception?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: kn2790 is an unknown quantity at this point 
Solved Threads: 0
kn2790 kn2790 is offline Offline
Light Poster

Re: Saving and displaying data on datagrid!

 
0
  #3
Apr 20th, 2009
the cn.open in the UPDATE bit
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: kn2790 is an unknown quantity at this point 
Solved Threads: 0
kn2790 kn2790 is offline Offline
Light Poster

Re: Saving and displaying data on datagrid!

 
0
  #4
Apr 20th, 2009
sorted
Last edited by kn2790; Apr 20th, 2009 at 7:54 am. Reason: sorted...
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: kn2790 is an unknown quantity at this point 
Solved Threads: 0
kn2790 kn2790 is offline Offline
Light Poster

Re: Saving and displaying data on datagrid!

 
0
  #5
Apr 20th, 2009
can anyone see the error in this syntax?

  1. Dim SAZ As String
  2. SAZ = "Update Bookings RoomID='" & rooid.Text & "', Date required='" & date1.Text & "', " _
  3. & "'Time required='" & time1.Text & "' StaffID='" & stafid.Text & "'"
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: kn2790 is an unknown quantity at this point 
Solved Threads: 0
kn2790 kn2790 is offline Offline
Light Poster

Re: Saving and displaying data on datagrid!

 
0
  #6
Apr 20th, 2009
anynonee helpp with my syntax error??

at the moment its looking like this

  1. SAZ = "Update Bookings set RoomID= '" & rooid.Text & "', [Date required] '" & date1.Text & "', " _
  2. & "[Time required] '" & time1.Text & "', StaffID '" & stafid.Text & "'"
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: kn2790 is an unknown quantity at this point 
Solved Threads: 0
kn2790 kn2790 is offline Offline
Light Poster

Re: Saving and displaying data on datagrid!

 
0
  #7
Apr 20th, 2009
solved thatt problem... now i have a problem at

Da = New OleDb.OleDbDataAdapter("Select * from bookings where StaffID<>null order by name asc", cN)
        dataS = New DataSet("MS")
        Da.Fill(dataS, "MS")
        DataGridView1.DataSource = dataS
        DataGridView1.DataMember = "MS"


No value given for one or more required parameters
anyone hazard a guess why?
Last edited by kn2790; Apr 20th, 2009 at 8:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: c0deFr3aK is an unknown quantity at this point 
Solved Threads: 10
c0deFr3aK's Avatar
c0deFr3aK c0deFr3aK is offline Offline
Junior Poster in Training

Re: Saving and displaying data on datagrid!

 
0
  #8
Apr 20th, 2009
Originally Posted by kn2790 View Post
anynonee helpp with my syntax error??

at the moment its looking like this

  1. SAZ = "Update Bookings set RoomID= '" & rooid.Text & "', [Date required] '" & date1.Text & "', " _
  2. & "[Time required] '" & time1.Text & "', StaffID '" & stafid.Text & "'"
is should look like this:

  1. SAZ = "Update Bookings set Bookings.RoomID= '" & rooid.Text & "', Bookings.Daterequired = '" & Format(date1.Text, "General Date") & "', " _
  2. & "Bookings.Timerequired = '" & time1.Text & "',Bookings.StaffID = '" & stafid.Text & "' WHERE Bookings.FileID = '" & your strings here &"'"
[/QUOTE]
Do not try to put spaces in your datafield and your query must specify what row to update that is why i supplied the WHERE clause in your statement.

If your Timerequired is a Date/Time Field, you must supply it with the correct Date/Time format in your time1.text, else it will give you another headache.

Tip: you can just have one field for your Daterequired and Timerequired. let say BookingDate so that you will only have one Textbox that will provide the Date and Time of Booking i.e (April 21, 2009 8:00AM)

Just supply the correct parameters and it will solve your problem.
Last edited by c0deFr3aK; Apr 20th, 2009 at 9:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: c0deFr3aK is an unknown quantity at this point 
Solved Threads: 10
c0deFr3aK's Avatar
c0deFr3aK c0deFr3aK is offline Offline
Junior Poster in Training

Re: Saving and displaying data on datagrid!

 
0
  #9
Apr 20th, 2009
Originally Posted by kn2790 View Post
solved thatt problem... now i have a problem at

Da = New OleDb.OleDbDataAdapter("Select * from bookings where StaffID<>null order by name asc", cN)
        dataS = New DataSet("MS")
        Da.Fill(dataS, "MS")
        DataGridView1.DataSource = dataS
        DataGridView1.DataMember = "MS"


No value given for one or more required parameters
anyone hazard a guess why?
Try this:

Da = New OleDb.OleDbDataAdapter("Select * from bookings where StaffID  is not null order by name asc", cN)
        dataS = New DataSet("MS")
        Da.Fill(dataS, "MS")
        DataGridView1.DataSource = dataS
        DataGridView1.DataMember = "MS"
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: kn2790 is an unknown quantity at this point 
Solved Threads: 0
kn2790 kn2790 is offline Offline
Light Poster

Re: Saving and displaying data on datagrid!

 
0
  #10
Apr 21st, 2009
i still get the same error?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum


Views: 1452 | Replies: 22
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC