For one, the DataSource property of any web control does not understand what a string is. In your your code, you are storing a string in a session and then assigning the Session to the DataBind method. The DataSource property understands certain collection types or interfaces. For instance, you could have assigned the Data Reader directly:
DataGrid1.DataSource = dtrResults
DataGrid1.DataBind()
dtrResults .Close
Next off, unless you are using the value on another page, the Session variable is useless. Once the DataGrid displays the value, it will preserve it in ViewState. In fact, you only have to create the DataGrid on the first page post, in the page load event:
Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
' Create DataGrid
End If
End Sub
: Hi all,
:
: I have pasted the following code from my form up until the DataGrid. At runtime, the browser shows the DataGrid but doesn't return an item from the database which the user has successfully searched for. Does anyone know why please?
:
:
: <%@ Page Language="VB" %>
:
:
: <%@ import Namespace="System.Data" %>
:
:
: <%@ import Namespace="System.Data.OleDb" %>
:
:
: <script runat="server">
:
:
:
:
:
: Sub Button1_Click(sender As Object, e As EventArgs)
:
:
:
:
:
: If TextBox1.Text = "" Then
:
:
: lblError.visible = True
:
:
: Else
:
:
:
:
:
: Dim DBConn as OleDbConnection
:
:
: Dim dtrResults as OleDBDataReader
:
:
: Dim DSLogin as New DataSet
:
:
:
:
:
: DBConn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
:
:
: & "DATA SOURCE=" _
:
:
: & Server.MapPath("/Back/UniDaysDB.mdb;"))
:
:
:
:
:
: DBConn.Open()
:
:
: Dim CmdStr As String
:
:
: Dim DBSelect As New OleDbCommand
:
:
:
:
:
: CmdStr =("SELECT Venue_Name FROM VenueDetails WHERE Venue_Name = @VenueName")
:
:
:
:
:
: DBSelect = new OleDbCommand(CmdStr, DBconn)
:
:
:
:
:
: DBSelect.Parameters.Add("@VenueName", OleDbType.VarChar, 255)
:
:
: DBSelect.Parameters("@VenueName").Value = TextBox1.text
:
:
:
:
:
: dtrResults = DBSelect.ExecuteReader()
:
:
:
:
:
: if dtrResults.Read()
:
:
:
:
:
: session("searchterm") = TextBox1.text
:
:
:
:
:
: 'lblMessage.visible = true
:
:
: 'lblMessage.Text = session("searchterm")
:
:
:
:
:
: DataGrid1.DataSource = session("searchterm")
:
:
: DataGrid1.DataBind()
:
:
:
:
:
: Else
:
:
: lblmessage.visible = true
:
:
: lblmessage.text = "Invalid venue"
:
:
:
:
:
: end if
:
:
: end if
:
:
:
:
:
: End Sub
:
:
:
:
:
: </script>
:
:
: <html>
:
:
: <head>
:
:
: <title>Untitled Document</title>
:
:
: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
:
:
: <style type="text/css">@import url( Back/main.css );
:
:
: </style>
:
:
: </head>
:
:
: <body>
:
:
: <form runat="server">
:
:
: <table height="457" cellspacing="0" cellpadding="0" width="756" border="0">
:
:
: <tbody>
:
:
: <tr>
:
:
: <td width="170" height="90">
:
:
: <a href="index.aspx"><img height="80" src="unidays2.gif" width="170" border="0" /></a></td>
:
:
: <td width="53">
:
:
:
:
:
: </td>
:
:
: <td width="2">
:
:
:
:
:
: </td>
:
:
: <td width="2">
:
:
:
:
:
: </td>
:
:
: <td width="2">
:
:
:
:
:
: </td>
:
:
: <td width="2">
:
:
:
:
:
: </td>
:
:
: <td width="2">
:
:
:
:
:
: </td>
:
:
: </tr>
:
:
: <tr class="nav">
:
:
: <td height="22">
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: </tr>
:
:
: <tr>
:
:
: <td class="leftNavDark" height="49">
:
:
: <p>
:
:
: </p>
:
:
: </td>
:
:
: <td>
:
:
: <font face="Verdana, Arial, Helvetica, sans-serif" color="#996633" size="4">Going
:
:
: out? Staying in? Find out what's near your Uni!</font>
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: </tr>
:
:
: <tr>
:
:
: <td class="leftNavDark" height="45">
:
:
:
:
:
: </td>
:
:
: <td>
:
:
: <font size="2">In this section you can log in to your personal section at Uni Days!</font></td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: </tr>
:
:
: <tr>
:
:
: <td class="leftNavDark" height="21">
:
:
: <a href="index.aspx">Home</a></td>
:
:
: <td rowspan="7">
:
:
: <p>
:
:
: <span class="subtitle2">Please enter your search query here :</span>
:
:
: </p>
:
:
: <p>
:
:
: <span class="subtitle2">
:
:
: <table>
:
:
: <tbody>
:
:
: <tr>
:
:
: <td>
:
:
: Search</td>
:
:
: <td>
:
:
: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
:
:
: <asp:Label id="lblError" runat="server" visible="False">Please enter a search term</asp:Label></td>
:
:
: <td>
:
:
: </td>
:
:
: <td>
:
:
: </td>
:
:
: </tr>
:
:
: <tr>
:
:
: <td>
:
:
: </td>
:
:
: <td>
:
:
: <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
:
:
: </td>
:
:
: <td>
:
:
: </td>
:
:
: <td>
:
:
: </td>
:
:
: </tr>
:
:
: <tr>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
: <asp:Label id="lblMessage" runat="server" visible="False">This venue does not appear
:
:
: in the database</asp:Label></td>
:
:
: <td>
:
:
: </td>
:
:
: <td>
:
:
: </td>
:
:
: </tr>
:
:
: <tr>
:
:
: <td>
:
:
:
:
:
: </td>
:
:
: <td>
:
:
: <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
:
:
: <AlternatingItemStyle cssclass=".alternating_item"></AlternatingItemStyle>
:
:
: <ItemStyle cssclass=".item"></ItemStyle>
:
:
: <Columns>
:
:
: <asp:BoundColumn HeaderText="Search Results"></asp:BoundColumn>
:
:
: </Columns>
:
:
: </asp:DataGrid>
:
:
:
:
: Many thanks,
:
: stokefan
: