K.Moore's 9 steps for easy web grid
OK I am pretty new to VB.NET and ASP.NET and I need to create a web form to update and edit and delete data from a table. I found this link: http://www.developer.com/net/asp/article.php/3314411
which is pretty much what i need. However upon compiling I get an object not referenced error. It has to do with step8 and adding these functions
Public Sub DataSave(ByVal DataSet As DataSet)
If DataExists() Then
ViewState.Item("__Data") = DataSet
Else
ViewState.Add("__Data", DataSet)
End If
End Sub
Public Function DataRetrieve() As DataSet
Return CType(ViewState.Item("__Data"), DataSet)
End Function
Public Function DataExists() As Boolean
If Not ViewState.Item("__Data") Is Nothing Then Return True
End Function
The error happens with the DataExists function
I have pasted the source code below for anyone to look at - maybe I need to add those functions in the actual viewstate property area and use the get/set? Like I said I am new to this and any help would be greatly appreciated.
Thanks in advance!! Bryan
CODE BELOW and in attachment(minus windows generated code)=======
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Public Sub DataSave(ByVal DataSet As DataSet)
If DataExists() Then
ViewState.Item("dsAuthors") = DataSet
Else
ViewState.Add("dsAuthors", DataSet)
End If
End Sub
Public Function DataRetrieve() As DataSet
Return CType(ViewState.Item("dsAuthors"), DataSet)
End Function
Public Function DataExists() As Boolean
If Not ViewState.Item("dsAuthors") Is Nothing _
Then Return True
End Function
WINDOWS GENERATED CODE
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
daAuthors.Fill(dsAuthors)
dgAuthors.DataSource = dsAuthors
dgAuthors.DataBind()
datasave(dsAuthors)
End If
End Sub
Private Sub SqlConnection1_InfoMessage(ByVal sender As System.Object, ByVal e As System.Data.SqlClient.SqlInfoMessageEventArgs) Handles cnAuthors.InfoMessage
End Sub
Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub dgAuthors_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgAuthors.SelectedIndexChanged
Dim intCount As Integer
For intCount = 1 To dgAuthors.Items.Count
dgAuthors.Items(intCount - 1).BorderStyle = BorderStyle.Groove
Next
dgAuthors.SelectedItem.BorderStyle = BorderStyle.Dashed
End Sub
Protected Overrides ReadOnly Property ViewState() As System.Web.UI.StateBag
Get
End Get
End Property
Public Overrides ReadOnly Property Session() As System.Web.SessionState.HttpSessionState
Get
End Get
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Code to respond to the Click event of the ADD button:
' Desc: Adds a new row to the DataSet, rebinds to the
' DataGrid, then makes the row editable
'If DataExists() = False Then Exit Sub
'dsAuthors = DataRetrieve()
Dim rowNew As System.Data.DataRow = dsAuthors.Tables(0).NewRow
' Enter sample values for non-null fields here
' ie, rowNew.Item("uniqueTag") = "sample"
' Alternatively, use separate text boxes for input
' and add field values in code, as above.
dsAuthors.Tables(0).Rows.Add(rowNew)
dgAuthors.EditItemIndex = dgAuthors.Items.Count
dgAuthors.DataSource = dsAuthors
dgAuthors.DataBind()
'DataSave(dsAuthors)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Code to respond to the Click event of the DELETE button:
' Desc: Deletes the selected row, updates the DataSet, then
' rebinds
'If DataExists() = False Then Exit Sub
If dgAuthors.SelectedIndex = -1 Then Exit Sub
'dsAuthors = DataRetrieve()
dsAuthors.Tables(0).Rows(dgAuthors.SelectedIndex).Delete()
dgAuthors.EditItemIndex = -1
dgAuthors.SelectedIndex = -1
dgAuthors.DataSource = dsAuthors
dgAuthors.DataBind()
'DataSave(dsAuthors)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
' Code to respond to the Click event of the EDIT button:
' Desc: Makes the selected row editable, then rebinds
'If DataExists() = False Then Exit Sub
If dgAuthors.SelectedIndex = -1 Then Exit Sub
'Dim dsAuthors As DataSet = DataRetrieve()
dgAuthors.DataSource = dsAuthors
dgAuthors.EditItemIndex = dgAuthors.SelectedIndex
dgAuthors.DataBind()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
' Code to respond to the Click event of the OK button:
' Desc: Cycles through the TextBox controls used during a
' standard edit, puts the values back in the DataSet, then
' rebinds. Add error handling as appropriate.
' NOTE: This code relies on the first column being a
' selection (>) button (it starts counting the cells from
' position 1, not 0). If you remove that button, you may
' have to change this code.
'If DataExists() = False Then Exit Sub
If dgAuthors.EditItemIndex = -1 Then Exit Sub
Dim intCount As Integer
'Dim dsAuthors As DataSet = DataRetrieve()
With dgAuthors
For intCount = 1 To .Items(.EditItemIndex).Cells.Count
If intCount = .Items(.EditItemIndex).Cells.Count _
Then Exit For
' Check that a control exists in this position
If .Items(.EditItemIndex).Cells(intCount).Controls. _
Count Then
' Check for a standard TextBox
If TypeOf (.Items(.EditItemIndex).Cells(intCount). _
Controls(0)) _
Is TextBox Then
Dim strValue As String = CType(.Items(. _
EditItemIndex). _
Cells(intCount).Controls(0), TextBox).Text
If strValue <> "" Then
' This isn't null, so store value
dsAuthors.Tables(0).Rows(.EditItemIndex).Item( _
intCount - 1) = strValue
Else
' Treat empty value as null
dsAuthors.Tables(0).Rows(.EditItemIndex).Item( _
intCount - 1) = System.DBNull.Value
End If
End If
End If
Next
.SelectedIndex = -1
.EditItemIndex = -1
'DataSave(dsAuthors)
.DataSource = dsAuthors
.DataBind()
End With
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
' Code to respond to the Click event of the CANCEL button:
' Desc: Used to cancel an edit. Deselects an selected rows
' and exits the edit mode, then rebinds.
'If DataExists() = False Then Exit Sub
dgAuthors.SelectedIndex = -1
dgAuthors.EditItemIndex = -1
'dgAuthors.DataSource = DataRetrieve()
dgAuthors.DataBind()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
' Code to respond to the Click event of the UPDATE button:
' Desc: Updates the underlying database, then rebinds.
' Add error handling code as appropriate.
'If DataExists() = False Then Exit Sub
'daAuthors.Update(DataRetrieve)
'dgAuthors.DataSource = DataRetrieve()
dgAuthors.DataBind()
End Sub
End Class