0

I am writing a very basic database input form for a sql database using vb.net

upon executing the code

dsnewrow = ds.Tables("Car Details").NewRow()

i am declaring dsnewrow just before this in the same sub here:

 Dim dsnewrow As DataRow

and ds is being declared here:

Dim ds As DataSet

in the public class of the form.

I understand that there is a problem with my ds variable, but I don't understand what it is,can anyone help?

full program:

Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Class Form1
    Dim inc As Integer
    Dim ds As New DataSet
    Dim da As SqlDataAdapter
    Dim maxrows As Integer

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

        If MessageBox.Show("Are you sure that you want to delete this record?", _
                           "Delete", MessageBoxButtons.YesNo, _
                           MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
            MsgBox("Operation Cancelled")
        Else
            ds.Tables("Car Details").Rows(inc).Delete()
            maxrows = maxrows - 1
            inc = 0
            NavigateRecords()
            da.Update(ds, "authors2")
        End If

    End Sub

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Car_DetailsTableAdapter.Fill(Me.CourseworkDataSet.Car_Details)
        Dim sqlstring As String
        Dim conn As SqlConnection
        Dim connstring As String


        connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Daniel\Documents\Coursework.mdf;Integrated Security=True;Connect Timeout=30"

        conn = New SqlConnection(connstring)
        conn.Open()
        MsgBox("successful")

        sqlstring = "SELECT * FROM Car Details"
        da = New SqlDataAdapter(sqlstring, conn)
        ds = New DataSet
        da.Fill(ds, "Car Details")

        conn.Close()
        conn = Nothing

        maxrows = ds.Tables("Car Details").Rows.Count
        inc = 0
        navigaterecords()
    End Sub

    Private Sub Car_DetailsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Car_DetailsBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Car_DetailsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CourseworkDataSet)

    End Sub

    Private Sub NavigateRecords()
        MakeTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(0)
        ModelTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(1)
        Engine_SizeTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(2)
        ColourTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(3)
        DoorsTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(4)
        Buy_PriceTextBox.Text = ds.Tables("Car Details").Rows(inc).Item(5)
        Sell_PriceTextBox = ds.Tables("Car Details").Rows(inc).Item(6)

    End Sub

    Private Sub btnFwd_Click(sender As Object, e As EventArgs) Handles btnFwd.Click
        If inc <> maxrows - 1 Then
            inc = inc + 1
            NavigateRecords()
        Else
            MsgBox("No More Records")

        End If
    End Sub

    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        If inc > 0 Then
            inc = inc - 1
            NavigateRecords()
        Else
            MsgBox("First Record")
        End If
    End Sub

    Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
        If inc <> 0 Then
            inc = 0
            NavigateRecords()
        End If

    End Sub

    Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
        If inc <> maxrows - 1 Then
            inc = maxrows - 1
            NavigateRecords()

        End If
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
        ds.Tables("Car Details").Rows(inc).Item(0) = MakeTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(1) = ModelTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(2) = Engine_SizeTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(3) = ColourTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(4) = DoorsTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(5) = Buy_PriceTextBox.Text
        ds.Tables("Car Details").Rows(inc).Item(6) = Sell_PriceTextBox.Text

    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        btnUpdate.Enabled = False
        btnAdd.Enabled = False
        btnConfirm.Enabled = True
        btnDelete.Enabled = False

        ModelTextBox.Clear()
        MakeTextBox.Clear()
        Engine_SizeTextBox.Clear()
        ColourTextBox.Clear()
        DoorsTextBox.Clear()
        Buy_PriceTextBox.Clear()
        Sell_PriceTextBox.Clear()
    End Sub

    Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
        If inc <> -1 Then
            Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)
            Dim dsnewrow As DataRow

            dsnewrow = ds.Tables("Car Details").NewRow()

            dsnewrow.Item("Make") = MakeTextBox.Text
            dsnewrow.Item("Model") = ModelTextBox.Text
            dsnewrow.Item("Engine Size") = Engine_SizeTextBox.Text
            dsnewrow.Item("Colour") = ColourTextBox.Text
            dsnewrow.Item("Doors") = DoorsTextBox.Text
            dsnewrow.Item("Buy Price") = Buy_PriceTextBox.Text
            dsnewrow.Item("Sell Price") = Sell_PriceTextBox.Text

            btnConfirm.Enabled = False
            btnAdd.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True

        End If
    End Sub

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        btnUpdate.Enabled = True
        btnAdd.Enabled = True
        btnConfirm.Enabled = True
        btnDelete.Enabled = True
        inc = 0
        NavigateRecords()
    End Sub
    End Class
4
  • can you show more of your code please? Commented Aug 13, 2013 at 16:23
  • Dim ds As new DataSet Commented Aug 13, 2013 at 16:24
  • Can post the error message you get? Commented Aug 13, 2013 at 16:46
  • the error is the title of the question Commented Aug 13, 2013 at 22:53

2 Answers 2

2

You are only declaring the variable ds. You should create an instance, before you can use the methods.

Dim ds As DataSet

should be

Dim ds As New DataSet

or

Dim ds As DataSet
ds = New DataSet
Sign up to request clarification or add additional context in comments.

Comments

2

This line in Form_Load is wrong

sqlstring = "SELECT * FROM Car Details"

A table name when contains spaces should be enclosed in square brackets

sqlstring = "SELECT * FROM [Car Details]"

when you execute the query it doesn't work and your dataset is never initialized with new. Then when you try to use it you get the NULL Reference Exception. I suppose that you run this code on a 64bit system where the exceptions in a Form_Load event are ignored

5 Comments

Correcting that still returns the same error, but thank you for picking up on that, clearly I need to read more around my lecture material!
It fails on the same line?
yeah, exactly the same place
I suggest to put a breakpoint in that line and when hit, hover with your cursor on the variable ds to see if it is null, Check also if you have a table named Car Details in your dataset
ds is [system.data.dataset]. I have a table calld Car Details in the Data Connections panel,and I have the table present in the data set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.