Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
foreach (Selected s1 in objlistCropped)
                   {
                       string strImg2 = s1.Image;

                       if (strImg1 == strImg2)
                       {
                           dtPreInstallation.Rows.Add(strImg2, s.strText);
                       }
                    }



strImg1 = Img1.Jpeg,
I need to restrict adding img1.jpeg again if its already there in datatable.

Thanks in Advance
Posted
Updated 20-Jan-15 23:12pm
v2

You can use a Unique constraint[^] to do the job.
 
Share this answer
 
Comments
Renjith_R 21-Jan-15 5:29am    
Its not working :(
George Jonsson 21-Jan-15 6:04am    
It should work to make the column containing the file name unique, but you need to take care of the exception properly.
Can you you explain what is not working and how you expect it to work?
Wendelius 21-Jan-15 8:39am    
Why do you say it's not working? Can you post the code you have tried.

In overall, I really don't suggest that you use the approach presented in solution 2. In my opinion you should rely on tested mechanisms that are built in in the framework instead of coding an alternative.
CHill60 21-Jan-15 7:08am    
5'd - I've learned something today, thank you!
Wendelius 21-Jan-15 8:36am    
Glad to be of service :)
C#
foreach (Selected s1 in objlistCropped)
            {
                //here you check the value is already added in the DataTable or not.
                bool duplicate = false;
                foreach (DataRow dr in dtPreInstallation.Rows)
                {
                    if (dr["columnName"].ToString() == "value") //compare the new value with the values in the datatable
                    {
                        duplicate = true;
                        return;
                    }
                }

                if (duplicate != true)
                {
                    dtPreInstallation.Rows.Add(strImg2, s.strText);
                }
                else
                {
                    //message : Value already exists
                }
            }



try this..
 
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