|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Checking unique constraint from applicationI have a unique column contraint (Indexed, non-clustered, Not-Null, Not PK, the column value is nvarchar type(50)) on a dataset. I am trying to figure out how to check for the unique value before submitting update back to database. I want to trap the user entry if it violates the constraint, and supply a message dialog so the user can make corrections before re-submitting the update. The message only needs to let the user know that the textbox entry they have attempted to save is already in the database, and they should try some other unique entry. So far, I haven't figure out how to raise the error, or trap it for processing at the application level. However, the table simply does not save the violating entry; nor does it return an error, so if a user does supply an entry that is not unique, they will not recieve any message indicating such, and it is not apparent until they try to look-up this new, redundant entry -- which isn't in the table. TIA Gary gary7,
Add this code to your sp. Assuming [c1] is the primary key and [c2] is the unique column: if exists (select * from dbo.t1 where c1 != @c1 and c2 = @c2) begin raiserror ('There is another row with same value as @c2.', 16, 1) return -1 end .... AMB Show quote "gary7" wrote: > Hi Folks, > I have a unique column contraint (Indexed, non-clustered, Not-Null, Not > PK, the column value is nvarchar type(50)) on a dataset. I am trying to > figure out how to check for the unique value before submitting update > back to database. > > I want to trap the user entry if it violates the constraint, and supply > a message dialog so the user can make corrections before re-submitting > the update. > > The message only needs to let the user know that the textbox entry they > have attempted to save is already in the database, and they should try > some other unique entry. > > So far, I haven't figure out how to raise the error, or trap it for > processing at the application level. > However, the table simply does not save the violating entry; nor does > it return an error, so if a user does supply an entry that is not > unique, they will not recieve any message indicating such, and it is > not apparent until they try to look-up this new, redundant entry -- > which isn't in the table. > > TIA > > Gary > > Thanks Alejandro;
I'll give this a try! Gary Alejandro Mesa wrote: Show quote > gary7, > > Add this code to your sp. Assuming [c1] is the primary key and [c2] is the > unique column: > > if exists (select * from dbo.t1 where c1 != @c1 and c2 = @c2) > begin > raiserror ('There is another row with same value as @c2.', 16, 1) > return -1 > end > > ... > > > AMB > > "gary7" wrote: > > > Hi Folks, > > I have a unique column contraint (Indexed, non-clustered, Not-Null, Not > > PK, the column value is nvarchar type(50)) on a dataset. I am trying to > > figure out how to check for the unique value before submitting update > > back to database. > > > > I want to trap the user entry if it violates the constraint, and supply > > a message dialog so the user can make corrections before re-submitting > > the update. > > > > The message only needs to let the user know that the textbox entry they > > have attempted to save is already in the database, and they should try > > some other unique entry. > > > > So far, I haven't figure out how to raise the error, or trap it for > > processing at the application level. > > However, the table simply does not save the violating entry; nor does > > it return an error, so if a user does supply an entry that is not > > unique, they will not recieve any message indicating such, and it is > > not apparent until they try to look-up this new, redundant entry -- > > which isn't in the table. > > > > TIA > > > > Gary > > > > So how to use this in the existing table query is my next question.
Any ideas? TIA gary7 wrote: Show quote > Thanks Alejandro; > I'll give this a try! > > Gary > > Alejandro Mesa wrote: > > gary7, > > > > Add this code to your sp. Assuming [c1] is the primary key and [c2] is the > > unique column: > > > > if exists (select * from dbo.t1 where c1 != @c1 and c2 = @c2) > > begin > > raiserror ('There is another row with same value as @c2.', 16, 1) > > return -1 > > end > > > > ... > > > > > > AMB > > > > "gary7" wrote: > > > > > Hi Folks, > > > I have a unique column contraint (Indexed, non-clustered, Not-Null, Not > > > PK, the column value is nvarchar type(50)) on a dataset. I am trying to > > > figure out how to check for the unique value before submitting update > > > back to database. > > > > > > I want to trap the user entry if it violates the constraint, and supply > > > a message dialog so the user can make corrections before re-submitting > > > the update. > > > > > > The message only needs to let the user know that the textbox entry they > > > have attempted to save is already in the database, and they should try > > > some other unique entry. > > > > > > So far, I haven't figure out how to raise the error, or trap it for > > > processing at the application level. > > > However, the table simply does not save the violating entry; nor does > > > it return an error, so if a user does supply an entry that is not > > > unique, they will not recieve any message indicating such, and it is > > > not apparent until they try to look-up this new, redundant entry -- > > > which isn't in the table. > > > > > > TIA > > > > > > Gary > > > > > > |
|||||||||||||||||||||||