ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1727
Number of posts: 3292

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
I Want my DataGrid to show my session variable Posted by stokefan on 7 Jan 2005 at 8:02 AM
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">


                        &nbsp; 


                    </td>


                    <td width="2">


                        &nbsp; 


                    </td>


                    <td width="2">


                        &nbsp; 


                    </td>


                    <td width="2">


                        &nbsp; 


                    </td>


                    <td width="2">


                        &nbsp; 


                    </td>


                    <td width="2">


                        &nbsp; 


                    </td>


                </tr>


                <tr class="nav">


                    <td height="22">


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </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>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                </tr>


                <tr>


                    <td class="leftNavDark" height="45">


                        &nbsp; 


                    </td>


                    <td>


                        <font size="2">In this section you can log in to your personal section at Uni Days!</font></td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                    <td>


                        &nbsp; 


                    </td>


                </tr>


                <tr>


                    <td class="leftNavDark" height="21">


                        <a href="index.aspx">Home</a></td>


                    <td rowspan="7">


                        <p>


                            &nbsp; <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>


                                            &nbsp; 


                                        </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>


                                            &nbsp; 


                                        </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
Report
Re: I Want my DataGrid to show my session variable Posted by iwilld0it on 10 Jan 2005 at 7:01 AM
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">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td width="2">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td width="2">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td width="2">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td width="2">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td width="2">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                 </tr>
: 
: 
:                 <tr class="nav">
: 
: 
:                     <td height="22">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </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>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                 </tr>
: 
: 
:                 <tr>
: 
: 
:                     <td class="leftNavDark" height="45">
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         <font size="2">In this section you can log in to your personal section at Uni Days!</font></td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                     <td>
: 
: 
:                         &nbsp; 
: 
: 
:                     </td>
: 
: 
:                 </tr>
: 
: 
:                 <tr>
: 
: 
:                     <td class="leftNavDark" height="21">
: 
: 
:                         <a href="index.aspx">Home</a></td>
: 
: 
:                     <td rowspan="7">
: 
: 
:                         <p>
: 
: 
:                             &nbsp; <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>
: 
: 
:                                             &nbsp; 
: 
: 
:                                         </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>
: 
: 
:                                             &nbsp; 
: 
: 
:                                         </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
:

Report
Re: I Want my DataGrid to show my session variable Posted by stokefan on 10 Jan 2005 at 12:27 PM
Hi tnanks for reply, I've tried this

: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:

though the same happens, the DataGrid appears but with no search result...

: 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 E

ventArgs)
:     If Not Page.IsPostBack Then
:         ' Create DataGrid
:     End If
: End Sub
: 


Report
Re: I Want my DataGrid to show my session variable Posted by iwilld0it on 11 Jan 2005 at 6:57 AM
It is quite possible that your sql query is not returning a result.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.