Displaying a message after adding duplicate records in database

Posted by user1770370 on Stack Overflow See other posts from Stack Overflow or by user1770370
Published on 2012-11-18T10:37:30Z Indexed on 2012/11/18 11:00 UTC
Read the original article Hit count: 144

I wrote program in C# winforms and SQL server and LINQ to SQL. I use user control instead of form. In my user control, I put 3 textbox, txtStartNumber, txtEndNumber, txtQuantity. user define value of textboxes, when clicked button, it will insert some records according to the value of txtQuantity.

I want to when duplicate number is created, it won't add to database and display message. how do i do? I must write code in code behind or server side? i must set this in store procedure or trigger?

 private void btnSave_Click(object sender, EventArgs e)
        {
            long  from = Convert.ToInt64(txt_barcode_f.Text);
            long to = Convert.ToInt64(txt_barcode_t.Text);
            long quantity = Convert.ToInt64(to - from);
            int card_Type_ID=Convert.ToInt32(cmb_BracodeType .SelectedValue);
            long[] arrCardNum = new long[(to - from)];

            arrCardNum[0]=from;

            for (long i = from; i < to; i++)
             {
                 for(int j=0; j<(to-from) ;j++)
                 {
                 arrCardNum[j]=from+j;
                 string r = arrCardNum[j].ToString();
                 sp.SaveCards(r, 2, card_Type_ID, SaveDate, 2);
                 }
             }
        }

Stored Procedure code.

ALTER PROCEDURE dbo.SaveCards
@Barcode_Num int
,@Card_Status_ID int 
,@Card_Type_ID int
,@SaveDate varchar(10)
,@Save_User_ID int 


AS

BEGIN 
INSERT INTO [Parking].[dbo].[TBL_Cards]
           ([Barcode_Num]
           ,[Card_Status_ID]
           ,[Card_Type_ID]       
           ,[Save_User_ID])

     VALUES
           (@Barcode_Num
           ,@Card_Status_ID
           ,@Card_Type_ID
           ,@Save_User_ID)


    END  

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql-server