G
Guest
I have this code in a test program I am playing with, but I receive an object
reference error when I click the Update, Add, or Delete buttons:
Declarations:
' Open a database connection.
Dim strConnection As String = _
"Data Source=localhost;Initial Catalog=Northwind;" _
& "Integrated Security=True"
Dim cn As SqlConnection = New SqlConnection(strConnection)
cn.Open( )
' Create a data adapter object and set its SELECT command.
Dim strSelect As String = _
"SELECT * FROM Categories"
Dim da As SqlDataAdapter = New SqlDataAdapter(strSelect, cn)
' Set the data adapter object's UPDATE, INSERT, and DELETE
' commands. Use the SqlCommandBuilder class's ability to auto-
' generate these commands from the SELECT command.
Dim autogen As New SqlCommandBuilder(da)
' Load a data set.
Dim ds As DataSet = New DataSet( )
da.Fill(ds, "Categories")
' Get a reference to the "Categories" DataTable.
Dim dt As DataTable = ds.Tables("Categories")
Update Button
' Modify one of the records.
Dim row As DataRow = dt.Select("CategoryName = 'Dairy Products'")(0)
row("Description") = "Milk and stuff"
' Update the database.
da.Update(ds, "Categories")
' Close the database connection.
cn.Close( )
Add Button
' Add a record.
row = dt.NewRow( )
row("CategoryName") = "Software"
row("Description") = "Fine code and binaries"
dt.Rows.Add(row)
' Update the database.
da.Update(ds, "Categories")
' Close the database connection.
cn.Close( )
Delete Button
' Delete a record.
row = dt.Select("CategoryName = 'MyCategory'")(0)
row.Delete( )
' Update the database.
da.Update(ds, "Categories")
' Close the database connection.
cn.Close( )
Help!
reference error when I click the Update, Add, or Delete buttons:
Declarations:
' Open a database connection.
Dim strConnection As String = _
"Data Source=localhost;Initial Catalog=Northwind;" _
& "Integrated Security=True"
Dim cn As SqlConnection = New SqlConnection(strConnection)
cn.Open( )
' Create a data adapter object and set its SELECT command.
Dim strSelect As String = _
"SELECT * FROM Categories"
Dim da As SqlDataAdapter = New SqlDataAdapter(strSelect, cn)
' Set the data adapter object's UPDATE, INSERT, and DELETE
' commands. Use the SqlCommandBuilder class's ability to auto-
' generate these commands from the SELECT command.
Dim autogen As New SqlCommandBuilder(da)
' Load a data set.
Dim ds As DataSet = New DataSet( )
da.Fill(ds, "Categories")
' Get a reference to the "Categories" DataTable.
Dim dt As DataTable = ds.Tables("Categories")
Update Button
' Modify one of the records.
Dim row As DataRow = dt.Select("CategoryName = 'Dairy Products'")(0)
row("Description") = "Milk and stuff"
' Update the database.
da.Update(ds, "Categories")
' Close the database connection.
cn.Close( )
Add Button
' Add a record.
row = dt.NewRow( )
row("CategoryName") = "Software"
row("Description") = "Fine code and binaries"
dt.Rows.Add(row)
' Update the database.
da.Update(ds, "Categories")
' Close the database connection.
cn.Close( )
Delete Button
' Delete a record.
row = dt.Select("CategoryName = 'MyCategory'")(0)
row.Delete( )
' Update the database.
da.Update(ds, "Categories")
' Close the database connection.
cn.Close( )
Help!