Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export Gridview contents to Excel , Error Gridview must be in form tag run at server

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I tried to put the gridview in a form, but then when I fill it programically it says the Gridview2 does not exist.
I get this error:
GridView2' is not a member of 'SOWTimeReporting.CreateExcelSheet'
What am I doing wrong? I just want to export the contents of the gridview to Excel, which changes as the user picks different drop down listss to filter it.
Code:
---------------- This is the HTML markup ------------------    
<asp:FormView ID="FormView1" runat="server">
        <ItemTemplate>
            <asp:GridView ID="GridView2" runat="server">
            </asp:GridView>
        </ItemTemplate>
    </asp:FormView>
[code]

[code]
------------------------------ VB code behind ----------------------
  the followin 3 linens are in the form load     
Dim queryTrans As String = "Select  blah blah blah

        [bold][COLOR=blue]Me.GridView2[/color][/bold].DataSource = [bold][COLOR=orange]PopulateGridwithSQL(queryTrans)[/color][/bold].  ' Get blue Squiggly on these lines
        [bold][COLOR=blue]Me.GridView2[/color][/bold].DataBind()

'--------- this is the function called above to fill the grid
Public Function [bold][COLOR=orange]PopulateGridwithSQL[/color][/bold]. ByVal SQLStatement As String)

        Dim Conn As SqlConnection
        'use ConnectionString from WEB config file
        Dim connStr As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
        Conn = New SqlConnection(connStr)
        Conn.Open()
        Dim daTrans As New SqlDataAdapter
        Dim dtTrans As New DataTable
        daTrans.SelectCommand = New SqlCommand(SQLStatement, Conn)
        daTrans.Fill(dtTrans)
         Return dtTrans
        Conn = Nothing
    End Function


DougP
 

First, there is No return type defined in the function declaration

Code:
Public Function PopulateGridwithSQL(ByVal SQLStatement As String) [red]as DataTable[/red]

Secondly, the gridview doesn't exists at Form_Load. You'll need to populate it in the FormView_DataBound event using the FindControl method.



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top