this is our insertion logic. but the problem is that after inserting one record we reset the textfields to clear them.and after that if we again press the insert button it duplicate the previous record into our database.we want to tackle with this??????so that no duplicate entry should be inserted into the database ????????????????

 protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        if (TextBox1.Text == "" || TextBox2.Text == "" || txtEventDate.Text == "" || TextBox4.Text == "" || TextBox3.Text == "" || TextBox6.Text == "" || TextBox7.Text == "")
        {
            //lblStatus.Text = "***You have missed any field*** ";
            Response.Write("<script Language=javascript> alert('***You have missed any field***') </script>");
            // lblStatus.Text = "Records require an Ref_no,Description,Purchase Price,Sale Price,Quantity,Warning Quantity,Expiry Date,Vendor.";
            return;

        }



        if (DropDownList1.Text == "Tendor Purchase" && TextBox8.Text == "")
        {
            Response.Write("<script Language=javascript> alert('***Please enter Tendor Number***') </script>");
            return;
        }
        if (DropDownList1.Text == "Spot Purchase" && TextBox8.Text != "")
        {
            Response.Write("<script Language=javascript> alert('***Tendor Number is not required for Spot Purchase***') </script>");
            return;
        }

        string sqlIns = "INSERT INTO Books_Magazines (Book_Name,Author_Name,Ref#,Publisher,Date_Of_Purchase,Vendor_Supplier,Price,Type_Of_Purchase,Tendor_No) VALUES (@Book_Name,@Author_Name,@Ref#,@Publisher,@Date_Of_Purchase,@Vendor_Supplier,@Price,@Type_Of_Purchase,@Tendor_No)";
        SqlConnection con = new SqlConnection();

        con.ConnectionString = "Data Source=.;Initial Catalog=ecsd1;User ID=sa;Password=786";
        SqlCommand cmd = new SqlCommand(sqlIns, con);

        try
        {
            con.Open();

                cmd.Parameters.Add("@Book_Name", TextBox1.Text);
                cmd.Parameters.Add("@Author_Name", TextBox2.Text);
                cmd.Parameters.Add("@Ref#", TextBox3.Text);
                cmd.Parameters.Add("@Publisher", TextBox4.Text);
                cmd.Parameters.Add("@Date_Of_Purchase", txtEventDate.Text);
                cmd.Parameters.Add("@Vendor_Supplier", TextBox6.Text);
                cmd.Parameters.Add("@Price", TextBox7.Text);
                cmd.Parameters.Add("@Type_Of_Purchase", DropDownList1.Text);
                cmd.Parameters.Add("@Tendor_No", TextBox8.Text);
                cmd.ExecuteNonQuery();
                lblStatus.Text = "***Record Inserted.***";

                if (cmd.ExecuteNonQuery() == 1)
                {
                    TextBox1.Text = "";
                    TextBox2.Text = "";
                    TextBox3.Text = "";
                    txtEventDate.Text = "";
                    TextBox4.Text = "";
                    DropDownList1.Text = "";
                    TextBox6.Text = "";
                    TextBox7.Text = "";
                    TextBox8.Text = "";
                    return;
                }


        }


        catch (Exception err)
        {
            lblStatus.Text = "***Error in inserting  record***";
            lblStatus.Text += err.Message;
        }
        finally { con.Close(); }


    }

You are calling your cmd.execute twice, once to execute it and again to ensure it worked and clear out the textboxes. That is where the duplication is coming from. Remove the first instance and the second will run and provide you with your check that it succeded at the same time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.