Solved

ASP.NET Select Command -  Call Function

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 93
SSRS Deployment problem 5 124
Build a string of emails from a gridview 2 29
Disable the weekends on datepicker control 6 51
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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

737 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