943,147 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 2150
  • ASP.NET RSS
Nov 16th, 2009
0

Help with a asp.NET search query

Expand Post »
I am trying to search a database using a user defined string from a text box. I am getting an error message with the "objectadaptor.Fill"

Here is the code:

ASP.NET Syntax (Toggle Plain Text)
  1. Imports System.Data.OleDb
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Imports System.Data.SqlTypes
  5.  
  6. Partial Class Search
  7. Inherits System.Web.UI.Page
  8.  
  9. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  10. Dim objConnection As OleDbConnection
  11. Dim objCommand As OleDbCommand
  12. Dim objAdapter As OleDbDataAdapter
  13. Dim objDataSet As DataSet
  14. Dim strSearch As String
  15. Dim strSQLQuery As String
  16.  
  17. 'some code taken from www.asp101.com
  18.  
  19. ' Get Search
  20. strSearch = TextBox1.Text
  21.  
  22. ' If there's nothing to search for then don't search
  23. ' o/w build our SQL Query and execute it.
  24. If Len(Trim(strSearch)) > 0 Then
  25.  
  26. ' Set up our connection.
  27. objConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" _
  28. & "Data Source=" & Server.MapPath("DB\ScrumManagementSystem.accdb") & ";")
  29.  
  30. ' Set up our SQL query text.
  31. strSQLQuery = "SELECT FName AS Name, FROM tblUser " _
  32. & "OR FName LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
  33. & "ORDER BY FName;"
  34.  
  35. ' Create new command object passing it our SQL query
  36. ' and telling it which connection to use.
  37. objCommand = New OleDbCommand(strSQLQuery, objConnection)
  38.  
  39. ' Get a DataSet to bind the DataGrid to
  40. objAdapter = New OleDbDataAdapter(objCommand)
  41. objDataSet = New DataSet()
  42. objAdapter.Fill(objDataSet) 'problem line!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  43.  
  44. ' DataBind DG to DS
  45. dgPaging.DataSource = objDataSet
  46. dgPaging.DataBind()
  47.  
  48. objConnection.Close()
  49.  
  50. Else
  51. TextBox1.Text = "Enter Search Here"
  52. End If
  53. End Sub
  54.  
  55. Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgPaging.SelectedIndexChanged
  56.  
  57. End Sub
  58.  
  59. Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  60. Response.Redirect("myProjectsPage.aspx")
  61. End Sub
  62.  
  63. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  64.  
  65. End Sub
  66. End Class
  67.  


Please help!
Last edited by adatapost; Nov 16th, 2009 at 9:55 am. Reason: Please use [code] tags to wrap your code.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
15389049 is offline Offline
8 posts
since Nov 2009
Nov 16th, 2009
0
Re: Help with a asp.NET search query
What is the error message ?
Sponsor
Featured Poster
Reputation Points: 546
Solved Threads: 717
Bite my shiny metal ass!
pritaeas is offline Offline
4,143 posts
since Jul 2006
Nov 16th, 2009
0

Thank you, i think i have it working

Click to Expand / Collapse  Quote originally posted by pritaeas ...
What is the error message ?
its a problem with the:

ASP.NET Syntax (Toggle Plain Text)
  1. objAdapter.Fill(objDataSet)
line

Though i think i have it fixed now, I was accesssing the database incorrectly and when i corrected this i no longer have the problem.

Thank you for posting!

here is my new code:

ASP.NET Syntax (Toggle Plain Text)
  1. Imports System.Data.OleDb
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Imports System.Data.SqlTypes
  5. Partial Class Search
  6. Inherits System.Web.UI.Page
  7.  
  8.  
  9. Sub btnSearch_OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
  10. Dim objCommand As OleDbCommand
  11. Dim objAdapter As OleDbDataAdapter
  12. Dim objDataSet As DataSet
  13. Dim strSearch As String
  14.  
  15. Dim objConnection As String = ("Provider=Microsoft.ACE.OLEDB.12.0;" _
  16. & "Data Source=" & Server.MapPath("DB\ScrumManagementSystem.accdb") & ";")
  17.  
  18. Dim cn As New OleDbConnection(objConnection)
  19. Dim strSQLQuery As String
  20.  
  21. strSearch = txtSearch.Text
  22.  
  23. If Len(Trim(strSearch)) > 0 Then
  24.  
  25. strSQLQuery = "SELECT FName & SName FROM tblUser WHERE FName LIKE '%" & Replace(strSearch, "'", "''") & "%' ORDER BY FName;"
  26.  
  27. cn.Open()
  28. Dim cmd As New OleDbCommand(strSQLQuery, cn)
  29.  
  30. objCommand = New OleDbCommand(strSQLQuery, cn)
  31.  
  32. objAdapter = New OleDbDataAdapter(objCommand)
  33. objDataSet = New DataSet()
  34. objAdapter.Fill(objDataSet)
  35.  
  36. dgPaging.DataSource = objDataSet
  37. dgPaging.DataBind()
  38.  
  39. 'ListBox1.DataSource = objDataSet
  40. 'ListBox1.DataBind()
  41.  
  42. cn.Close()
  43. Else
  44. txtSearch.Text = "Enter search criteria!"
  45. End If
  46. End Sub
  47.  
  48. Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgPaging.SelectedIndexChanged
  49.  
  50. End Sub
  51.  
  52. Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  53. Response.Redirect("myProjectsPage.aspx")
  54. End Sub
  55.  
  56. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  57.  
  58. End Sub
  59. End Class
Reputation Points: 10
Solved Threads: 0
Newbie Poster
15389049 is offline Offline
8 posts
since Nov 2009
Nov 16th, 2009
0
Re: Help with a asp.NET search query
You never opened the connection (objConnection)

Glad you fixed it it anyway
Reputation Points: 10
Solved Threads: 2
Newbie Poster
shibbard is offline Offline
17 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: using .resx file to dynamically change page content based on user
Next Thread in ASP.NET Forum Timeline: new to asp.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC