Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Dear All.
Can anyone help with a code to import from excel to sql using progress bar, where it update the label with the items being imported, If item exist, it should display in label.
My previous post shows sample of code i used for the copy but i seem to be lost as to how to use the progressbar in it

C#
private void openFileFile()
{
    string name;
    string model;
    string serial;
    string serial2;
    string windowsuser = txtuserid.Text;
    string createdate = txtcreated.Text;
    string cost = txtcost.Text;
    string quantity = txtquantity.Text;
    string active = "1";
    string filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "product.xls");
    bool flag = true;
    Econ = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0");
    try
    {
        OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", Econ);
        Econ.Open();
        OleDbDataReader odr = ocmd.ExecuteReader();
        if (odr.HasRows)
        {
            while (odr.Read())
            {
                name = odr.GetValue(0).ToString();
                model = odr.GetValue(1).ToString();
                serial = odr.GetValue(2).ToString();
                serial2 = odr.GetValue(3).ToString();
                importexcel(name, model, serial, serial2, windowsuser, createdate, cost, quantity, active);
            }
            flag = false;
        }
        else
        {
            lblstat.Text= "Object reference not set.Empty worksheet.";
            timer.Stop();
            //MessageBox.Show("Object reference not set.\nError: Empty worksheet.", "Inventory");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

public void importexcel(string name, string model, string serial, string serial2, string windowsuser, string createdate, string cost, string quantity, string active)
{
    con = new SqlConnection(connection);
    string seek = "select * from productx where serial='" + serial + "'";
    SqlCommand cmd2 = con.CreateCommand();
    cmd2.CommandText = seek;
    con.Open();
    SqlDataReader dr;
    dr = cmd2.ExecuteReader();
    if (dr.HasRows)
    {
        while (dr.Read())
        {
            lblstat.Text = "Error importing duplicate serial number: " + serial;
            //MessageBox.Show("Error importing duplicate serial number:" + "\n" + serial, "Inventory");
        }
    }
    else
    {
        try
        {
            con.Close();
            string insertexcel = "insert into productx(name, model, serial, serial2,windowsuser,Ondate,cost,quantity,status)values(@name, @model, @serial, @serial2,@wuser,@date,@cost,@quantity,@stat)";
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = insertexcel;
            cmd.Parameters.AddWithValue("@name", SqlDbType.NVarChar).Value = name;
            cmd.Parameters.AddWithValue("@model", SqlDbType.NVarChar).Value = model;
            cmd.Parameters.AddWithValue("@serial", SqlDbType.NVarChar).Value = serial;
            cmd.Parameters.AddWithValue("@serial2", SqlDbType.NVarChar).Value = serial2;
            cmd.Parameters.AddWithValue("@wuser", SqlDbType.NVarChar).Value = windowsuser;
            cmd.Parameters.AddWithValue("@date", SqlDbType.NVarChar).Value = createdate;
            cmd.Parameters.AddWithValue("@cost", SqlDbType.NVarChar).Value = cost;
            cmd.Parameters.AddWithValue("@quantity", SqlDbType.NVarChar).Value = quantity;
            cmd.Parameters.AddWithValue("@stat", SqlDbType.NVarChar).Value = active;
            cmd.CommandType = Com
Posted
Updated 6-Sep-15 12:41pm
v2
Comments
[no name] 5-Sep-15 9:40am    
And what is the Problem?
1. You Import from Excel, so I assume you can determine how much rows in advance.
2. Set your Progress bar to no of rows.
3. Step the Progress bar for each row you read.

In case you are using another technics which allows you to Import Excel "more directly" and this technics does not Support something like "Progress callback", yep then you are lost ;)
famosajr 6-Sep-15 1:50am    
Here is the code i use for import. I want the label name lblstat to be updated with each progress, whether error or successful.
private void openFileFile()
{
string name;
string model;
string serial;
string serial2;
string windowsuser = txtuserid.Text;
string createdate = txtcreated.Text;
string cost = txtcost.Text;
string quantity = txtquantity.Text;
string active = "1";
string filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "product.xls");
bool flag = true;
Econ = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0");
try
{
OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", Econ);
Econ.Open();
OleDbDataReader odr = ocmd.ExecuteReader();
if (odr.HasRows)
{
while (odr.Read())
{
name = odr.GetValue(0).ToString();
model = odr.GetValue(1).ToString();
serial = odr.GetValue(2).ToString();
serial2 = odr.GetValue(3).ToString();
importexcel(name, model, serial, serial2, windowsuser, createdate, cost, quantity, active);
}
flag = false;
}
else
{
lblstat.Text= "Object reference not set.Empty worksheet.";
timer.Stop();
//MessageBox.Show("Object reference not set.\nError: Empty worksheet.", "Inventory");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
public void importexcel(string name, string model, string serial, string serial2, string windowsuser, string createdate, string cost, string quantity, string active)
{
con = new SqlConnection(connection);
string seek = "select * from productx where serial='" + serial + "'";
SqlCommand cmd2 = con.CreateCommand();
cmd2.CommandText = seek;
con.Open();
SqlDataReader dr;
dr = cmd2.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
lblstat.Text = "Error importing duplicate serial number: " + serial;
//MessageBox.Show("Error importing duplicate serial number:" + "\n" + serial, "Inventory");
}
}
else
{
try
{
con.Close();
string insertexcel = "insert into productx(name, model, serial, serial2,windowsuser,Ondate,cost,quantity,status)values(@name, @model, @serial, @serial2,@wuser,@date,@cost,@quantity,@stat)";
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = insertexcel;
cmd.Parameters.AddWithValue("@name", SqlDbType.NVarChar).Value = name;
cmd.Parameters.AddWithValue("@model", SqlDbType.NVarChar).Value = model;
cmd.Parameters.AddWithValue("@serial", SqlDbType.NVarChar).Value = serial;
cmd.Parameters.AddWithValue("@serial2", SqlDbType.NVarChar).Value = serial2;
cmd.Parameters.AddWithValue("@wuser", SqlDbType.NVarChar).Value = windowsuser;
cmd.Parameters.AddWithValue("@date", SqlDbType.NVarChar).Value = createdate;
cmd.Parameters.AddWithValue("@cost", SqlDbType.NVarChar).Value = cost;
cmd.Parameters.AddWithValue("@quantity", SqlDbType.NVarChar).Value = quantity;
cmd.Parameters.AddWithValue("@stat", SqlDbType.NVarChar).Value = active;
cmd.CommandType = Com
Patrice T 6-Sep-15 18:42pm    
I updated you question with your code but the end is missing.

1 solution

Hi,

It seems you don't follow what msdn says.

Try this sample code

C#
using System.Data;
using System.Data.SqlClient;
public class A {
   public static void Main() {
      using (SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;")) {
         connection.Open();
         SqlCommand command= connection.CreateCommand();
         command.CommandText = "SELECT * FROM Categories ORDER BY CategoryID";
         command.CommandTimeout = 15;
         command.CommandType = CommandType.Text;
      }
   }
}



Your code instanciates a CreateCommand() before calling Open() method from SqlConnection.
That's what msdn does not advise.

And the sample code you gave does not show a progress bar.
 
Share this answer
 
v2

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