VB.NET类,函数不显示存储过程结果 [英] VB.NET Class with Function not displaying Stored Procedure results

查看:61
本文介绍了VB.NET类,函数不显示存储过程结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在做一个项目,我正在使用一个类(类是单独的形式),其中一个函数读取一个允许的存储过程我来过滤/搜索我的Gridview中显示的记录。当我不使用Class时,我可以让代码正常工作,但是...我需要它来处理类。我正在寻找的最终结果是能够过滤/搜索填充的GridView。当我运行此代码时,我得到自定义警报,说找不到记录但当然有记录。



这是我班级中的代码:

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





这是我在aspx页面中的代码



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





还有其他代码这有助于过滤/搜索,但就像我说我可以在不使用类的情况下使代码工作,但我真的需要它与类一起工作。我不知道你是否需要我的更多信息才能帮助我。此外,当我运行此代码时,我得到我的自定义警报没有找到记录但当然有记录。我几乎肯定我正在连接到数据库,但我认为我没有进入存储过程。我的存储过程工作正常,我的GridView确实已填充。



任何帮助将不胜感激。谢谢



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

推荐答案

尝试在finally子句中评论以下几行

dt.Clear()

dt。 Dispose()



似乎你在清除/处理后返回dt。
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.


对我来说似乎一切都很好唯一的问题是你错过了捕获部分

然后你没有打开连接
seems everything fine to me the one and only problem is you miss the catch part
and then you didn't open the connection


这篇关于VB.NET类,函数不显示存储过程结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆