?
Solved

ASP.NET Select Command -  Call Function

Posted on 2006-11-07
6
Medium Priority
?
1,280 Views
Last Modified: 2007-12-19
I have 3 files (Test1.aspx, Test1.aspx.vb, TestResults.aspx).  I'm using a function to create my SelectCommand sql string.  On my TestResults.aspx page, I want to call the function but I'm getting a "Declaration Expected" error message for 'GridView1'.  If i try to declare GridView1, it says it's already declared.  Any suggestions?  Here is what my files look like:
*****************************************
Test1.aspx (this page actually contains several drop down lists and text boxes - I shortened here for simplicity)

    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="Input Value1" runat="server" Wrap="False"></asp:TextBox>
        <br />
        <asp:TextBox ID="Input Value2" runat="server"></asp:TextBox><br />
        <br />
        &nbsp;<asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>

**************************************
Test1.aspx.vb (SQLString function is actually a long if/then statement that uses the values from the form fields on Test1.aspx.).

Public Class Test1
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Server.Transfer("TestResults.aspx")
    End Sub

    Public Shared Function SQLString() As String

        SQLString = "SELECT * FROM Table1"

    End Function

End Class
***************************************
TestResults.aspx looks like this:

<%@ Page Language="VB" AutoEventWireup="false"%>

<script runat=server>
        GridView1.SelectCommand = Test1.SQLString()
</script>

<head runat="server">
    <title>Untitled Page</title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1">
            <Columns>
                <asp:BoundField DataField="Field1" HeaderText="Field1" SortExpression="Field1" />
                <asp:BoundField DataField="Field2" HeaderText="Field2" SortExpression="Field2" />
                <asp:BoundField DataField="Field3" HeaderText="Field3" SortExpression="Field3" />
            </Columns>
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="mydb.mdb"></asp:AccessDataSource>

0
Comment
Question by:drees727
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17891918
I think you need to set the Select property of the AccessDataSource1 datasource control, not the grid.
0
 

Author Comment

by:drees727
ID: 17893162
I modifed it the following but am getting 'Declaration Expected' for AccessDataSource1.

<script runat=server>
        AccessDataSource1.SelectCommand = Test1.SQLString()
</script>
0
 
LVL 9

Accepted Solution

by:
kraffay earned 500 total points
ID: 17893446
I got these two pages to work against the Northwind.mdb with your technique:

Page 1:

---------------------------------------------------------------------------------------------------------

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Session("InputValue1") = Me.InputValue1.Text
        Session("InputValue2") = Me.InputValue2.Text
        Response.Redirect("Step2.aspx")
       
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page 1</title>
</head>
<body>
    <form id="form1" runat="server">
   <div>
        <asp:TextBox ID="InputValue1" runat="server" Text="Alfreds Futterkiste" Wrap="False"></asp:TextBox>
        <br />
        <asp:TextBox ID="InputValue2" runat="server" Text="Maria Anders"></asp:TextBox><br />
        <br />
        &nbsp;<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
    </form>
</body>
</html>


---------------------------------------------------------------------------------------------------------

Page 2:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim CompanyName As String = Session("InputValue1")
        Dim ContactName As String = Session("InputValue2")
       
        Me.AccessDataSource1.SelectCommand = "Select * FROM Customers WHERE CompanyName ='" & CompanyName & "' AND ContactName = '" & ContactName & "'"
        Me.GridView1.DataBind()
       
    End Sub
   
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page 2</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Northwind.mdb" ></asp:AccessDataSource>
        <br />
        &nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1">
            <Columns>
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
                <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
                <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question