×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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 --> c#

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(); 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close