Solved

ASP.NET Select Command -  Call Function

Posted on 2006-11-07
6
1,270 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
  • 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 125 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 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