www.webdeveloper.com
Results 1 to 8 of 8

Thread: Error...of declaring...

  1. #1
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168

    Error...of declaring...

    Hello, I've tried this code without using a where statement and it works fine, but as soon as I put one in I get this error:
    Name 'dgResults' is not declared.
    I tried declarion Putting Dim in front of it, and then all by itself..I don't get it. Thx in Advance

    <%@ Page Language="VB" Debug="true" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.OleDb" %>
    <script runat="server">

    sub Page_Load(sender as Object, e as EventArgs)

    Dim Find_Item = request.form("Find_Item")
    'Create a connection string
    Dim connString as String
    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
    "C:\Profiles\Databases\Profiles.mdb;"


    Dim objConnection as OleDbConnection
    objConnection = New OleDbConnection(connString)
    objConnection.Open()


    Dim strSQL as String = "SELECT * FROM Profiles WHERE First_Name = '" & Find_Item & "' "


    Dim objCommand as OleDbCommand
    objCommand = New OleDbCommand(strSQL, objConnection)


    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)


    dgResults.DataSource = objDataReader
    dgResults.DataBind()


    objDataReader.Close()
    end sub

    </script>
    <html>
    <head>
    </head>
    <body>
    <pre>&lt;aspataGrid id="dgResults" runat="server" /&gt;</pre>
    </body>
    </html>

  2. #2
    Join Date
    Nov 2004
    Location
    Utah
    Posts
    23
    ...

    You have to make sure that you detect a PostBack to make sure that you have data. If not, then you need to make sure that the Request.form("Find_Item") is not Nothing so that you do not get an error.

    The key to this is to only bind when you have a Non-Nothing value in the Request.Form("Find_Item")

    Dim Find_Item as String
    If Not Request.Form("Find_Item") Is Nothing Then
    Find_Item = Request.Form("Find_Item").ToString()
    ' Do all your binding here.
    End If

    That way you will not get an error or get nothing in the return.

    Ben Miller
    All responses are provided AS IS and do not necessarily reflect my employers views.

  3. #3
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    <script runat="server">

    sub Page_Load(sender as Object, e as EventArgs)

    Dim Find_Item as String

    If Not Request.Form("Find_Item") Is Nothing Then
    Find_Item = Request.Form("Find_Item").ToString()
    ' Do all your binding here.
    dgResults.DataSource = objDataReader
    dgResults.DataBind()
    End If
    'Create a connection string
    Dim connString as String
    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
    "C:\Profiles\Databases\Profiles.mdb;"

    'Open a connection
    Dim objConnection as OleDbConnection
    objConnection = New OleDbConnection(connString)
    objConnection.Open()

    'Specify the SQL string
    Dim strSQL as String = "SELECT * FROM Profiles WHERE First_Name = '" & Find_Item & "' "

    'Create a command object
    Dim objCommand as OleDbCommand
    objCommand = New OleDbCommand(strSQL, objConnection)

    'Get a datareader
    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

    objDataReader.Close()
    end sub

    </script>

    Also here is the sending page. Am I even getting contents of my text box to the receiving page? I'm just starting out so plz bear with me.
    Thx in Advance.

    Sub Request_Page (sender As Object, e As EventArgs)

    response.redirect("Testing.aspx")

    End Sub

    </script>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">

    <asp:TextBox id="Find_Item" runat="server"></asp:TextBox>
    <asp:Button id="Button1" onclick="Request_Page" runat="server" Text="Search"></asp:Button>
    </form>
    </body>
    </html>

  4. #4
    Join Date
    Nov 2004
    Location
    Utah
    Posts
    23
    Look below for the revised code. As I said in your example you have a simple search page that in order to show the DataGrid you would want to make sure that you have a search criteria. But on the first load you will want to check for IsPostBack = True because if you first load this page, then Request.Form("find_item") will be nothing and will still cause problems if you leave your code as it is.

    But when they click the Button, it will cause IsPostBack = True and will allow you to tell that they should have put something in the form. You check for the Find_Item and then bind it to the datagrid, otherwise you do not.

    Revised Code....

    <script runat="server">

    sub Page_Load(sender as Object, e as EventArgs)

    If IsPostBack = True Then
    Dim Find_Item as String

    If Not Request.Form("Find_Item") Is Nothing Then
    Find_Item = Request.Form("Find_Item").ToString()

    'Create a connection string
    Dim connString as String
    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
    "C:\Profiles\Databases\Profiles.mdb;"

    'Open a connection
    Dim objConnection as OleDbConnection
    objConnection = New OleDbConnection(connString)
    objConnection.Open()

    'Specify the SQL string
    Dim strSQL as String = "SELECT * FROM Profiles WHERE First_Name = '" & Find_Item & "' "

    'Create a command object
    Dim objCommand as OleDbCommand
    objCommand = New OleDbCommand(strSQL, objConnection)

    'Get a datareader
    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

    dgResults.DataSource = objDataReader
    dgResults.DataBind()

    objDataReader.Close()

    ' End If Request.Form("Find_Item") ...
    End If

    ' End If IsPostBack = True
    End If

    end sub

    </script>

    <html>
    <head>
    </head>
    <body>
    <form runat="server">
    <asp:TextBox id="Find_Item" runat="server"></asp:TextBox>
    <asp:Button id="Button1" onclick="Request_Page" runat="server" Text="Search"></asp:Button>
    </form>
    </body>
    </html>
    All responses are provided AS IS and do not necessarily reflect my employers views.

  5. #5
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    Hi, I don't seem to get any errors, but nothing is displaying... this is what I use to display my results...it displayed fine before..I also trying putting in a first name in my database directly into my WHERE Statement and still nothing came up..any help? Thx in Advance.
    <pre>
    <aspataGrid id="dgResults" runat="server"></aspataGrid>
    </pre>

  6. #6
    Join Date
    Nov 2004
    Location
    Utah
    Posts
    23
    Well, the concept is that you need to have the form to submit the find_item and the datagrid on the same page. Does the following make sense? With ASP.NET it is all on one page now and when the form is runat="server" then it will postback to itself. It is better to use events than to do the old Classic ASP approach. In the beginning I was helping you solve a problem, but I thought it was better to help you solve it using ASP.NET instead of trying to make ASP.NET do it ASP's way.

    Let me know.

    <script Language="VB" runat="server">
    sub Page_Load()
    if Not IsPostBack Then
    dgResults.Visible = false
    end if
    end sub

    Sub get_results(sender as object, e as System.EventArgs)
    'Create a connection string
    Dim connString as String
    connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
    "C:\Profiles\Databases\Profiles.mdb;"

    'Open a connection
    Dim objConnection as OleDbConnection
    objConnection = New OleDbConnection(connString)
    objConnection.Open()

    'Specify the SQL string
    Dim strSQL as String = "SELECT * FROM Profiles WHERE First_Name = '" & Find_Item.Text & "' "

    'Create a command object
    Dim objCommand as OleDbCommand
    objCommand = New OleDbCommand(strSQL, objConnection)

    'Get a datareader
    Dim objDataReader as OleDbDataReader
    objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

    dgResults.DataSource = objDataReader
    dgResults.DataBind()

    objDataReader.Close()
    dgResults.Visible = true

    End Sub
    </script>

    <html>
    <head><title>Search form</title>
    </head>
    <body>
    <form runat="server">
    <asp:TextBox id="Find_Item" runat="server" /><br>
    <asp:Button id="cmdSearch" runat="Server" onclick="get_results" />
    <br><aspataGrid id="dgResults" runat="server" visible="false" />
    </form>
    </body>
    </html>
    All responses are provided AS IS and do not necessarily reflect my employers views.

  7. #7
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    Hi, ty for the help really appreciated and yes, I'm just starting to go from Classic ASP to .net. I'm getting this error...? And I have no clue what or if I could replace it with anything.. Thx in advance!

    'DataSource' is not a member of 'System.Web.UI.HtmlControls.HtmlGenericControl'.

  8. #8
    Join Date
    Nov 2004
    Location
    Utah
    Posts
    23
    I am not sure where it is getting the HtmlGenericControl. The dgResults control is an <asp : DataGrid /> control.

    If you want to send me the page itself in email you can mail it to me at mylistsATbenmiller.net. (obviously replacting AT with @).

    I will help you get to the bottom of this. Then we will post the final answer to this forum.

    Ben Miller
    All responses are provided AS IS and do not necessarily reflect my employers views.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles