-
Feb 28th, 2018, 01:40 AM
#1
Thread Starter
Frenzied Member
[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.
-
Feb 28th, 2018, 02:06 AM
#2
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:
myTableAdapter.Fill(myDataSet.Customers)
and this code will get only data for that ID:
vb.net Code:
myTableAdapter.FillByCustomerID(myDataSet.Customers, customerID)
-
Feb 28th, 2018, 02:09 AM
#3
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:
WHERE CustomerID = @CustomerID
or an unnamed parameter:
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.
-
Feb 28th, 2018, 03:46 AM
#4
Thread Starter
Frenzied Member
-
Feb 28th, 2018, 03:56 AM
#5
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.
-
Feb 28th, 2018, 04:13 AM
#6
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|