I am working on filling a datagrid based on results found from a user entering criteria in text boxes and clicking a search button. I have been toying around with Session() to store variables but I dont understand it too well and its giving me trouble. After the user clicks search, the datagrid should only display the desired rows from a database. I can only achieve this by commenting out the line in the load method as follows. If I do not comment the line, the program works, but I get all possible results of the search every time:
My error when I remove the afore mentioned line of code is as follows:
The IListSource does not contain any data sources.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The IListSource does not contain any data sources.
Source Error:
Line 77: softwareGrid.DataMember = "[SOFTWARE DATABASE]"
Line 78: softwareGrid.DataKeyField = "Software #"
Line 79: softwareGrid.DataBind()
Line 80: End Sub
Line 81:
Source File: c:\inetpub\wwwroot\ASPproject\WebApplication2\WebForm1.aspx.vb Line: 79
Stack Trace:
[HttpException (0x80004005): The IListSource does not contain any data sources.]
System.Web.UI.DataSourceHelper.GetResolvedDataSource(Object dataSource, String dataMember)
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
WebApplication2.WebForm1.BindDataGrid() in c:\inetpub\wwwroot\ASPproject\WebApplication2\WebForm1.aspx.vb:79
WebApplication2.WebForm1.softwareGrid_EditCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\ASPproject\WebApplication2\WebForm1.aspx.vb:193
System.Web.UI.WebControls.DataGrid.OnEditCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
My code is as follows, with the commented line marked with ***:
Dim dsSoftware As DataSet = New DataSet()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
dsSoftware = SoftwareDB.getEntries
Me.BindDataGrid()
Session("dsSoftware") = dsSoftware
Else
'dsSoftware = Session("dsSoftware") *** This is the line that would allow things to work, but causes too many search returns.
End If
End Sub
Private Sub BindDataGrid()
softwareGrid.DataSource = dsSoftware
softwareGrid.DataMember = "[SOFTWARE DATABASE]"
softwareGrid.DataKeyField = "Software #"
softwareGrid.DataBind()
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal E As System.EventArgs) Handles btnSearch.Click
Dim Connect As OleDbConnection = New OleDbConnection()
Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter()
Dim SelectStatement, ConnectString As String
Dim WhereClause As String
WhereClause = "Where "
If txtSoftwareNum.Text <> "" Then
WhereClause = WhereClause & "InStr([Software #],'" & _
txtSoftwareNum.Text & "')>0 AND "
End If
If txtSoftwareName.Text <> "" Then
WhereClause = WhereClause & "InStr([Software Name],'" & _
txtSoftwareName.Text & "')>0 AND "
End If
If txtVersion.Text <> "" Then
WhereClause = WhereClause & "InStr(Version,'" & _
txtVersion.Text & "')>0 AND "
End If
If txtLocation.Text <> "" Then
WhereClause = WhereClause & "InStr(Location,'" & _
txtLocation.Text & "')>0 AND "
End If
If txtSoftwareBrand.Text <> "" Then
WhereClause = WhereClause & "InStr([Soft Brand],'" & _
txtSoftwareBrand.Text & "')>0 AND "
End If
If txtDatePurchased.Text <> "" Then
WhereClause = WhereClause & "InStr(DatePurchased,'" & _
txtDatePurchased.Text & "')>0 AND "
End If
If txtFirstName.Text <> "" Then
WhereClause = WhereClause & "InStr(FirstName,'" & _
txtFirstName.Text & "')>0 AND "
End If
If txtLastName.Text <> "" Then
WhereClause = WhereClause & "InStr(LastName,'" & _
txtLastName.Text & "')>0 AND "
End If
If txtSerialNumber.Text <> "" Then
WhereClause = WhereClause & "InStr([Serial Number],'" & _
txtSerialNumber.Text & "')>0 AND "
End If
If txtModel.Text <> "" Then
WhereClause = WhereClause & "InStr(Model,'" & _
txtModel.Text & "')>0 AND "
End If
If Right(WhereClause, 4) = "AND " Then
WhereClause = Left(WhereClause, Len(WhereClause) - 4)
End If
SelectStatement = "Select * From [SOFTWARE DATABASE] " & WhereClause
' If they didnt enter anything in the textboxes:
If txtSoftwareNum.Text = "" And txtSoftwareName.Text = "" And _
txtVersion.Text = "" And txtLocation.Text = "" And _
txtSoftwareBrand.Text = "" And txtDatePurchased.Text = "" And _
txtFirstName.Text = "" And txtLastName.Text = "" And _
txtSerialNumber.Text = "" And txtModel.Text = "" Then
SelectStatement = "Select * From [SOFTWARE DATABASE]"
Else
End If
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Inetpub\wwwroot\ASPproject\WebApplication2\software\softwaredb.mdb"
Connect.ConnectionString = ConnectString
Adapter.SelectCommand = New OleDbCommand(SelectStatement, Connect)
Adapter.SelectCommand.Connection.Open()
Adapter.Fill(dsSoftware, "[SOFTWARE DATABASE]")
softwareGrid.DataSource = dsSoftware.Tables("[SOFTWARE DATABASE]")
Page.DataBind()
End Sub
Public Sub softwareGrid_EditCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles softwareGrid.EditCommand
If Session("Add Mode") = True Then
Dim i As Integer = dsSoftware.Tables("[SOFTWARE DATABASE]").Rows.Count - 1
dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(i).Delete()
Session("AddMode") = False
End If
softwareGrid.EditItemIndex = e.Item.ItemIndex
Me.BindDataGrid()
End Sub
Public Sub softwareGrid_CancelCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles softwareGrid.CancelCommand
If Session("AddMode") = True Then
Dim i As Integer = dsSoftware.Tables("[SOFTWARE DATABASE]").Rows.Count - 1
dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(i).Delete()
Session("AddMode") = False
End If
softwareGrid.EditItemIndex = -1
Me.BindDataGrid()
End Sub
Public Sub softwareGrid_UpdateCommand(ByVal source As Object, ByVal E As DataGridCommandEventArgs) Handles softwareGrid.UpdateCommand
Dim tbSoftwareNum As String
tbSoftwareNum = E.Item.Cells(0).Text
Dim tbSoftwareName As String
tbSoftwareName = CType(E.Item.Cells(1).Controls(0), TextBox).Text
Dim tbVersion As String
tbVersion = CType(E.Item.Cells(2).Controls(0), TextBox).Text
Dim tbLocation As String
tbLocation = CType(E.Item.Cells(3).Controls(0), TextBox).Text
Dim tbSoftBrand As String
tbSoftBrand = CType(E.Item.Cells(4).Controls(0), TextBox).Text
Dim tbDatePurchased As String
tbDatePurchased = CType(E.Item.Cells(5).Controls(0), TextBox).Text
Dim tbFirstName As String
tbFirstName = CType(E.Item.Cells(6).Controls(0), TextBox).Text
Dim tbLastName As String
tbLastName = CType(E.Item.Cells(7).Controls(0), TextBox).Text
Dim tbSerialNumber As String
tbSerialNumber = CType(E.Item.Cells(8).Controls(0), TextBox).Text
Dim tbModel As String
tbModel = CType(E.Item.Cells(9).Controls(0), TextBox).Text
If ValidEntry(tbSoftwareNum, tbSoftwareName, tbLocation) Then
Debug.WriteLine("The row index is:")
Debug.WriteLine(E.Item.ItemIndex)
Debug.WriteLine("This is ds:")
Debug.WriteLine(dsSoftware.GetXml)
Dim dr As DataRow = dsSoftware.Tables("[SOFTWARE DATABASE]").Rows(E.Item.ItemIndex)
Try
dr("Software #") = tbSoftwareNum
dr("Software Name") = tbSoftwareName
dr("Version") = tbVersion
dr("Location") = tbLocation
dr("Soft Brand") = tbSoftBrand
dr("DatePurchased") = tbDatePurchased
dr("FirstName") = tbFirstName
dr("LastName") = tbLastName
dr("Serial Number") = tbSerialNumber
dr("Model") = tbModel
Me.UpdateDataBase()
softwareGrid.EditItemIndex = -1
Me.BindDataGrid()
Session("AddMode") = False
Catch eConstraint As ConstraintException
lblMessage.Text = "A category with that ID already exists."
End Try
End If
End Sub
Private Function ValidEntry(ByVal SoftwareNum As String, ByVal SoftwareName As String, _
ByVal Location As String) As Boolean
ValidEntry = True
If SoftwareNum = "" Then
ValidEntry = False
lblMessage.Text = "Software Number is required."
ElseIf Len(SoftwareNum) > 7 Then
ValidEntry = False
lblMessage.Text = "Software Number must be 7 characters or less."
ElseIf SoftwareName = "" Then
ValidEntry = False
lblMessage.Text = "Software Name is required."
ElseIf Location = "" Then
ValidEntry = False
lblMessage.Text = "A Location for where the software is stored must be specified."
End If
End Function
Private Sub UpdateDataBase()
Select Case SoftwareDB.UpdateEntries(dsSoftware)
Case SoftwareDB.UpdateResult.ConcurrencyError
lblMessage.Text = "Another user has updated that category. Please try again."
Case SoftwareDB.UpdateResult.ForeignKeyError
lblMessage.Text = "That entry is in use."
Case SoftwareDB.UpdateResult.PrimaryKeyError
lblMessage.Text = "Another user has added a category with that software number."
Case SoftwareDB.UpdateResult.OtherOleDbError
lblMessage.Text = "An unspecified OleDb Server error has occurred."
End Select
dsSoftware = SoftwareDB.getEntries
Session("dsSoftware") = dsSoftware
End Sub
There it is. Is there anyway to configure Session() without redirecting the search results to another page? Anyone know?