Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Search button

Search button

Scheduled Pinned Locked Moved Visual Basic
databasehelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shakizil
    wrote on last edited by
    #1

    OK, It me again. Now I want to know if it is possible to have a search button on one form. When I click on the button it should take all the data from the Database and display it on the other two forms. I know for sure that I might be able to do this by calling funtions but don't know if it will work. If any of you have ever done this kind of coding please help this poor guy. Thanks.

    M 1 Reply Last reply
    0
    • S shakizil

      OK, It me again. Now I want to know if it is possible to have a search button on one form. When I click on the button it should take all the data from the Database and display it on the other two forms. I know for sure that I might be able to do this by calling funtions but don't know if it will work. If any of you have ever done this kind of coding please help this poor guy. Thanks.

      M Offline
      M Offline
      Mike K Clark
      wrote on last edited by
      #2

      I believe that I understand your problem. This is how you would go about doing it. I don't know what server you are trying to run off of (MS Sql server, oracle, an internal dataset application, whatever) but you would have to setup a dataconnection to call the information from the server, then you would have to setup a dataset off of that dataconnection to actually go in and retrieve the data, once you put the dataset control in your environment you have to bind the properties of it so that it uses the dataconnection. I believe the property is datasource (if you set the data source to your connection which could be named mydbcon or whatever you named it) the data source allows the dataset to connect through the dataconnection type which actually the dataconnection only connects to the server, logs in, then the dataset retrieves the information requested from the server. .Net has server control boud functions for this that login to a server and retrieve data from it based off of data connections and datasets. These controls should automatically be placed inside your toolbox in VStudios. The next step that I am about to arrive at is binding the button search to display the dataset on the next control. This is probably the easiest part. We will display Form2 with the dataset control on the form using the nice easy With New function that is supported in VB.Net. This is how you would call Form2 to load on the button click event to display with the dataset object: Private Sub btnsearch_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click With New Form2 .Show() End With End Sub Now I don't know how you have your environment setup but if you have the dataset object to display on the load of form2 then you have to set the search results to bind with the search query given. Again you can do this using the with New function. I apologize if this sounds confusing, I am at school and don't have Visual Studios on me so I am having to do this all off my head ;) If you have any further questions or problems MSDN libraries is a great resource on explaining how to connect datasets to dataconnections and dataconnections to servers (or any other programming problems you may arrive at). Thanks hope this helps! ~ Mike

      S 1 Reply Last reply
      0
      • M Mike K Clark

        I believe that I understand your problem. This is how you would go about doing it. I don't know what server you are trying to run off of (MS Sql server, oracle, an internal dataset application, whatever) but you would have to setup a dataconnection to call the information from the server, then you would have to setup a dataset off of that dataconnection to actually go in and retrieve the data, once you put the dataset control in your environment you have to bind the properties of it so that it uses the dataconnection. I believe the property is datasource (if you set the data source to your connection which could be named mydbcon or whatever you named it) the data source allows the dataset to connect through the dataconnection type which actually the dataconnection only connects to the server, logs in, then the dataset retrieves the information requested from the server. .Net has server control boud functions for this that login to a server and retrieve data from it based off of data connections and datasets. These controls should automatically be placed inside your toolbox in VStudios. The next step that I am about to arrive at is binding the button search to display the dataset on the next control. This is probably the easiest part. We will display Form2 with the dataset control on the form using the nice easy With New function that is supported in VB.Net. This is how you would call Form2 to load on the button click event to display with the dataset object: Private Sub btnsearch_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click With New Form2 .Show() End With End Sub Now I don't know how you have your environment setup but if you have the dataset object to display on the load of form2 then you have to set the search results to bind with the search query given. Again you can do this using the with New function. I apologize if this sounds confusing, I am at school and don't have Visual Studios on me so I am having to do this all off my head ;) If you have any further questions or problems MSDN libraries is a great resource on explaining how to connect datasets to dataconnections and dataconnections to servers (or any other programming problems you may arrive at). Thanks hope this helps! ~ Mike

        S Offline
        S Offline
        shakizil
        wrote on last edited by
        #3

        Thanks alot Mike, I really appreciate your time and effort. I am using Access DB and this is the code in my search button. Right now it does'nt even work for Form1. By that I mean I am missing simple code to open the database. I am using stored procedures by the way. Take a look and let me know if it makes sense. ----------------------------------------------------------------------------- Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click Dim data As New dbAccess Dim logNumber As Long 'data.GetAllChangeControl(CDate(txtdtrqst.Text), CDate(txttrgdt.Text), CDate(txttime.Text), txtrqstby.Text, txtprojname.Text, txtdscp.Text, txtusers.Text, txtdpt.Text) logNumber = data.GetAllChangeControl Dim srch As Change srch.DateRequested = txtdtrqst.Text srch.TargetDate = txttrgdt.Text srch.Time = txttime.Text 'txtdtrqst.Text = srch.DateRequested End Sub ---------------------------------------------------------------------------- This is the class called dbAccess Public Function GetAllChangeControl() As String Dim con As OleDbConnection Dim cmd As OleDbCommand = New OleDbCommand con = New OleDbConnection(connectionstring) cmd.Connection = con cmd.CommandText = "EXECUTE GetAllChangeControl" con.Open() GetAllChangeControl = CStr(cmd.ExecuteScalar()):mad: con.Close() End Function ---------------------------------------------------------------------------- This is another class which only has all the stored procedures and they refletct the stored procedures in Access. I mean the same code is in Access. sSQL = "CREATE PROC GetAllChangeControl AS SELECT * FROM ChangeControl WHERE LogNumber = @LogNumber;" CreateStoredProc(sSQL) --------------------------------------------------------------------------- When I run the program I get the error message where you see the mad face. err msg "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.windows.forms.dll" Any help would be appreciated. Thanks.

        M I 2 Replies Last reply
        0
        • S shakizil

          Thanks alot Mike, I really appreciate your time and effort. I am using Access DB and this is the code in my search button. Right now it does'nt even work for Form1. By that I mean I am missing simple code to open the database. I am using stored procedures by the way. Take a look and let me know if it makes sense. ----------------------------------------------------------------------------- Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click Dim data As New dbAccess Dim logNumber As Long 'data.GetAllChangeControl(CDate(txtdtrqst.Text), CDate(txttrgdt.Text), CDate(txttime.Text), txtrqstby.Text, txtprojname.Text, txtdscp.Text, txtusers.Text, txtdpt.Text) logNumber = data.GetAllChangeControl Dim srch As Change srch.DateRequested = txtdtrqst.Text srch.TargetDate = txttrgdt.Text srch.Time = txttime.Text 'txtdtrqst.Text = srch.DateRequested End Sub ---------------------------------------------------------------------------- This is the class called dbAccess Public Function GetAllChangeControl() As String Dim con As OleDbConnection Dim cmd As OleDbCommand = New OleDbCommand con = New OleDbConnection(connectionstring) cmd.Connection = con cmd.CommandText = "EXECUTE GetAllChangeControl" con.Open() GetAllChangeControl = CStr(cmd.ExecuteScalar()):mad: con.Close() End Function ---------------------------------------------------------------------------- This is another class which only has all the stored procedures and they refletct the stored procedures in Access. I mean the same code is in Access. sSQL = "CREATE PROC GetAllChangeControl AS SELECT * FROM ChangeControl WHERE LogNumber = @LogNumber;" CreateStoredProc(sSQL) --------------------------------------------------------------------------- When I run the program I get the error message where you see the mad face. err msg "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.windows.forms.dll" Any help would be appreciated. Thanks.

          M Offline
          M Offline
          Mike K Clark
          wrote on last edited by
          #4

          I think you may be running into problems on this specific function cmd.ExecuteScalar(). I need to know what data its trying to pull. Try puting a breakpoint on the line and executing it. When it stops at that line open up the immediate window (down by your output for compilation) and type into it ?GetAllChangeControl It would be good if it said NULL or Nothing, which I am sure it will sense it dies at that point. Does Visual Studios give you the key dropdown after you type cmd.? If it doesn't that is where the problem would be. I'm just throwing out a list of ideas here, try the debugging with the immediate window and let me know what the output of it is. ~ Mike

          1 Reply Last reply
          0
          • S shakizil

            Thanks alot Mike, I really appreciate your time and effort. I am using Access DB and this is the code in my search button. Right now it does'nt even work for Form1. By that I mean I am missing simple code to open the database. I am using stored procedures by the way. Take a look and let me know if it makes sense. ----------------------------------------------------------------------------- Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click Dim data As New dbAccess Dim logNumber As Long 'data.GetAllChangeControl(CDate(txtdtrqst.Text), CDate(txttrgdt.Text), CDate(txttime.Text), txtrqstby.Text, txtprojname.Text, txtdscp.Text, txtusers.Text, txtdpt.Text) logNumber = data.GetAllChangeControl Dim srch As Change srch.DateRequested = txtdtrqst.Text srch.TargetDate = txttrgdt.Text srch.Time = txttime.Text 'txtdtrqst.Text = srch.DateRequested End Sub ---------------------------------------------------------------------------- This is the class called dbAccess Public Function GetAllChangeControl() As String Dim con As OleDbConnection Dim cmd As OleDbCommand = New OleDbCommand con = New OleDbConnection(connectionstring) cmd.Connection = con cmd.CommandText = "EXECUTE GetAllChangeControl" con.Open() GetAllChangeControl = CStr(cmd.ExecuteScalar()):mad: con.Close() End Function ---------------------------------------------------------------------------- This is another class which only has all the stored procedures and they refletct the stored procedures in Access. I mean the same code is in Access. sSQL = "CREATE PROC GetAllChangeControl AS SELECT * FROM ChangeControl WHERE LogNumber = @LogNumber;" CreateStoredProc(sSQL) --------------------------------------------------------------------------- When I run the program I get the error message where you see the mad face. err msg "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.windows.forms.dll" Any help would be appreciated. Thanks.

            I Offline
            I Offline
            Icharus
            wrote on last edited by
            #5

            Try the following with your function: Public Function GetAllChangeControl() As String Dim con As OleDbConnection Dim cmd As OleDbCommand = New OleDbCommand con = New OleDbConnection(connectionstring) cmd.Connection = con cmd.CommandText = "EXECUTE GetAllChangeControl" con.Open() '--------------------------------------------- ' Here is the change Dim retVal As Object Try retVal = cmd.ExecuteScalar() If Not(IsNothing(retVal)) Then Return Cstr(retVal) Else Return String.Empty End If Catch ex As OledbException Throw ex Finally con.Close() If Not con Is Nothing Then con.Dispose If Not cmd Is Nothing Then cmd.Dispose End Try '------------------------------------------ End Function Let me explain, it is possible that your "cmd.ExecuteScalar" is not returning anything and you are not checking for that, so what I've done here is do a simple check and return the appropriate value. I have also enclosed the procedure in a Try statement to capture the Exception, and make sure that even if an Exception is thrown, the connection is closed and you realease your resources. I hope this helps.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups