Create web form with Web Matrix "error"

I have created the web form (Filterd Data Report) with Web Matrix, but when I run the following error:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 62:     Sub DataGrid1_SelectedIndexChanged(sender As Object, e As EventArgs)
Line 63:     
Line 64:         Function MyQueryMethod(ByVal empno As Decimal) As System.Data.DataSet
Line 65:             Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\webpage\Atte"& _
Line 66:                 "ndance.mdb"


Source File: C:\webpage\NewFile.aspx    Line: 64 

Here is full code:

Sub Page_Load(Sender As Object, E As EventArgs)

    If Not Page.IsPostBack Then

        ' Databind the filter dropdown on the first request only
        ' (viewstate will restore these values on subsequent postbacks).

        ' TODO: update the ConnectionString and CommandText values for your application
        Dim ConnectionString As String = "server=(local);database=pubs;trusted_connection=true"
        Dim CommandText As String = "select distinct au_lname from Authors"

        Dim myConnection As New SqlConnection(ConnectionString)
        Dim myCommand As New SqlCommand(CommandText, myConnection)

        ' TODO: Update the DataTextField value
        DropDownList1.DataTextField = "au_lname"

        myConnection.Open()

        DropDownList1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        DropDownList1.DataBind()

        ' insert an "All" item at the beginning of the list
        DropDownList1.Items.Insert(0, "-- All Authors --")

    End If

End Sub


Sub ApplyFilter_Click(Sender As Object, E As EventArgs)

    ' TODO: update the ConnectionString value for your application
    Dim ConnectionString As String = "server=(local);database=pubs;trusted_connection=true"
    Dim CommandText As String

    ' get the filter value from the DropDownList
    Dim filterValue As String = DropDownList1.SelectedItem.Text.Replace("'", "''")

    ' TODO: update the CommandText value for your application
    If filterValue = "-- All Authors --" Then
        CommandText = "select distinct title as Title, price as Price, ytd_sales as [YTD Sales] from titleview"
    Else
        CommandText = "select title as Title, price as Price, ytd_sales as [YTD Sales] from titleview where au_lname = '" & filterValue & "'"
    End If

    Dim myConnection As New SqlConnection(ConnectionString)
    Dim myCommand As New SqlCommand(CommandText, myConnection)

    myConnection.Open()

    DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    DataGrid1.DataBind()

End Sub

Sub DataGrid1_SelectedIndexChanged(sender As Object, e As EventArgs)

    Function MyQueryMethod(ByVal empno As Decimal) As System.Data.DataSet
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\webpage\Atte"& _
            "ndance.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "SELECT [StaffLogRun].[empno], [StaffLogRun].[name], [StaffLogRun].[datetimeIn], ["& _
            "StaffLogRun].[title] FROM [StaffLogRun] WHERE ([StaffLogRun].[empno] = @empno)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_empno As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_empno.ParameterName = "@empno"
        dbParam_empno.Value = empno
        dbParam_empno.DbType = System.Data.DbType.[Decimal]
        dbCommand.Parameters.Add(dbParam_empno)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function
End Sub

Can someone help me to remove error

Thanks 

 

 

 


 

 

0
kafsar
2/14/2008 5:26:58 PM
asp.net.visual-basic.net 3721 articles. 0 followers. Follow

2 Replies
940 Views

Similar Articles

[PageSpeed] 24

 You can not have a function inside a subroutine

Change this 

Sub DataGrid1_SelectedIndexChanged(sender As Object, e As EventArgs)

    Function MyQueryMethod(ByVal empno As Decimal) As System.Data.DataSet
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\webpage\Atte"& _
            "ndance.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "SELECT [StaffLogRun].[empno], [StaffLogRun].[name], [StaffLogRun].[datetimeIn], ["& _
            "StaffLogRun].[title] FROM [StaffLogRun] WHERE ([StaffLogRun].[empno] = @empno)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_empno As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_empno.ParameterName = "@empno"
        dbParam_empno.Value = empno
        dbParam_empno.DbType = System.Data.DbType.[Decimal]
        dbCommand.Parameters.Add(dbParam_empno)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function
End Sub

 

To this

 

Sub DataGrid1_SelectedIndexChanged(sender As Object, e As EventArgs)

End Sub

    Function MyQueryMethod(ByVal empno As Decimal) As System.Data.DataSet
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\webpage\Atte"& _
            "ndance.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "SELECT [StaffLogRun].[empno], [StaffLogRun].[name], [StaffLogRun].[datetimeIn], ["& _
            "StaffLogRun].[title] FROM [StaffLogRun] WHERE ([StaffLogRun].[empno] = @empno)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_empno As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_empno.ParameterName = "@empno"
        dbParam_empno.Value = empno
        dbParam_empno.DbType = System.Data.DbType.[Decimal]
        dbCommand.Parameters.Add(dbParam_empno)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function

 


Silverlight-help
Vb Tips
Space Coast .Net User Group
0
Ken
2/14/2008 6:00:16 PM

I received the following error while using SQL 2000 or Access.mdb  why, should I use SQL Server 2005 ?

An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Source Error:

Line 13:         Dim myCommand As New SqlCommand(CommandText, myConnection)
Line 14: 
Line 15:         myConnection.Open()
Line 16: 
Line 17:         DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

Thanks

0
kafsar
2/14/2008 11:51:26 PM
Reply: