i am trying to insert value using dataset but its giving me an error of null point exception(object was not created) at run time

'my code is

Dim cnCustomers As OleDbConnection

        cnCustomers = New OleDbConnection

        Try
            With cnCustomers
                '  If .State = ConnectionState.Open Then .Close()

                .ConnectionString = cnString
                .Open()
            End With
        Catch ex As OleDbException
            MsgBox(ex.ToString)
        End Try

        Dim dt As New DataTable
        dt = dsstudent.Tables("survey1")

      Dim dsnewRow As DataRow ---- giving error at this point 
            dsnewRow = dt.NewRow()

Error is :-(System.NullReferenceException)
tip is "Use the New keyword to create the instance. " at dim dt as new table


did it but not working

Edited 4 Years Ago by __avd: Added [code] tags.

What is dsStudent, I assume it is a dataset but I cant see you initialising it or declaring it. I se you opening a connection string then suddenly you're setting datatables....

The easiest way to insert a record into a table is by ADO as follows

Dim con As New ADODB.Connection
con.Open("Driver={SQL Server};Server=JIM-PC\SQLEXPRESS;Database=mydb;Trusted_Connection=yes;")

Dim query As String = "insert into Table1 (RegistrationNo,Field1,Field2) values(12,'John','Doe')"

con.Execute(query)
con.Close()

In this case I am inserting into a SQL Express 2008 table "Table1" in the database "mydb". That table has three fields named "RegistrationNo", "Field1" and "Field2".

i am trying to insert value using dataset but its giving me an error of null point exception(object was not created) at run time

'my code is

Dim cnCustomers As OleDbConnection

cnCustomers = New OleDbConnection

Try
With cnCustomers
' If .State = ConnectionState.Open Then .Close()

.ConnectionString = cnString
.Open()
End With
Catch ex As OleDbException
MsgBox(ex.ToString)
End Try

Dim dt As New DataTable
dt = dsstudent.Tables("survey1")

Dim dsnewRow As DataRow ---- giving error at this point
dsnewRow = dt.NewRow()
Error is :-(System.NullReferenceException)
tip is "Use the New keyword to create the instance. " at dim dt as new table


did it but not working

It's better show us your code completely... You're using dsstudent without declaring it. And also your connectionString :)

this is my code tell me where m wrong !!

Imports System.Data.OleDb
Public Class Form1
    Dim daCustomers As New OleDbDataAdapter()
    Dim dsstudent As New DataSet()
    Public Const cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=E:\crawler\survey.mdb"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cnCustomers As OleDbConnection

        cnCustomers = New OleDbConnection

        Try
            With cnCustomers
                '  If .State = ConnectionState.Open Then .Close()

                .ConnectionString = cnString
                .Open()
            End With
        Catch ex As OleDbException
            MsgBox(ex.ToString)
        End Try

        Dim dt As New DataTable
        dt = dsstudent.Tables("survey1")

        If TextBox1.Text = "" Or TextBox3.Text = "" Then
            MsgBox("Please fill up ID or information.", MsgBoxStyle.Critical)

            Exit Sub
        End If


        ' If State = gModule.FormState.adStateAddMode Then
        Try   ' add a row

            Dim j As Integer
            Dim k As Integer = 0
            'Dim dsnewRow As DataRow = dsstudent.Tables("survey").NewRow
            Dim dsnewRow As DataRow
            dsnewRow = dt.NewRow()
            'dsnewRow
            For i = 1 To 10

                dsnewRow("ID") = TextBox1.Text
                dsnewRow("deptt") = TextBox3.Text
                dsnewRow("survey") = "1"
                dsnewRow("Question") = 1

                For j = 1 To 4
                    k = k + 1
                    If CType(Me.Controls.Find("RadioButton" & k, True)(0), RadioButton).Checked = True Then
                        dsnewRow("Answer") = CType(Me.Controls.Find("RadioButton" & j, True)(0), RadioButton).Text
                    End If
                Next j
                
                dt.Rows.Add(dsnewRow)

            Next i
           
            daCustomers.Update(dsstudent, "survey")

            MsgBox("Record successfully saved.", MsgBoxStyle.Information)
        Catch ex As OleDbException
            MsgBox(ex.ToString)
        End Try
    End Sub
    

End Class

Edited 4 Years Ago by __avd: Added [code] tags.

You have not populated the dataset so there are no tables in dsStudent.This is why you are getting an error tryng to use a table that does not exist. You must populate your dataset before using it.

You need to use an OLEDb Data adapter to populate your dataset.

This article has been dead for over six months. Start a new discussion instead.