Solved

ASP.NET Select Command -  Call Function

Posted on 2006-11-07
6
1,266 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now