1,105,633 Community Members

Adding Rows to a database error

Member Avatar
Grandiago
Newbie Poster
15 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm trying to add a new row to my database so that i can add a new record. But it always gives this error: "Object reference not set to an instance of an object." And i can't figure out. Can you guys help me? I ahve my codee.

    Private Sub StudentInfo()
        If inc <> -1 Then
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow

            dsNewRow = ds.Tables("StudentInfoDataSet").NewRow()
            dsNewRow.Item(1) = txtFname.Text
            dsNewRow.Item(2) = txtMname.Text
            dsNewRow.Item(3) = txtSurname.Text
            dsNewRow.Item(4) = txtAddress.Text
            dsNewRow.Item(5) = cmbSex.Text
            dsNewRow.Item(6) = dtpBirthdate.Text
            dsNewRow.Item(7) = txtBirthplace.Text
            dsNewRow.Item(8) = txtCitizenship.Text
            dsNewRow.Item(9) = txtReligion.Text
            dsNewRow.Item(10) = txtContactNo.Text
            dsNewRow.Item(11) = txtStudentNo.Text
            dsNewRow.Item(12) = txtSY.Text
            dsNewRow.Item(13) = cmbYL.Text
            dsNewRow.Item(14) = cmbStatus.Text
            ds.Tables("StudentInfoDataSet").Rows.Add(dsNewRow)
            da.Update(ds, "StudentInfoDataSet")
        End If

    End Sub
Member Avatar
__avd
Posting Genius (adatapost)
6,703 posts since Oct 2008
Reputation Points: 1,811 [?]
Q&As Helped to Solve: 1,272 [?]
Skill Endorsements: 61 [?]
Moderator
 
0
 

You should have to include "Error/Exception" trace to your post. The error says that you've some reference variables that contains null. Please verify ds and da variables.

Use Using to ensure the ADO resources are disposed properly:

Using Cn As New OleDb.OleDbConnection
     Using Cmd As New OleDb.OleDbCommand
       Cn.ConnectionString = "your_connection_string"
       Cmd.CommandText = "INSERT INTO SAMPLE (NO,NAME) VALUES (@no,@name)"

       Cmd.Parameters.AddWithValue("@no",10)
       Cmd.Parameters.AddWithValue("@name","foo")

       Cn.Open()
       Cmd.ExecuteNonQuery()
       Cn.Close()
     End Using
 End Using
Member Avatar
Grandiago
Newbie Poster
15 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm sorry about the incomplete code. If i'm gonna use that code, will i have to change my database and database provider? I'm not really familiar with ADO, cause our prof rarely taughts and only taught me about using access. So will your code change a whole lot of my program??

Member Avatar
Grandiago
Newbie Poster
15 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I made a more simplier way to add. But it still doesn't work. This is the whole code, and it still messes up. Im using microsoft Visual studio and access as my compiler and data base. im supposed to create a program that allows me to add a new record at an existing database. but i always get this error: "Object reference not set to an instance of an object." on this line: "dsNewRow = ds.Tables("Sample").NewRow()" why is this happening?

here is a sample of my code:

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
       Dim cmd As New OleDb.OleDbCommand
       Dim adp As New OleDb.OleDbDataAdapter
       Dim ds As New DataSet
       Dim inc As Integer
       Dim da As New OleDb.OleDbDataAdapter
       Dim cb As New OleDb.OleDbCommandBuilder(da)
       Dim con As New OleDb.OleDbConnection
       Dim dbProvider As String
       Dim dbSource As String
       Dim Sql As String


       dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
       dbSource = "Data Source = C:\Documents and Settings\Migs\MyDocuments\SAMPLE.accdb"

       Sql = "SELEcT * FROM tblSample"
       da = New OleDb.OleDbDataAdapter(Sql, con)

       con.ConnectionString = dbProvider & dbSource


       con.Open()

       If inc <> -1 Then

       Dim dsNewRow As DataRow

       dsNewRow = ds.Tables("Sample").NewRow()

       dsNewRow.Item("FName") = txtFname.Text
       dsNewRow.Item("LName") = txtLName.Text

       ds.Tables("Sample").Rows.Add(dsNewRow)
       da.Update(ds, "Sample")

       End If


       con.Close()

    End Sub
Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

You are trying to add a row to a datatable called "Sample". This datatable doesn't exist -yet.
First fill your datatable - which will create it- and then try to add a new row to it.
Alternatively you might want to think inserting the record to your db without the dataset/datatable & dataadapter.

By the way, since you didn't assign a value to inc, the If inc <> -1 will always return true.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: