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...
Imports System.Data.OleDb
Public Class Addbooking
Inherits System.Windows.Forms.Form
Public cN As OleDbConnection
Public myOleDbCommand As OleDbCommand
Dim Da As New OleDbDataAdapter()
Dim dataS As New DataSet()
Dim Nextb As Long
Dim PrevB As Long
Dim TotalC As Long
Private Sub Addbooking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'LVuserdbDataSet1.Bookings' table. You can move, or remove it, as needed.
Me.BookingsTableAdapter.Fill(Me.LVuserdbDataSet1.Bookings)
End Sub
Private Sub connecttome()
Nextb = 0
PrevB = 0
Dim STrPath = System.IO.Directory.GetCurrentDirectory & "\mydb.mdb"
Try
cN = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & STrPath & "';Jet OLEDB:Database Password=1")
myOleDbCommand = New OleDbCommand("Select * from bookings where StaffID<>null order by name asc")
cN.Open()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "")
End
End Try
myOleDbCommand.Connection = cN
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"
Dim x = dataS.Tables("ms").Rows.Count()
TotalC = x - 1
FILL(0)
Dim Dr As OleDb.OleDbDataReader = myOleDbCommand.ExecuteReader
cN.Close()
Button11()
End Sub
Private Sub FILL(ByVal X As Long)
If dataS Is Nothing Then Return
Try
With dataS.Tables("ms").Rows(X)
stafid.Text = IIf(IsDBNull(.Item("Staff ID")), "NULL", .Item("Staff ID"))
rooid.Text = IIf(IsDBNull(.Item("Room ID")), "NULL", .Item("Room ID"))
date1.Text = IIf(IsDBNull(.Item("Date Required")), "NULL", .Item("Date Required"))
time1.Text = IIf(IsDBNull(.Item("Time Required")), "NULL", .Item("Time Required"))
areq.Text = IIf(IsDBNull(.Item("Additional requirements")), "NULL", .Item("Additional requirements"))
End With
Catch err As Exception
End Try
Me.Text = X
End Sub
Private Sub Button11()
btnaddbook.Enabled = True
If TotalC <= 0 Then
btnaddbook.Enabled = True
Else
btnaddbook.Enabled = False
End If
End Sub
Private Sub btnaddbook_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddbook.Click
If rooid.Text = "" Then
MsgBox("Invalid StaffID!", MsgBoxStyle.Information, "")
rooid.Focus()
Exit Sub
End If
If date1.Text = "" Then date1.Text = " "
If time1.Text = "" Then time1.Text = " "
If stafid.Text = "" Then
SAVE()
Else
UPDATE1()
End If
End Sub
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
Private Sub SAVE()
Dim DC1 As New OleDb.OleDbCommand()
Dim SQL As String
cN.Open()
'Stop
Dim Ad As New OleDbDataAdapter("select * from bookings where StaffID='" & rooid.Text & "'", cN)
Dim Check_dataset As DataSet
Check_dataset = New DataSet("check")
Ad.Fill(Check_dataset, "check")
If Check_dataset.Tables("check").Rows.Count <> 0 Then
MsgBox("Item StaffID already exists!", MsgBoxStyle.Information, "")
rooid.SelectionStart = 0
rooid.SelectionLength = Len(rooid.Text)
rooid.Focus()
cN.Close()
Exit Sub
End If
SQL = "Insert into Bookings(StaffID,RoomID,Date required,Time required,Additional requirements) values(" _
& "'" & stafid.Text & "', " _
& "'" & rooid.Text & "', " _
& "'" & date1.Text & "', " _
& "'" & time1.Text & "', " _
& "'" & areq.Text & "')"
'MsgBox(SQL)
'SQL = "Insert into booking(name) values('x')"
DC1 = New OleDbCommand(SQL, cN)
DC1.ExecuteNonQuery()
' MsgBox("Save", MsgBoxStyle.OKOnly, "")
cN.Close()
connecttome()
End Sub
End Class
its runs fine until this bit where the error occurs..
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
Object reference not set to an instance of an object. at cn.open bit... from what i can see my connection seem fine :S 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