473,385 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Filtering SQL view

I am trying to populate a dataviewgrid with records from a database that are
duplicates. I have created 3 views to get the information I want, but if I
try to apply a filter to my bindingsource control, I am returned all
records, not just the duplicates. Here are my 3 SQL views:
View1: - records are dated by year, this view filters all except current
year
SELECT TOP 100 PERCENT fldCliID, fldName2, fldName1, fldAdd1, fldAdd2,
fldCity, fldState, fldZip, fldStudioNo, fldGraduationYear,
fldOrganizationID,
fldCliNo
FROM dbo.tblClients
WHERE (fldGraduationYear = N'2007')
ORDER BY fldName2, fldName1

View2: - this view actully picks out the dups only
SELECT TOP 100 PERCENT fldName2, fldName1, fldCliID, fldAdd1, fldAdd2,
fldCity, fldZip, fldCliNo, fldState, fldGraduationYear, fldOrganizationID
FROM dbo.VIEW1
WHERE (fldName2 IN
(SELECT [fldName2]
FROM [View1] AS Tmp
GROUP BY [fldName2], [fldName1]
HAVING COUNT(*) 1 AND fldName1 =
dbo.View1.fldName1))
ORDER BY fldName2, fldName1

View3: - this is the view used in the program to pull the records into a
datatab;e
SELECT fldName2, fldName1, fldCliID, fldAdd1, fldAdd2, fldCity, fldZip,
fldCliNo, fldState, fldGraduationYear, fldOrganizationID
FROM dbo.VIEW2

After binding the datatable to the binding source and applying a filter (on
fldorganizationID), all records (for the selected organization) are
displayed.

Any ideas on a different way to accomplish this?
Aug 25 '06 #1
2 3956
"Tim Kelley" <tk*****@company.comwrote in
news:OK**************@TK2MSFTNGP03.phx.gbl:
I am trying to populate a dataviewgrid with records from a database
that are duplicates. I have created 3 views to get the information I
want, but if I try to apply a filter to my bindingsource control, I am
returned all records, not just the duplicates. Here are my 3 SQL
views: View1: - records are dated by year, this view filters all
except current year
SELECT TOP 100 PERCENT fldCliID, fldName2, fldName1, fldAdd1,
fldAdd2, fldCity, fldState, fldZip, fldStudioNo, fldGraduationYear,
fldOrganizationID,
fldCliNo
FROM dbo.tblClients
WHERE (fldGraduationYear = N'2007')
ORDER BY fldName2, fldName1

View2: - this view actully picks out the dups only
SELECT TOP 100 PERCENT fldName2, fldName1, fldCliID, fldAdd1,
fldAdd2, fldCity, fldZip, fldCliNo, fldState, fldGraduationYear,
fldOrganizationID FROM dbo.VIEW1
WHERE (fldName2 IN
(SELECT [fldName2]
FROM [View1] AS Tmp
GROUP BY [fldName2], [fldName1]
HAVING COUNT(*) 1 AND fldName1 =
dbo.View1.fldName1))
ORDER BY fldName2, fldName1

View3: - this is the view used in the program to pull the records into
a datatab;e
SELECT fldName2, fldName1, fldCliID, fldAdd1, fldAdd2, fldCity,
fldZip, fldCliNo, fldState, fldGraduationYear, fldOrganizationID
FROM dbo.VIEW2

After binding the datatable to the binding source and applying a
filter (on fldorganizationID), all records (for the selected
organization) are displayed.

Any ideas on a different way to accomplish this?

It not clear what you are trying to do. As I am currently learning all
this I decided to take up your challenge.

------------------------------------------------------------------------
The following view will give you all records that are duplicates.
(assuming you want/need to use views, though I've no idea why you are
using 3)
------------------------------------------------------------------------
CREATE VIEW vwClientDuplicates
(fldName2, fldName1, fldCliID, fldAdd1, fldAdd2, fldCity, fldZip,
fldCliNo, fldState, fldGraduationYear, fldOrganizationID, [Rows])
AS
SELECT fldName2, fldName1, fldCliID, fldAdd1, fldAdd2, fldCity,
fldZip,
fldCliNo, fldState, fldGraduationYear, fldOrganizationID, count(*)
FROM tblClients
WHERE fldGraduationYear != DATEPART(yyyy, getdate())
GROUP BY fldName2, fldName1, fldCliID, fldAdd1, fldAdd2, fldCity, fldZip,
fldCliNo, fldState, fldGraduationYear, fldOrganizationID
HAVING count(*) 1

------------------------------------------------------------------------
The following code will allow you to filter, give or take a few
modifications.
------------------------------------------------------------------------
private void Form1_Load(object sender, System.EventArgs e)
{
// Query
string queryStr =
"SELECT * FROM View1";

// Create Connection
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = "Data Source=(local); Initial Catalog =
TEST; Integrated Security=SSPI";

// Create Command
SqlCommand cmd = cnn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = queryStr;

// Create DataSet
dsClients ds = new dsClients();

// Create DataAdapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "tblClients");

// Open Connection
cnn.Open();

// DataReader
SqlDataReader dr = cmd.ExecuteReader();

comboBox1.Text = "Select...";
// Populate ComboBox
while(dr.Read())
{
comboBox1.Items.Add(dr.GetString(10));
}

comboBox1.DisplayMember =
ds.tblClients.fldOrganizationIDColumn.ToString();
comboBox1.ValueMember =
ds.tblClients.fldOrganizationIDColumn.ToString();

// Close Reader
dr.Close();
// Close Connection
cnn.Close();
// Create View
dv = new DataView(ds.tblClients);
dataGrid1.DataSource = dv;

}

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
// Filter
dv.RowFilter = "fldOrganizationID = '" +
comboBox1.SelectedItem + "'";
}

------------------------------------------------------------------------

HTH
JTC ^..^
Aug 26 '06 #2
Correction :o
private void Form1_Load(object sender, System.EventArgs e)
{
// Query
string queryStr =
"SELECT * FROM vwDuplicates" // NOT view1
Aug 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Ayzin | last post by:
Hi, I have a problem that might be easy to solve(possibly, I've just overlooked an easy solution). Here we go: I have a dataset with 2 datatables in it. Now, I need to do the following: if...
1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
4
by: Doug | last post by:
I have your typically form/subform. You enter the account number in a textbox and select whether you want to see the detail or summary information on the main form. Both fields I want to filter...
8
by: | last post by:
hi, i have a form on which a user can choose specific criteria such as dates etc, in order to filter the report that is called from the form. i do this by using the Where section of the...
2
by: Konrad | last post by:
Hi Can you point examples in .NET of filtering (avoiding) displaying web pages with unwanted content on machine with ie? Thanks Konrad
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
3
by: iamguyster | last post by:
Hi, I have an exercise I need to give to my pupils (I'm a teacher!) and I am trying to get a query working preferably using the query design view, without having to edit the SQL. The query involves...
2
by: =?Utf-8?B?amV6MTIzNDU2?= | last post by:
Hi ASP.Net experts I have a archived SQL Server 2000 database table with about 300,000 records, and need to display filtered data on a ASP.Net 2 web page. Basic filtering needs to be done on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.