You must Sign In to post a response.
  • Category: ASP.NET

    Preventing duplicate entries in gridview in

    I have a gridview in my website application in which I am Inserting data into it. I need to show error message while entering the repeated item in the gridview. Here is my code for inserting data into the gridview.

    protected void AddNew(object sender, EventArgs e)
    Control control = null;
    if (GridView1.FooterRow != null)
    control = GridView1.FooterRow;
    control = GridView1.Controls[0].Controls[0];
    string SlNo = (control.FindControl("txtSlNo") as TextBox).Text;
    string Code = (control.FindControl("txtcode") as TextBox).Text;
    string details = (control.FindControl("txtdetails") as TextBox).Text;
    string Qty = (control.FindControl("txtqty") as TextBox).Text;
    string UPrice = (control.FindControl("txtuprice") as TextBox).Text;
    using (SqlConnection con = new SqlConnection("Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xx;Password=xxxxx"))
    using (SqlCommand cmd = new SqlCommand())
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "INSERT INTO [qtable] VALUES(@Code,@details,@Qty,@UPrice)";
    cmd.Parameters.AddWithValue("@SlNo", SlNo);
    cmd.Parameters.AddWithValue("@Code", Code);
    cmd.Parameters.AddWithValue("@details", details);
    cmd.Parameters.AddWithValue("@Qty", Qty);
    cmd.Parameters.AddWithValue("@UPrice", UPrice);
  • #762718
    Hello Shivaji,

    I want to clear one thing. Whether you want to show an error while duplicate entry inserting into database or while binding data to gridview and you got duplicate entry.

    Let me clear this thing so that we can help you in better way.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762721
    Before asking the question you should be very clear what you need. As per my understand the post your requirement is you need to restrict duplicate entries while entering, in that case instead of use direct query in code behind wrote one Stored Procedure and call that procedure in your code behind, and while inset time check whether record is exist in database or not. If not exists then insert into database else show your message.

    Declare @exist varchar(100);

    set @exist= (select col from tablename where conditions;

    if( @exist is not null )
    --insert statement
    -- alert message

    and return that the result using output parameter and then show it in your application.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #762722
    @Nirav, while I entering data into gridview at that time it will need to show error message that the" code u r entering is already exist" I need to show this error while at the time of entering the data.

  • #762950
    Check whether the primary key value that you are inserting in database already exists or not. And if it exists, show the error message. For that you need to compare the primary key value that you are inserting with all the primary key values in that particular table.

  • Sign In to post your comments