Results 1 to 3 of 3

Thread: DataTable, DataAdapter, and Queries

  1. #1

    Thread Starter
    Fanatic Member Lerroy_Jenkins's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    661

    Question DataTable, DataAdapter, and Queries

    I posted some of my Programs code up here and long story short it wasn't quiet as good as I hoped. So I was shown a thread that might be able to help me. Link. This is a post by jmcilhinney, very smart and reasonably easy to understand. But I am having problems applying some of the ideas to my program.

    What I was doing.

    To retrieve my members list I have 3 text boxes and 1 date time picker as search options. The end user would type or pick information they wanted, and any relating members would become displayed inside of a data grid view.

    Example code

    vb.net Code:
    1. Private Sub PopulateCustomerData(ByVal QueryString As String)
    2.  
    3.         ' This Private Sub is used to pull the needed information out from the database.
    4.         ' The Sub is expecting a QueryString from any source.
    5.         ' Once the Sub has been given the QueryString it then uses this _
    6.         ' information to query the database and to populate the needed fields inside of the customer data section.
    7.  
    8. Dim Connstring As String = "Provider=microsoft.ACE.oledb.12.0;data source =  " & GetAppPath() & "GymMemberData.accdb"
    9.         Dim da As OleDbDataAdapter = New OleDbDataAdapter(QueryString, Connection)
    10.         Dim ds As DataSet = New DataSet
    11.         da.Fill(ds, "MemberInfo")
    12.  
    13.         tboFName2.Text = ds.Tables("MemberInfo").Rows(0).Item("FirstName").ToString
    14.         tboLName2.Text = ds.Tables("MemberInfo").Rows(0).Item("LastName").ToString
    15.         lblMemberID.Text = ds.Tables("MemberInfo").Rows(0).Item("MemberID").ToString
    16.         tboPhoneNumber.Text = ds.Tables("MemberInfo").Rows(0).Item("PhoneNumber").ToString
    17.         tboAddressLine1.Text = ds.Tables("MemberInfo").Rows(0).Item("AddressLine1").ToString
    18.         tboAddressLine2.Text = ds.Tables("MemberInfo").Rows(0).Item("AddressLine2").ToString
    19.         tboAddressLine3.Text = ds.Tables("MemberInfo").Rows(0).Item("AddressLine3").ToString
    20.         tboPostCode.Text = ds.Tables("MemberInfo").Rows(0).Item("AddressPostCode").ToString
    21.         cboCardIssued.Text = ds.Tables("MemberInfo").Rows(0).Item("CardIssued").ToString
    22.  
    23.         Connection.Close()
    24.  
    25.  
    26.     End Sub

    Query string is set by this

    vb.net Code:
    1. Private Sub txtLName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tboLName.TextChanged
    2.  
    3.         ' This Sub sends the Private Sub "PopulateGridVIew" a QueryString, _
    4.         ' that is then used to query the database against characters in the persons last name.
    5.  
    6.         PopulateGridView("SELECT FirstName, LastName, MemberID, startdate, " & _
    7.                          "CardIssued FROM MembersTable WHERE Lastname Like '" & _
    8.                          tboLName.Text & "%' AND MarkedforDeletion = False")
    9.  
    10.     End Sub

    So I go to the database every time the user types anything, and I retrieve the needed information. Later on in the program the user then has the option to update and delete and create. Which is similar, it connects to the database and then updates the records.

    My original problem was that I was then trying to repopulate the datagridview within the same update,delete and create button sub. But for some reason and I still have no idea why, it wasn't working.

    jmcilhinney pointed out that I should be retrieving the data, storing it localy, editing it localy and then updating the database at the end. Fine, but can I get my head around how to do that? No.

    His examples show you using a fixed Query, where as you can see in mine, it all depends on what the user types in. He also sets the DataAdapter and DataTable as open to the whole form, fine.=

    Sorry I am waffeling on. What I am trying to say is that, I understand the idea behind all of his examples, but I am really struggeling to implement the examples into my code. And I am here asking for some help please guys .

    I try and not ask for help but I am really stuck and confused here.
    Lerroy

    "η β π", or "Eta Beta Pi" (Eat A Better Pie)

    01001000
    01000101
    01001100
    01010000


    My Own Code - WordCounter

    Useful Forum Links -Reputation - What is it?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: DataTable, DataAdapter, and Queries

    What kind of return do you get when you run that query?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataTable, DataAdapter, and Queries

    You're using a fixed query just as much as I was, or at least you should be. You should be creating the DataAdapter with its SelectCommand and adding the appropriate parameters. All you do to retrieve the data is then set the parameter Values and call Fill. E.g.
    vb.net Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Public Class Form1
    4.  
    5.     Private table As New DataTable
    6.     Private connection As New SqlConnection("connection string here")
    7.     Private adapter As New SqlDataAdapter("SELECT * FROM Person WHERE FirstName LIKE '%' + @FirstName + '%'", connection)
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         Me.adapter.SelectCommand.Parameters.Add("@FirstName", SqlDbType.VarChar, 50)
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         Me.adapter.SelectCommand.Parameters("@FirstName").Value = Me.TextBox1.Text
    15.         Me.adapter.Fill(Me.table)
    16.         Me.DataGridView1.DataSource = Me.table
    17.     End Sub
    18.  
    19. End Class
    You can then use the same data adapter to save the data exactly as I showed in the CodeBank.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width