Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET Select Command -  Call Function

Posted on 2006-11-07
6
Medium Priority
?
1,290 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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