| | |
Saving and displaying data on datagrid!
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
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...
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
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...
VB.NET Syntax (Toggle Plain Text)
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..
[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 problemNote: 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
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
can anyone see the error in this syntax?
VB.NET Syntax (Toggle Plain Text)
Dim SAZ As String SAZ = "Update Bookings RoomID='" & rooid.Text & "', Date required='" & date1.Text & "', " _ & "'Time required='" & time1.Text & "' StaffID='" & stafid.Text & "'"
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
anynonee helpp with my syntax error??
at the moment its looking like this
at the moment its looking like this
VB.NET Syntax (Toggle Plain Text)
SAZ = "Update Bookings set RoomID= '" & rooid.Text & "', [Date required] '" & date1.Text & "', " _ & "[Time required] '" & time1.Text & "', StaffID '" & stafid.Text & "'"
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
solved thatt problem... now i have a problem at
No value given for one or more required parameters
anyone hazard a guess why?
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.
•
•
•
•
anynonee helpp with my syntax error??
at the moment its looking like this
VB.NET Syntax (Toggle Plain Text)
SAZ = "Update Bookings set RoomID= '" & rooid.Text & "', [Date required] '" & date1.Text & "', " _ & "[Time required] '" & time1.Text & "', StaffID '" & stafid.Text & "'"
VB.NET Syntax (Toggle Plain Text)
SAZ = "Update Bookings set Bookings.RoomID= '" & rooid.Text & "', Bookings.Daterequired = '" & Format(date1.Text, "General Date") & "', " _ & "Bookings.Timerequired = '" & time1.Text & "',Bookings.StaffID = '" & stafid.Text & "' WHERE Bookings.FileID = '" & your strings here &"'"
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.
•
•
•
•
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?
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"![]() |
Similar Threads
Other Threads in the VB.NET Forum
- Previous Thread: Out of Process Server in VB.NET?
- Next Thread: Sync DataGridView to Database
Views: 1452 | Replies: 22
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net 2005 2008 access account application arithmetic array arrays basic bing button buttons c# center check checkbox code combobox component convert crystalreport data database databasesearch datagrid datagridview design designer dissertation dissertations dropdownlist excel file-dialog folder ftp generatetags google gridview hardcopy highlighting images inline insert installer intel internet listview mobile monitor ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project save searchbox searchvb.net select serial server soap sorting studio syntax table tcp text textbox time timer toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





