Click here to Skip to main content
15,887,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am building an fileupload app and have got one challenge holding me back. So far, the app allows users upload and excel file using the openfiledialog control, and then they can preview the data in a datagridview before sending it to the database.

The issue is I need to confirm that the values in the cells of two columns in the datagrid already exists in another table in the my database before they can submit the datagridview to the database.

To help understand my issue, lets assume I have DatagridView1 and I need to confirm that the Values in cells of Column1 and Column2 already exist in TableProducts before sending the DatagridView1 to the database.

Any help or ideas will be appreciated.
Posted
Comments
Jawad Ahmed Tanoli 22-Nov-13 11:21am    
is it hudge amount of data in gridiew???
Uzoma Umekwe 22-Nov-13 11:30am    
yes about 10,000 records
Jawad Ahmed Tanoli 22-Nov-13 14:12pm    
how your loading data from excel to gridview?and what other action your doing on gridview before sending to database just want to check the two cell values to check wether exsist in database table?
Uzoma Umekwe 25-Nov-13 10:42am    
Yes. I just want to check if the values in the cells of two columns exists in a table in the database before sending to database

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace console_poc
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Entity> lstDBData = new List<Entity>();
            // fetch all the db data from that table which u want to compare and store it in a list
            // or a datatable for LINQ i have used List<T>


            List<Entity> lstUploadData = new List<Entity>();
            // store the excel upload data in this list

            List<Entity> lstDataWhichExistsInDb = new List<Entity>();
            // this list will store the data which is available in the db

            List<Entity> lstDataWhichDoesNotExistsInDb = new List<Entity>();
            // this list will store the data which is not available in the db

            // processs..
            // storing the existing db data in one list and new value in another list
            lstUploadData.ForEach(k =>
                {
                    var item = lstDBData.FirstOrDefault(l => l.Column1 == k.Column1 && l.Column2 == k.Column2);
                    if (item == null)
                        lstDataWhichDoesNotExistsInDb.Add(k);
                    else
                        lstDataWhichExistsInDb.Add(k);
                });

            // now based on ur requirement u can use which list u want..


        }
    }
    public class Entity
    {
        public string Column1 { get; set; }
        public string Column2 { get; set; }
    }
}
 
Share this answer
 
v2
its simple, just get the 2 values you want to check then try this

Datarow[] Dr = Datatable1.Select(" Name='Value 1' or Name='Value2'");//replace "Value 1" & "Vaue 2" with your value
if(Dr.Length > 0)
{
    //Means Record Exists
}
else
{
    //Else Not
}
 
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