Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm doing a project where I'm using a Class(Class is separate form) with a Function that Reads a Stored Procedure that allows me to Filter/Search records that are displayed in my Gridview. I CAN get the code to work correctly when I don't use the Class, but...I need it to work WITH the class. The end result I'm looking for is to be able to Filter/Search the populated GridView. When I do run this code I get my custom alert saying 'No Records Found' But of course there are records.

This is the code in my Class:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class EmpRecords

    Public Shared Function SearchEmpRecords_Sp(searchBy As String, searchVal As String) As DataTable

        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString)
        Dim adp As New SqlDataAdapter()
        Dim cmd As New SqlCommand()
        Dim dt As New DataTable()

        Try
            cmd.Connection = con
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "SearchEmpRecords_Sp"
            cmd.Parameters.AddWithValue("@SearchBy", searchBy)
            cmd.Parameters.AddWithValue("@SearchVal", searchVal)
            adp.SelectCommand = cmd
            adp.Fill(dt)
        Finally
            dt.Clear()
            dt.Dispose()
            adp.Dispose()
            con.Close()
        End Try
        Return dt
    End Function


End Class


This is my code in the aspx page
Private Sub getEmpRecords(searchBy As String, searchVal As String)

        Try
            Dim dt = EmpRecords.SearchEmpRecords_Sp(searchBy, searchVal)
            If dt.Rows.Count > 0 Then
                grdEmp.DataSource = dt
                grdEmp.DataBind()
            Else
                grdEmp.DataSource = Nothing
                grdEmp.DataBind()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


There is other code that helps the Filter/Search but like I said I can get the code to work without using the class, But I really need for it to work with the Class. I don't know if you need more info from me in order to help me with this. Also when I do run this code I get my custom alert saying 'No Records Found' But of course there are records. I almost positive I'm connecting to the database but I don't think I'm getting to the Stored Procedure. My Stored Procedure works fine and my GridView does get populated.

Any help will be greatly appreciated. Thanks
Posted
Updated 14-Nov-13 12:16pm
v2

Try commenting the following lines in the finally clause
dt.Clear()
dt.Dispose()

It seems that you are returning dt after it is cleared/disposed.
 
Share this answer
 
Comments
Commish13 15-Nov-13 10:17am    
I just got it to work. I make the changes you suggested and Thava and it works perfectly. Thanks for your help
seems everything fine to me the one and only problem is you miss the catch part
and then you didn't open the connection
 
Share this answer
 
v3
Comments
Commish13 15-Nov-13 9:40am    
I thank you for your help. I did make the changes you suggestions but I'm still getting the 'No Records Found' message. Is there something else that could be missing?
Commish13 15-Nov-13 10:17am    
I just got it to work. I make the changes you suggested and tmik and it works perfectly. Thanks for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900