Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to handle a primary key error in ASP.NET

    Hi everybody!

    I'm new in programming with ASP.NET in Microsoft Visual Studio 2008, with the page language being C#.

    I made a simple application to register wireless connection users. When a user doesn't instert a primar key, in my case a MAC address, the following error msg is displayed: Violation of PRIMARY KEY constraint 'aaaaaclients_PK'. Cannot insert duplicate key in object 'dbo.clients'. The duplicate key value is (XXXXXXXXX).
    The statement has been terminated.

    I know why the message is displayed, but I want to customize this and put my own message as the user will not understand what that is. Please help me how to handle this. (If there are any codes, please tell me where exactly to insert the codes)

    The code (part) of the .aspx page for registering clients(where the error occurs) is this:

    Code:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:wirelessConnectionString1 %>" InsertCommand="INSERT INTO [clients] ([FirstName], [Surname], [RegNo], [Brand], [MAC], [RegDate], [Expiry], [Remarks]) VALUES (@FirstName,@MAC)" 
     
    ProviderName="<%$ ConnectionStrings:wirelessConnectionString1.ProviderName %>" 
    SelectCommand="SELECT [FirstName], [Surname], [MAC] FROM [clients]" 
     
    UpdateCommand="UPDATE [clients] SET [FirstName] = @FirstName, [Surname] = @Surname, WHERE [MAC] = @MAC">
     
    <deleteparameters>
     
    <asp:Parameter Name="MAC" Type="String" />
     
    </deleteparameters>
    <insertparameters> 
     
    <asp:Parameter Name="FirstName" Type="String" />
     <asp:Parameter Name="Surname" Type="String" />
    <asp:Parameter Name="MAC" Type="String" />
     
    
    </insertparameters>
    Thanks in advance,
    Frank.

  • #2
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No one to help?

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,718
    Thanks
    41
    Thanked 191 Times in 190 Posts
    i think it is because (and it probably worked once) if you do not validate for that field and it get sent it get's sent as a string still
    Code:
    string value = "";
    and now you try to insert into a table a new PK of a unique value of "" where "" already exists. Check you DB there is probably a PK with value of "" somewhere.

    IMO you should not be using a MAC as a PK (even though they are unique); make a TAB_ID column and let SQL handle it's own PK'ing... don't try to mess with it's engine. Also you should validate for blank strings especially if you are banking on it using that string as a PK

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •