Results 1 to 6 of 6

Thread: [RESOLVED] Fill based on value

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Resolved [RESOLVED] Fill based on value

    Hi am not sure how to word this question....

    When user login I store that CustomerID in Globalvariable. Then on my form I have e.g.:

    Customer Details (textboxes) --> Orders (Datagridview)

    I have to admit that with new generation software I have be spoiled because I just drag datasources to form... This means that ALL data is loaded and I just browse to customer I want. But now I am trying to improve on this. I only want to load customer records based on that CustomerID..

    So for the customers TableAdapter I modify SELECT to include a WHERE statement:

    Code:
    SELECT CustomerID, CustomerName
    FROM tblCustomers
    WHERE (CustomerID = 1)
    This works perfect..... for customerID 1... But now Customer 2 logs in and that ID is stored in Globalvariable. So my question is this: How can I get that ID from Globalvariable into my WHERE statement?

    Thanks
    Last edited by schoemr; Feb 28th, 2018 at 01:43 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Fill based on value

    Don't modify the default query. Add a new query to the same table adapter, which will add new methods to execute that query. If you want to filter data by CustomerID, you would name the methods FillByCustomerID and GetDataByCustomerID. After that, this code will get all the data:
    vb.net Code:
    1. myTableAdapter.Fill(myDataSet.Customers)
    and this code will get only data for that ID:
    vb.net Code:
    1. myTableAdapter.FillByCustomerID(myDataSet.Customers, customerID)

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

    Re: Fill based on value

    Note that, when you add the query, you don't put a value in the SQL code but rather a parameter. Depending on the database, you might use a named parameter:
    sql Code:
    1. WHERE CustomerID = @CustomerID
    or an unnamed parameter:
    sql Code:
    1. WHERE CustomerID = ?
    That will then add a parameter to the command object inside the table adapter and that is mapped to a parameter on the FillByCustomerID method.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Fill based on value

    Got it , thanks JMC

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: [RESOLVED] Fill based on value

    If you're going to use typed DataSets, I would suggest doing some reading on table adapters.

    https://msdn.microsoft.com/en-us/library/7zt3ycf2.aspx

    It's a subject that a lot of people, including many experienced developers, don't know a lot about and end up doing or recommending bad things as a result.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: [RESOLVED] Fill based on value

    Okay I will do that, thank you

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