need help:unable add records with OLEDB

C

Chriz Yuen

Sub Button1_Click(sender As Object, e As EventArgs)
Dim ConnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=" & server.mappath("EzySys.mdb") & ";"
Dim myConn As New OleDbConnection(ConnectionString)
Dim myDataAdapter As New OleDbDataAdapter()
dim mySQL = "select * from resident_info"
myDataAdapter.SelectCommand = New OleDbCommand(mySQL, myConn)
Dim custCB As OleDbCommandBuilder = New
OleDbCommandBuilder(MyDataAdapter)

myConn.Open()

Dim DS As DataSet = New DataSet
myDataAdapter.Fill(DS)
Dim table As DataTable
Dim newRow As DataRow

' Code to modify data in DataSet here
table=DS.Tables("Resident_Info")
newRow=table.NewRow()
newRow("Resident_ID")="SL100"
table.Rows.add(newRow)
myConn.Close()
End Sub




Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
 
C

Cor Ligthert

Chriz,

I made your code more simple. Know that the table names and everything
between quotes what comes from SQL is case sensitive, that was probably your
error. However see all the changes I made here in this message (so watch
typos)

Know that this sub has no sense at all, a webpage is stateless, what means
that you read a database add a new record to a datatable and than all
information is gone when the sub ends and the page on the client side is
refreshed.

\\\
Sub Button1_Click(sender As Object, e As EventArgs)
Dim ConnectionString As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=" & server.mappath("EzySys.mdb") & ";"
Dim myConn As New OleDbConnection(ConnectionString)
dim mySQL = "select * from resident_info"
Dim myDataAdapter As New OleDbDataAdapter(mySQL, myConn)
Try
myConn.Open()
Dim DS As DataSet = New DataSet
myDataAdapter.Fill(DS)
catch ex as exception
me.myfield = ex.tostring 'a label on your form somewhere to test
exit sub
finally
myConn.close
end try
Dim custCB As OleDbCommandBuilder = New
OleDbCommandBuilder(MyDataAdapter)
Dim table As DataTable
Dim newRow As DataRow
' Code to modify data in DataSet here
table=DS.Tables(0)
newRow=table.NewRow()
newRow("Resident_ID")="SL100"
table.Rows.add(newRow)
End Sub
///

I hope this helps something?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top