locked
How to Prevent Duplicate ID insert into gridview using viewstate... RRS feed

  • Question

  • User-367318540 posted

    I am inserting data into gridview from textboxes using viewstate now i want to prevent duplicate ID insert into gridview.

    Tuesday, August 20, 2019 2:05 PM

Answers

  • User-367318540 posted

    here i found to prevent duplicate ID insert into gridview

    protected void Button1_Click(object sender, EventArgs e)
            {
                insertbalgv();
            }
    
    private void insertbalgv()
    {
     	
    
                if
                     (txtOrder.Text == null)
                {
                    Response.Write("<script language='javascript'>alert('Please Input Orderno');</script>");
                }
                else
                {
                    if
                         (txtorderno.Text == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input Bale No');</script>");
                    }
                    else if
                    (txtqty.Text == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input QTY');</script>");
                    }
                    else if
    
                        ((int.Parse(txtOrder.Text) != (int.Parse(txtorderno.Text))))
                        lbmsg.Text = "Bale is Not in order";
                    else if
                            (txtorderno.Text == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input Orderno');</script>");
                    }
    
                    else if (DropDownList1.SelectedValue == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input Item');</script>");
                    }
                    else
                    {
                        {
                            if (DropDownList1.SelectedItem != null)
                            {
                                // get value and text which item you have selected
                                int codeitem = Convert.ToInt32(DropDownList1.SelectedItem.Value.ToString());
                                string Descriptionitem = DropDownList1.SelectedItem.Text.ToString();
    
                             
                                //add the selected item to gridview
    
                                DataTable dtgridview = ViewState["dt"] as DataTable;
                                DataRow dr2 = dtgridview.NewRow();
                                bool ifExist = false;
                                foreach (DataRow dr in dtgridview.Rows)
                                {
                                    if (dr["Prdno"].ToString() == TextBox1.Text.Trim())
                                    {
                                        ifExist = true;
                                        break;
                                    }
    
                                }
                                if (!ifExist)
                                {
                                    dr2["Prdno"] = TextBox1.Text.Trim();
                                    dr2["Codeitem"] = codeitem;
                                    dr2["Descriptionitem"] = Descriptionitem;
                                    dr2["Orderno"] = txtorderno.Text;
    
                                    dr2["QTY"] = txtqty.Text;
                                    dtgridview.Rows.Add(dr2);
                                    ViewState["dt"] = dtgridview;
                                    GridView1.DataSource = dtgridview;
                                    GridView1.DataBind();
                                }
                                else
                                {
                                   // this.lbgvck.Visible = true;
                                    lbgvck.Text = "Bale Alread Exits...";
                                }
                               
                                clear();
                            }
                        }
                    }
                }
            }
            
            private void clear()
            {
                TextBox1.Text = "";
                txtqty.Text = "";
                txtorderno.Text = "";
                DropDownList1.SelectedValue = "";
    
    
            
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 20, 2019 2:56 PM

All replies

  • User475983607 posted

    akhterr

    I am inserting data into gridview from textboxes using viewstate now i want to prevent duplicate ID insert into gridview.

    Keep in mind that we have no idea how your code works.   I assume simply write code that queries for the duplicate ID before updating ViewState with new records.

    Is there any reason why you are caching records in ViewState rather than using a database table?   Databases have constraints that stop this sort of problem.  Also the ASP.NET DataTable can be configured for apply unique constraints to a column.

    Tuesday, August 20, 2019 2:24 PM
  • User-367318540 posted

    Is there any reason why you are caching records in ViewState rather than using a database table?

    Because i have to insert multiple record into database at once ...

    Tuesday, August 20, 2019 2:55 PM
  • User-367318540 posted

    here i found to prevent duplicate ID insert into gridview

    protected void Button1_Click(object sender, EventArgs e)
            {
                insertbalgv();
            }
    
    private void insertbalgv()
    {
     	
    
                if
                     (txtOrder.Text == null)
                {
                    Response.Write("<script language='javascript'>alert('Please Input Orderno');</script>");
                }
                else
                {
                    if
                         (txtorderno.Text == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input Bale No');</script>");
                    }
                    else if
                    (txtqty.Text == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input QTY');</script>");
                    }
                    else if
    
                        ((int.Parse(txtOrder.Text) != (int.Parse(txtorderno.Text))))
                        lbmsg.Text = "Bale is Not in order";
                    else if
                            (txtorderno.Text == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input Orderno');</script>");
                    }
    
                    else if (DropDownList1.SelectedValue == "")
                    {
                        Response.Write("<script language='javascript'>alert('Please Input Item');</script>");
                    }
                    else
                    {
                        {
                            if (DropDownList1.SelectedItem != null)
                            {
                                // get value and text which item you have selected
                                int codeitem = Convert.ToInt32(DropDownList1.SelectedItem.Value.ToString());
                                string Descriptionitem = DropDownList1.SelectedItem.Text.ToString();
    
                             
                                //add the selected item to gridview
    
                                DataTable dtgridview = ViewState["dt"] as DataTable;
                                DataRow dr2 = dtgridview.NewRow();
                                bool ifExist = false;
                                foreach (DataRow dr in dtgridview.Rows)
                                {
                                    if (dr["Prdno"].ToString() == TextBox1.Text.Trim())
                                    {
                                        ifExist = true;
                                        break;
                                    }
    
                                }
                                if (!ifExist)
                                {
                                    dr2["Prdno"] = TextBox1.Text.Trim();
                                    dr2["Codeitem"] = codeitem;
                                    dr2["Descriptionitem"] = Descriptionitem;
                                    dr2["Orderno"] = txtorderno.Text;
    
                                    dr2["QTY"] = txtqty.Text;
                                    dtgridview.Rows.Add(dr2);
                                    ViewState["dt"] = dtgridview;
                                    GridView1.DataSource = dtgridview;
                                    GridView1.DataBind();
                                }
                                else
                                {
                                   // this.lbgvck.Visible = true;
                                    lbgvck.Text = "Bale Alread Exits...";
                                }
                               
                                clear();
                            }
                        }
                    }
                }
            }
            
            private void clear()
            {
                TextBox1.Text = "";
                txtqty.Text = "";
                txtorderno.Text = "";
                DropDownList1.SelectedValue = "";
    
    
            
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 20, 2019 2:56 PM
  • User475983607 posted

    Because i have to insert multiple record into database at once ...

    ViewState is not required to submit multiple records. 

    The solution is very fragile.  You'll lose the ViewState if the user refreshes the page.   How do you handle SQL errors?  

    Tuesday, August 20, 2019 3:17 PM
  • User-367318540 posted

    The solution is very fragile.  You'll lose the ViewState if the user refreshes the page.   How do you handle SQL errors?  

    i am using view state because i have master child record to insert into database means that master Record ID will get insert into each row of gridview before inserting data into database,Viewstate benefit is that user can review all row in gridview....and user will not refresh page when user add data into gridview....

    Tuesday, August 20, 2019 3:21 PM