Frage

I have the following query called searchit

SELECT 2 AS sourceID, BLOG_COMMENTS.bID, BLOG_TOPICS.Topic_Title,
    BLOG_TOPICS.LFD, BLOG_TOPICS.LC,
    BLOG_COMMENTS.Comment_Narrative
FROM BLOG_COMMENTS INNER JOIN BLOG_TOPICS
    ON BLOG_COMMENTS.bID = BLOG_TOPICS.bID
WHERE  (BLOG_COMMENTS.Comment_Narrative LIKE @Phrase)

This query executes AND returns the correct results in the query builder! HOWEVER, the query needs to run in code-behind, so I have the following line:

DataTable blogcomments = btad.searchit(aphrase);

There are no null fields in any row of any column in EITHER of the tables. The tables are small enough I can easily detect null data. Note that bID is key for blog_topics and cID is key for blog comments.

In any case, when I run this I get the following error:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Tables have a 1 x N relationship, many comments for each blog entry. IF I run the query with DISTINCT and remove the Comment_Narrative from the return fields, it returns data correctly (but I need the other rows!) However, when I return the other rows, I get the above error!

I think tells me that there is a constraint on the return table that I did not put there, therefore it must somehow be inheriting that constraint from the call to the query itself because one of the tables happens to have a primary key defined (which it MUST have). But why does the query work fine in the querybuilder? The querybuilder does not care that bID is duped in the result (and it should not be), but the code-behind DOES care.

Addendum:

Just as tests,

  1. I removed the bID from the return list and I still get the error.
  2. I removed the primary key from blog_topics.bID and I get the same error.

This kinda tells me that it's not the fact that my bID is duped that is causing the problem.

Another test: I went into the designer code (I know it's nasty, I'm just desperate). I added the following:

    // zzz
    try
    {

        this.Adapter.Fill(dataTable);
    }
    catch ( global::System.Exception ex )
    {

    }

Oddly enough, when I run it, I get the same error as before AND it doesn't show the changes I've made in the error message:

Line 13909:            }
Line 13910:            BPLL_Dataset.BLOG_TOPICSDataTable dataTable = new BPLL_Dataset.BLOG_TOPICSDataTable();
Line 13911:            this.Adapter.Fill(dataTable);
Line 13912:            return dataTable;
Line 13913:        }

I'm stumped.... Unless maybe it sees I'm not doing anything in the try catch and is optimizing for me.

Another addendum: Suspecting that it was ignoring the test code I added to the designer, I added something to the catch. It produces the SAME error and acts like it does not see this code. (Well, okay, it DOES NOT see this code, because it prints out same as before into the browser.)

    // zzz
    try
    {

        this.Adapter.Fill(dataTable);
    }
    catch ( global::System.Exception ex )
    {
        System.Web.HttpContext.Current.Response.Redirect("errorpage.aspx");
    }

The thing is, when I made the original post, I was ALREADY trying to do a work-around. I'm not sure how far I can afford to go down the rabbit hole. Maybe I read the whole mess into C# and do all the joins and crap myself. I really hate to do that, because I've only recently gotten out of the habit, but I perceive I'm making a good faith effort to use the the tool the way God and Microsoft intended. From wit's end, tff.

War es hilfreich?

Lösung 2

My problem came from ignorance and a bit of dullness. I did not realize that just because a field is a key in the sql table does mean it has to be a key in the tableadapter. If one has a key field defined in the SQL table and then creates a table adapter, the corresponding field in the adapter will also be a key. All I had to do was unset the key field in the tableadapter and it worked.

Solution:

  • Select the key field in the adapter.
  • Right click
  • Select "Delete Key" (keeps the field, but removes the "key" icon)

That's it.

Andere Tipps

You don't really show how you're running this query from C# ... but I'm assuming either as a straight text in a SqlCommand or it's being done by some ORM ... Have you attempted writing this query as a Stored Procedure and calling it that way? The stored Procedure would be easier to test and run by itself with sample data.

Given the fact that the error is mentioning null values I would presume that, if it is a problem with the query and not some other element of your code, then it'd have to be on one of the following fields: BLOG_COMMENTS.bID BLOG_TOPICS.bID BLOG_COMMENTS.Comment_Narrative

If any of those fields are Nullable then you should be doing a COALESCE or an ISNULL on them before using them in any comparison or Join. It's situations like these which explain why most DBAs prefer to have as few nullable columns in tables as possible - they cause overhead and are prone to errors.

If that still doesn't fix your problem, then COALESCE/ISNULL all fields that are nullable and are being returned by this query. Take all null values out of the equation and just get the thing working and then, if you really need the null values to be null, go back through and remove the COALESCE/ISNULLs one at a time until you find the culprit.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top