0

I'm trying to check for duplicate pks when trying to insert a new record. I want to show an error if it does, but i'm not sure where i am going wrong/if this is the right way to go.

Please see below. I put a breakpoint at the function AgentSave_Click and saw its its not going into the IF statement

<asp:Button ID="AgentSave" runat="server" CausesValidation="true" OnClick="AgentSave_Click" Style="margin-left: 0px" Text="Save"   />

     protected void AgentSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (AccountNumber.Text.Trim().Equals("select PRIMARYKEYNAME from TABLEXYZ"))
            {

                Response.Write("<script type=\"text/javascript\">" + "window.alert('ERROR: The Account Number entered is already assigned to an Agent.');" + "</script>");
                AccountNumber.Focus();
            }
            else
            {.....
New2This
  • 253
  • 1
  • 6
  • 22

2 Answers2

2

OnClientClick property is meant to execute some javascript code on client before data is send back to server.

According your answer you are trying to execute server code. So you should replace onclientclick with onclick="duplicate_PK" which gets executed on server.

Regards, Uros

Uroš Goljat
  • 1,806
  • 14
  • 15
0
<asp:Button ID="AgentSave" runat="server"  OnClick="AgentSave_Click" Style="margin-left: 0px" Text="Save"   />

remove CausesValidation="true" or chANGE TO CausesValidation="false"

Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
  • i need this button to validate certain fields on the form when being enter like the PK field and other. y remove the validation? – New2This Nov 11 '13 at 18:28
  • ok.. check this [link](http://stackoverflow.com/questions/14191235/i-must-add-causesvalidation-false-to-every-asp-button-to-work-why) and reply – Sajad Karuthedath Nov 11 '13 at 18:31