Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what i write inside loop to display similar item code on datagridview and insert different itemcode

Status
Not open for further replies.

engsalah2018

Technical User
Sep 10, 2018
5
EG
Problem

SQL Server Database(2014) Items Table

ItemCode(pk) ItemName

001 mouse

002 keyboard

003 Headphone

On File Excel sheet 2010

ItemCode ItemName

001 mouse

002 keyboard

004 screen

005 Ram

Actually i need when import excel file insert different items code that not exist

on sql server database and Exist Items On Database and Found on Excel not insert but display on datagridview .

according to my case insert itemcodes 004,005 on table Items.

and show 001,002 in grid view as exist items .

my function as below

my code (Inside Loop)
Code:
public static void ImportFromExcelToDataBase()
    {
       Datatable dt = ShowExcelData();
       DataTable dtItems = GetSqlItems();
       
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //what i write here 
                // if itemcode exist on excel exist on sql server database
                then display similar items exist on database and excel as 001,002 on datagridview
                //else 
                 // do insert data
                string Insert = "Insert Into Values (" + data + ")";
                DataAccess.ExecuteNonQuery(Insert);
            }
               
    }
 public DataTable ShowExcelData()
        {
            string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);

            OleDbConnection con = new OleDbConnection(connectionString);


            con.Open();
            DataTable dt = new DataTable();

            dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();


            OleDbCommand com = new OleDbCommand();
            com.Connection = con;
           
            com.CommandText = @"SELECT  [ItemCode],[ItemsName],[ItemAddress] FROM  [" + SheetName + "] ";
            OleDbDataAdapter oledbda = new OleDbDataAdapter();
            oledbda.SelectCommand = com;
            DataSet ds = new DataSet();
            oledbda.Fill(ds);
            dt = ds.Tables[0];
            con.Close();
            return dt;


        }
dt = ShowExcelData();

 public DataTable GetSqlItems()
        {
            string GetItems = @"select ItemCode,ItemsName,ItemAddress from Items";


           DataTable tbGetItems = DataAccess.ExecuteDataTable(GetItems );
            return tbGetItems ;
        }
dtItems = GetSqlItems();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top