asp.net gridview data insert two times how to avoid repeate

RAVI 1,056 Reputation points
2024-11-20T03:38:06.81+00:00

Hello

This is my gridview data on button click i have foreach insert query

User's image

most of them time it insert only one time but sometime it insert like this how to avoid duplicate insert

User's image

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,520 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RAVI 1,056 Reputation points
    2024-11-20T07:41:54.8766667+00:00

    how to avoid duplicate insert of gridview data using asp.net c# on button click for example

    SqlConnection zcon = new SqlConnection(ConfigurationManager.ConnectionStrings["invConnectionString"].ConnectionString);
                    zcon.Open();
                    foreach (GridViewRow gvr in GridView3.Rows)
                    {
                       
                       string data1 = ((Label)gvr.FindControl("L1")).Text;
                       string data2 = ((Label)gvr.FindControl("L2")).Text;
       	           string data3 = ((Label)gvr.FindControl("L3")).Text;
    
                        SqlCommand ck = new SqlCommand("Insert into Store(Format,Name,Qty)values(@Format,@Name,@Qty)", zcon);
    
                        ck.Parameters.Add(new SqlParameter("@Format", data1));
                        ck.Parameters.Add(new SqlParameter("@Name", data2));
                        ck.Parameters.Add(new SqlParameter("@Card_Qty", data3));  
                        ck.ExecuteNonQuery();
    
                    }
    
    

    My gridview has only this data

    11111

    sometimes it goes two times to sql database table or more how to avoid such duplicates..


  2. AgaveJoe 28,541 Reputation points
    2024-11-20T10:34:47.8366667+00:00

    sometime it saving two or more times how to avoid such duplication You did not explain the source of the double entry or why a duplicate entry is a problem in your unknown design. Could it be the user double click a button? Maybe the submitted submitted fields should be unique?

    If we assume your design requires uniqueness, simply check if the record exists before adding the record. If there is a unique combination of fields in your design then add a unique constraint to the table.

    IF NOT EXISTS(SELECT (1) FROM Store WHERE Format = @Format AND Name = @Name AND Qty = @Qty)
        Insert into Store(Format,Name,Qty) values(@Format,@Name,@Qty)
    
    
    

    If you are trying to stop a user from double clicking the submit button then write a JavaScript click handler to and disable the button. We cannot see your markup so at this point it is not clear how to approach the JavaScript application. There are many online examples of disabling a button, just do a Google search.

    https://www.google.com/search?q=JAvaScript+stop+submit+button+double+click+in+web+forms

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.