Thursday, December 29

To Preventing duplicate primary key value in sql server table in asp.net

 protected void btninsert_Click(object sender, EventArgs e)
    {
        myDataClassesDataContext dc = new myDataClassesDataContext();
        try
        {
            Test obj = new Test();
            obj.hello = "shiba";
            obj.hello21 = "shiba1";
            obj.hello25 = "shiba3";

            dc.Tests.InsertOnSubmit(obj);
            dc.SubmitChanges();
            Gv2.DataSource = dc.Tests;
            Gv2.DataBind();
        }
        catch(Exception ex)
        {
            if (ex.Message.Contains("PRIMARY KEY"))
            {
               
                Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('This data is already exist');</script>");
            }
            else

            {
                throw;
            }
        }
    }

Thanks shibashish mohanty

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty