tags:

views:

47

answers:

1

I have a "junction" table that stores a user's userID (same userID that is in aspnet_membership table) and an ID for a product. I have added this table to a .dbml file. I am trying to do an insert into the table using LINQ and it's failing. Both fields in the junction table are set as primary keys, however, I do not have the aspnet_membership table in the .dbml file (nor do I have a relationship set between the two tables - it won't let me because the userID field in aspnet_membership is not a primary key). Here's my code (I have used similar code in other locations without a problem so I suspect the .dbml configuration is the problem). Any ideas why the inserts are failing?

bool success = false;
ASIStoreDataDataContext storeContext = new ASIStoreDataDataContext();
try
{
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
        STOR_NewsletterUser newsSub = new STOR_NewsletterUser();
        newsSub.SNWS_NewsLetterID = newsletterID;
        newsSub.UserID = userID;
        storeContext.STOR_NewsletterUsers.InsertOnSubmit(newsSub);
        storeContext.SubmitChanges();
        success = true;
    }
}
catch { }

return success;
+1  A: 

First of all, get rid of that try/catch block. Any random exception could be happenning in there, and you wouldn't know about it because you're eating the exception. Don't ever do that.

Now, maybe if you had posted the exception or other reason you believe the insert is failing, I could give you a better response. Instead, the best I can suggest is that you change your code as follows:

ASIStoreDataDataContext storeContext = new ASIStoreDataDataContext();
try
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
        return false;

    STOR_NewsletterUser newsSub = new STOR_NewsletterUser();
    newsSub.SNWS_NewsLetterID = newsletterID;
    newsSub.UserID = userID;
    storeContext.STOR_NewsletterUsers.InsertOnSubmit(newsSub);
    storeContext.SubmitChanges();
    return true;
}
catch (Exception ex) {
    Console.WriteLine(ex.ToString()); // Or Debug.Print, or whatever
    throw;                            // Don't eat the exception
}

When you've got that done, edit your question to include the exception that is displayed. If no exception occurs, then edit your question to say what does happen that should not happen, or what does not happen that should happen.

John Saunders