搜索数据库通过sqldatareader将结果返回到gridview
我正在尝试创建一个搜索页面,这允许管理员按某些条件搜索数据库中的条目,例如省(如州,但不同国家)
这是我到目前为止的代码。问题是我没有收到任何错误。但我也没有得到任何结果。该页面只是回发并返回到空白搜索页面。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的变量名称不匹配(
Search
与strSearch
),但我怀疑这是一个拼写错误。更大的问题是,通过将匹配的字符放入参数中,它们会被引用。将它们放在 SQL 语句本身中。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.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.
好的,
当我做了 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)
这是在黑暗中的疯狂拍摄,但是这个变量是否需要在第一个 % 之前和最后一个 % 之后有刻度线,因为它是一个字符串值?
原文:
修改:
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:
Revised: