Solved

ASP.NET: Table Adapters and SELECT DISTINCT

Posted on 2008-06-20
13
1,369 Views
Last Modified: 2013-11-26
'lo guys,

I've not been having a lot of luck finding a neat solution to this issue so I thought I'd give a question a try.

I have an ASP.NET web application accessing an MS SQL database. The Data Access Layer is all currently done using TableAdapters and for the most part they're working very nicely with the ObjectDataSource component (kind of skipped Business Logic in most cases).

I want to populate a few drop down boxes with SELECT DISTINCT queries. The problem is two-fold.

First I can't create those queries and execute them successfully within the schema for an existing Table Adapter (unique / not null constraints).

And secondly, I can't create another TableAdapter including only the columns without constraints, it crashes (An unexpected error has occurred) on the last step and won't create the FillBy and GetBy part.

Here's where I get a bit stuck. I don't really understand anything in the source for the table adapters well enough to manually add stuff. And I'm having trouble thinking of an easy alternative without resorting to littering my code with SQL queries.

Is placing an SQL query to populate a DataSet for this in the Code Behind file a poor plan (in terms of ongoing maintenance)? Or is there a better place to put such things? Or a better / simpler alternative?

Thanks,

Chris Dent
0
Comment
Question by:Chris Dent
  • 7
  • 6
13 Comments
 
LVL 3

Expert Comment

by:JayeshKitukale
ID: 21830218
1. Please provide the table fields and their types here.
2. Provide the fields you are trying to select in the tableadapter which fails
3. Answer to your last question is YES - it is a bad practice
4. An alternative is to hand-code the tableadapter, but this is the last resort after we investigate from answers to above questions
0
 
LVL 3

Expert Comment

by:JayeshKitukale
ID: 21830226
If you have SQL management studio or from code try firing the select distinct query you want to create the adapter for.
0
 
LVL 70

Author Comment

by:Chris Dent
ID: 21830420

Hey :)

The query works perfectly when executed using query manager or as an SQL Data Source in ASP.

As far as I understand, when using the Table Adapter it generates a Schema on first creation. Then every query associated with the Table Adapter uses that same Schema (hence the warning when you create a query that doesn't quite).

It also executes successfully using Preview Data from the Table Adapter in Visual Studio.

In my case I have a main Table Adapter with:

SELECT * FROM Software_Installed

Where Software_Installed contains:

Linked_ID - SmallInt (Foreign Key)
Linked_Asset_Tag - varchar(50) (Foreign Key)
Software_Name - varchar(50)
Software_Publisher - varchar(50)
Software_Version - varchar(50)
Install_Date - datetime

Software_Name and Linked_Asset_Tag are a composite / combined Primary Key for the table. Because of that neither is allowed to be Null.

The query I want to use to populate the Drop Down Box is:

SELECT DISTINCT Software_Publisher FROM Software_Installed

Not exactly complicated :)

The problem comes, as far as I know, when it fills the Data Set it checks it for constraints, then fails because the returned data leaves most columns Null (I can see this in Preview Data on the Table Adapter).

I thought the normal approach was therefore to create a Table Adapter with a Schema that only contains the fields I'm interested in (those without constraints). However that just makes Visual Studio pop up the unexpected error.

At the moment the best way I can think of to get around this is to call a Public Function stored in a Class in the App_Code directory and have that fill and return a DataSet when called from the Code Behind for the page. Hopefully that is at least better than the SQL Data Source or having the query itself in the code behind?

Chris
0
 
LVL 3

Expert Comment

by:JayeshKitukale
ID: 21830489
Did you try without DISCTINCT in SELECT DISTINCT Software_Publisher FROM Software_Installed:
SELECT Software_Publisher FROM Software_Installed?
Does this work?
Your app_code approach looks fine as far as a quick solution is concerned.
0
 
LVL 70

Author Comment

by:Chris Dent
ID: 21830556

I hadn't, but I have now. It gives the same error.

Exception Details: System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Isn't it fun? ;)

Chris
0
 
LVL 3

Expert Comment

by:JayeshKitukale
ID: 21830573
Try adding one field to the select statement at a time until it works: Start from adding the primary fields. I assume you are creating a new TableAdapter.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Expert Comment

by:JayeshKitukale
ID: 21830580
Also check if adding another SELECT * from table work in the new table adapter?
0
 
LVL 70

Author Comment

by:Chris Dent
ID: 21830618

It works if I add the Primary fields (or the fields with constraints), but I get a lot of repetition (2270 rows in the table) because the row is no longer distinct.

Unless there's an alternative query to get the field I want?

Inability to come up with that just leads to back to my lack of knowledge of asp.net / visual basic / sql; maybe I should stick with Active Directory and networking, much safer :)

Chris
0
 
LVL 70

Author Comment

by:Chris Dent
ID: 21830640

I have a couple of other SELECT queries (in addition to the default) under each table adapter and those all work well.

e.g.

SELECT * FROM Software_Installed WHERE Software_Name=@Something

It's the just the DISTINCT, or any where the columns returned are limited and exclude Primary Keys / Indexes that seem to cause the problem.

Chris
0
 
LVL 3

Accepted Solution

by:
JayeshKitukale earned 500 total points
ID: 21830661
:) FYI these tools that generate code always have limitations - they work mostly only for the basic requirements. We started using TableAdapters in our last project and ended up using NHibernate which is much more serious non-microsoft non-rapid application development robust technology, but the learning curve is too steep to get started quickly.
0
 
LVL 3

Expert Comment

by:JayeshKitukale
ID: 21830688
For more info on such - see the Law of Leaky Abstractions by Joel (http://joelonsoftware.com)
0
 
LVL 70

Author Comment

by:Chris Dent
ID: 21830696

Fair enough :) I suspected it might not be an easy solution. It was worth a try in case I was just missing something really obvious.

I'll just stick with code in a public class to deal with these then. There are only 10 of this type at the moment.

Thanks for your help though, it's certainly appreciated :)

Chris
0
 
LVL 70

Author Closing Comment

by:Chris Dent
ID: 31469091
Thanks :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now