I am developing an asp.net website using typed dataset as DAL , everything works fine until today I was about to add some new functionality to my website
here is the abstract :
get amount from amount_field from user_table and calculate something then update user_table
here is my sql query for getting amount_field in typed dataset :
select userId , Amount from [user] WHERE userId=@userid
I named this query : getUserCreditByID(@userid)
and in my BLL I call this query in such this way :
public static int getuseramount(long id)
{
int amount = 0;
userTableAdapters.userTableAdapter usert = new userTableAdapters.userTableAdapter();
user.userDataTable userd = usert.GetUserCreditByID(id);
foreach (user.userRow R in userd)
{
amount = R.Amount;
}
usert.Dispose();
userd.Dispose();
return amount;
}
and when I call this function I’ll get this error :
Failed to enable constraints. One or
more rows contain values violating
non-null, unique, or foreign-key
constraints.
but when I change the DAL’s sql query into this :
SELECT userID, username, password, address1, address2, tel1, tel2, cell, active, email, showEmail, last_login, Amount, registerdate, websiteUrl, vote, registerIP, city,
firstname, lastname
FROM [user]
WHERE (userID = @userid)
everything works fine
I am dazed about this , what is the problem?
It’s better to include all of the fields within the user table anyway; maybe because fields were omitted, it inserts null? Probably a stretch…
Better yet, if you just want those two fields, use an update stored procedure so that you have finer grained control over the update and can update that field correctly. If you map the update proc to the table, then the update should work fine.