Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi am using a gridview . I want to save the data in the DB by using foreach loop .Mean while I have to check whether duplicate records are there are not in gridview . Can any one suggest me how to do . Thanks in advance.

C#
//Before inserting/updating the formula, insert the existing formula in OldFormula table
       btnSave.Enabled = false;
       fpsPL.FeedTypeId = Convert.ToInt32(ddlFeedType.SelectedValue);
       fpsPL.UpdatedBy = Session["username"].ToString();
       bool archived = false;
       archived = fpsBAL.InsertOldFormula(fpsPL);
       if (archived)
       {
           string products = "";
           foreach (GridViewRow row in gvFormula.Rows)
           {
               TextBox txt = (TextBox)row.Cells[2].FindControl("txtWeight");
               DropDownList ddlproduct = (DropDownList)row.FindControl("ddlproduct");
               if (txt != null)
               {
                   if (txt.Text.Trim() != "" && Convert.ToSingle(txt.Text.Trim()) > 0)
                   {
                       fpsPL.FeedTypeId = Convert.ToInt32(ddlFeedType.SelectedValue);
                       fpsPL.Product = ddlproduct.SelectedItem.Text;
                       fpsPL.Weight = Convert.ToSingle(txt.Text.Trim());
                       fpsPL.UpdatedBy = Session["username"].ToString();
                       bool added = false;
                       added = fpsBAL.InsertFormulaProduct(fpsPL);
                       if (!added)
                       {
                           products = row.Cells[2].Text.Trim() + ",";
                       }
                   }
               }
           }
           if (products.Trim() != "")
           {
               //Few products have not been added in formula, convey the same
               ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "InsertProduct", "alert('Products " + products.TrimEnd(',') + " are not added into formula.');", true);
           }
       }
       else
       {
           ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Archive", "alert('Insertion/Updation failed, check data once');", true);
       }
       btnSave.Enabled = true;
Posted
Updated 23-Sep-13 20:09pm
v2
Comments
ArunRajendra 24-Sep-13 2:03am    
Post your code.
Thanks7872 24-Sep-13 2:06am    
duplicate records are there or not? What is there?
Bhagavan Raju M 24-Sep-13 2:12am    
Duplicate records are there I have to eliminate
Bhagavan Raju M 24-Sep-13 2:10am    
@rohan Duplicate records are there I have to eliminate
Thanks7872 24-Sep-13 2:11am    
Use reply button while making comment. I already asked you what do you mean by 'there'?

1 solution

C#
try
           {
               if (MSGrid1.Col == 0)
               {
                   putslno();
               }
               else if (MSGrid1.Col == 2)
               {
                   int currow = MSGrid1.Row;
                   for (int i = 1; i <= MSGrid1.Rows - 1; i++)
                   {
                       for (int j = i + 1; j <= MSGrid1.Rows - 1; j++)
                       {
                           if ((MSGrid1.get_TextMatrix(j, 1) == MSGrid1.get_TextMatrix(i, 1)))
                           {
                               if (MSGrid1.get_TextMatrix(j, 1) != "")
                               {
                                   MessageBox.Show("This Item Name is already selected");
                                   MSGrid1.set_TextMatrix(j, 1, "");
                                   MSGrid1.set_TextMatrix(j, 2, "");
                                   MSGrid1.set_TextMatrix(j, 3, "");
                                   MSGrid1.set_TextMatrix(j, 4, "");
                                   MSGrid1.set_TextMatrix(j, 5, "");
                                   MSGrid1.Focus();
                                   MSGrid1.Col = 1;
                                   return;
                               }
                           }
                       }
                   }
                   PlaceTxtBox();
               }
               else if (MSGrid1.Col == 4)
               {
                   PlaceTxtBox();
               }
               else if (MSGrid1.Col == 6)
               {
                   GridCombo();
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);

}

i have used msfluxgirid u can chk same logic with data grid
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900