Rishi Chatterjee

Rishi Chatterjee

  • NA
  • 116
  • 4.7k

Validate Excel Row with Database Row

Apr 18 2020 2:12 AM
Dear All,
 I want to validate Excel Row(UserId) with Database Row(UserId), If it is match then you will insert data  other wise through message. I need to fetch all the data in excel row by User Id.
 

        private void Import1()
        {
            try
            {
                if (FileUpload1.HasFile)
                {
                    string path = Path.GetFileName(FileUpload1.FileName);
                     path = path.Replace(" ", "");
                      FileUpload1.SaveAs(Server.MapPath("~/ExcelFile/") + path);
                     String ExcelPath = Server.MapPath("~/ExcelFile/") + path;
                     OleDbConnection OleDbcon = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + ExcelPath + "; Extended Properties=Excel 8.0; Persist Security Info = False");
                    OleDbcon.Open();

                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$] ", OleDbcon);
                    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(cmd);
                    ds = new DataSet();
                    objAdapter1.Fill(ds);
                    Dt = ds.Tables[0];
                  
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void Import2( )
        {

                    SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection String"].ConnectionString);
                    con2.Open();
                    SqlCommand cnn = new SqlCommand("select * from Units Where UserId=70", con2);
                    SqlDataAdapter adapter = new SqlDataAdapter(cnn);
                
                    dt1 = new DataTable();

                    adapter.Fill(dt1);
                }
              

            }
            catch (Exception ex)
            {

            }
        }

 


        private void Check()
        {
            try
            {

                for( int i = 0; i < Dt.Rows.Count; i++)
                {
                   
                    for(int j=0; j< dt1 .Rows.Count;j++)
                {


                        if (Dt.Rows[i][0].ToString() != dt1.Rows[j][0].ToString())
                        {

                            int RowNo = i + 2;
                            Response.Write("Employee Id Not Match '" + RowNo + "'");
                            return;

                        }
                    }
                   
                }

Answers (3)