搜索数据库通过sqldatareader将结果返回到gridview

发布于 2024-08-23 00:44:58 字数 1539 浏览 3 评论 0原文

我正在尝试创建一个搜索页面,这允许管理员按某些条件搜索数据库中的条目,例如省(如州,但不同国家)

这是我到目前为止的代码。问题是我没有收到任何错误。但我也没有得到任何结果。该页面只是回发并返回到空白搜索页面。 GridView 不显示任何结果。

这是我的代码:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class administration_Search
    Inherits System.Web.UI.Page

    Protected Sub ProvinceButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProvinceButton.Click
        'get the search string from text box
        Dim Search As String
        Search = ("%" & ProvinceButton.Text & "%")
        'if the string has value then continue with search
        If Len(Trim(Search)) > 0 Then
            Dim con As String = WebConfigurationManager.ConnectionStrings("fubar").ToString()
            'build sql string 
            Dim s As String
            s = ("SELECT id, surname, name FROM orders WHERE province like @strSearch")
            'declar connection
            Dim c As SqlConnection = New SqlConnection(con)
            'add command
            Dim x As New SqlCommand(s, c)
            'add parameter
            x.Parameters.AddWithValue("@strSearch", strSearch)
            c.Open()
            Dim r As SqlDataReader = x.ExecuteReader
            GV.DataSource = r
            GV.DataBind()
            c.Close()
        Else
            Province.Text = ("Please enter search terms")
        End If
    End Sub
End Class

在 .aspx 页面上,我有一个文本框(id:省)和一个按钮(id:提交)

任何人都可以发现我哪里出错了。当您没有收到错误或结果时,这非常令人沮丧:0)

谢谢!

I am trying to create a search page, this allows the admin to search through the entries in the database by certain criteria, such as province (like state but diff country)

Here is the code I have so far. The problem is that I am not getting any errors. But I am also not getting any results. The page just posts back and returns to the blank search page. The GridView does not display any results.

here is my code as it stands:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class administration_Search
    Inherits System.Web.UI.Page

    Protected Sub ProvinceButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProvinceButton.Click
        'get the search string from text box
        Dim Search As String
        Search = ("%" & ProvinceButton.Text & "%")
        'if the string has value then continue with search
        If Len(Trim(Search)) > 0 Then
            Dim con As String = WebConfigurationManager.ConnectionStrings("fubar").ToString()
            'build sql string 
            Dim s As String
            s = ("SELECT id, surname, name FROM orders WHERE province like @strSearch")
            'declar connection
            Dim c As SqlConnection = New SqlConnection(con)
            'add command
            Dim x As New SqlCommand(s, c)
            'add parameter
            x.Parameters.AddWithValue("@strSearch", strSearch)
            c.Open()
            Dim r As SqlDataReader = x.ExecuteReader
            GV.DataSource = r
            GV.DataBind()
            c.Close()
        Else
            Province.Text = ("Please enter search terms")
        End If
    End Sub
End Class

On the .aspx page I have a textbox (id: province) and a button (id:submit)

Can anyone spot where I am going wrong. Its very frustrating when you dont get errors or results :0)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葵雨 2024-08-30 00:44:58

您的变量名称不匹配(SearchstrSearch),但我怀疑这是一个拼写错误。更大的问题是,通过将匹配的字符放入参数中,它们会被引用。将它们放在 SQL 语句本身中。

Dim strSearch As String
strSearch = ProvinceButton.Text.Trim()

...

s = ("SELECT id, surname, name FROM orders WHERE province like '%' + @strSearch + '%'")

Your variable names don't match (Search vs. strSearch), but I suspect that's a typo. The bigger problem is that by putting the matching characters in the parameter, they are being quoted. Put them in the SQL statement itself.

Dim strSearch As String
strSearch = ProvinceButton.Text.Trim()

...

s = ("SELECT id, surname, name FROM orders WHERE province like '%' + @strSearch + '%'")
靖瑶 2024-08-30 00:44:58

1) 我建议您检查正在执行的SQL

在 c.Open() 行上放置一个断点。当您到达那里时,检查 SqlCommand x。查看 SELECT 语句的最终版本是什么。将其复制并粘贴到 Management Studio 中,然后运行它以查看是否存在语法问题。

2) 您还可以尝试填充一些SqlCommand 属性。设置x.CommandType = CommandType.Text。

3) 我还建议您在最后关闭连接之前关闭并处置 SqlDataReader c。这并不能解决这个特定问题,但这是一个很好的做法。

1) I would suggest that you examine the SQL that is being executed.

Put a breakpoint on the line c.Open(). When you get there, examine the SqlCommand x. See what the final version of the SELECT statement is. Copy and paste it into Management Studio, then run it to see if there is a syntax problem.

2) You might also try populating some of the SqlCommand properties. Set x.CommandType = CommandType.Text.

3) I would also suggest that you Close and Dispose of the SqlDataReader at the end, right before you close the Connection c. That won't solve this particular problem, but it is a good practice.

汹涌人海 2024-08-30 00:44:58

好的,

当我做了 tvanfosson 善意指出的修改后,我仍然遇到问题;

我不小心输入了:strSearch = ProvinceButton.Text.Trim()
当我应该输入: strSearch = Province.Text.Trim()

非常感谢:0)

OK

Once i did the amends kindly pointed out by tvanfosson I still had problems;

I had accidentally typed: strSearch = ProvinceButton.Text.Trim()
When I should have typed: strSearch = Province.Text.Trim()

Thanks a lot :0)

娇妻 2024-08-30 00:44:58

这是在黑暗中的疯狂拍摄,但是这个变量是否需要在第一个 % 之前和最后一个 % 之后有刻度线,因为它是一个字符串值?

原文:

Search = ("%" & ProvinceButton.Text & "%") 

修改:

Search = ("'%" & ProvinceButton.Text & "%'") 

This is a wild shot in the dark, but does this variable need to have tick marks before the first % and after the last % since it's a string value?

Original:

Search = ("%" & ProvinceButton.Text & "%") 

Revised:

Search = ("'%" & ProvinceButton.Text & "%'") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文