Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent duplicate enteries being inserted into SQL server db?
    primarykey
    data
    text
    <p>net, razor, webmatrix, working with .cshtml pages.</p> <p>I am using a jQuery to open a modal form and using that form to insert data into the SQL db (SQLExpress 2012). I am trying to restrict duplicate enteries by using the following code but get a message from web page [object XMLHttpReques] instead.</p> <p>AddDiscount.cshtml code is:</p> <pre><code>@{ if(IsPost){ var db = Database.Open("A-LensCatFE-01SQL") ; var CCode=UrlData[0]; var Q1 = "SELECT * FROM dbo.tblPromos WHERE CustomerCode =@0"; var ds = db.ExecuteDataSet(Q1, CCode); if(ds.Tables.Count &gt; 0 &amp;&amp; ds.Tables[0].Rows.Count &gt; 0) { MessageBox.Show("Duplicates"); } else { var sql = "INSERT INTO dbo.tblPromos (CustomerID, CustomerCode, Discount, VAT, VATRate, iScription, SRPBasedOn)" + "VALUES (@0, @1, @2, @3, @4, @5, @6)"; var customerid = Request["customerid"]; var customercode = Request["customercode"]; var discount = Request["discount"]; var vat = Request["vat"] == "on" ? true : false; var vatrate = Request["vatrrate"]; var iscription = Request["iscription"]; var srpbasedon = Request["srpbasedon"]; db.Execute(sql, customerid, customercode, discount, vat, vatrate, iscription, srpbasedon); } } } </code></pre> <p>Your help is needed, many thanks.</p> <p>Stored Procedure code is:</p> <p>CREATE PROCEDURE dbo.spDiscDup</p> <pre><code>@CustomerID int =NULL, @CustomerCode nvarchar(max)=NULL, @Discount decimal(5,2)=NULL, @VAT bit =NULL, @VATRate decimal (5,2)=NULL, @iScription money=NULL, AS BEGIN IF EXISTS(SELECT * FROM tblPromos WHERE CustomerCode=@CustomerCode) ("Duplicates") ELSE INSERT INTO dbo.tblPromos (CustomerID, CustomerCode, Discount, VAT, VATRate, iScription, SRPBasedOn)" + "VALUES (@0, @1, @2, @3, @4, @5, @6) </code></pre> <p>END GO</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload