none
import excel file to datagridview then insert into database sqlce RRS feed

  • Question

  • Hello Everyone,

    I have some question regarding the importing the excel file to datagridview then save or insert into database sqlce

    I'm imported already the file into datagrid but when I try to save into database is empty.

    Monday, September 23, 2013 6:55 AM

Answers

  • Hello,

    I am a bit confused about which is empty, the datagridview or database?

    So I guess that the database is empty.

    If I understand correctly, you want to know how to insert data from datagridview into database.

    And there is sample about this issue:

    class DAL //data access layer
    
        {
    
            string connString = @"yourConnectionString";
    
            SqlDataAdapter da;
    
            SqlCommandBuilder builder;
    
            public DataTable GetData()
    
            {
    
                DataTable table = new DataTable("Customers");
    
                SqlConnection conn = new SqlConnection(connString);
    
                da = new SqlDataAdapter();
    
                da.SelectCommand = new SqlCommand(@"SELECT * FROM TableName", conn);
    
                builder = new SqlCommandBuilder(da);
    
                da.Fill(table);
    
                return table;
    
            }
    
            public void UpdateData(DataTable table)
    
            {
    
                if (da != null && builder != null)
    
                {
    
                    builder.GetUpdateCommand();
    
                    da.Update(table);
    
                }
    
            }
    
    }
    
    class Form1 : Form
    
    {
    
        DAL dal;
    
        DataTable table;
    
        public Form1()
    
        {
    
            //class`s constructor
    
            dal = new DAL();
    
            table = dal.GetData();
    
            dataGridView.BindingSource = new BindingSource(table, null);
    
        }
    
        private void buttonUpdate(...)
    
        {
    
            dal.UpdateData(table);
    
        }
    
    }
    

    If I have misunderstood, please let me know.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 24, 2013 3:45 AM
    Moderator
  • It would probably help to see your code, but if you are attempting to save the Excel data source (DataTable?) to SqlCE then the RowState of each DataRow would need to be changed to "Added" using the SetAdded method.

    For Each row As DataRow In dt.Rows
     row.SetAdded()
    Next row


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, September 24, 2013 12:52 PM

All replies

  • Hi,

    could you please share some codes about your program so that the issue will be understood more clearly.

    Thanks.

    Monday, September 23, 2013 9:31 AM
  • Hello,

    I am a bit confused about which is empty, the datagridview or database?

    So I guess that the database is empty.

    If I understand correctly, you want to know how to insert data from datagridview into database.

    And there is sample about this issue:

    class DAL //data access layer
    
        {
    
            string connString = @"yourConnectionString";
    
            SqlDataAdapter da;
    
            SqlCommandBuilder builder;
    
            public DataTable GetData()
    
            {
    
                DataTable table = new DataTable("Customers");
    
                SqlConnection conn = new SqlConnection(connString);
    
                da = new SqlDataAdapter();
    
                da.SelectCommand = new SqlCommand(@"SELECT * FROM TableName", conn);
    
                builder = new SqlCommandBuilder(da);
    
                da.Fill(table);
    
                return table;
    
            }
    
            public void UpdateData(DataTable table)
    
            {
    
                if (da != null && builder != null)
    
                {
    
                    builder.GetUpdateCommand();
    
                    da.Update(table);
    
                }
    
            }
    
    }
    
    class Form1 : Form
    
    {
    
        DAL dal;
    
        DataTable table;
    
        public Form1()
    
        {
    
            //class`s constructor
    
            dal = new DAL();
    
            table = dal.GetData();
    
            dataGridView.BindingSource = new BindingSource(table, null);
    
        }
    
        private void buttonUpdate(...)
    
        {
    
            dal.UpdateData(table);
    
        }
    
    }
    

    If I have misunderstood, please let me know.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 24, 2013 3:45 AM
    Moderator
  • It would probably help to see your code, but if you are attempting to save the Excel data source (DataTable?) to SqlCE then the RowState of each DataRow would need to be changed to "Added" using the SetAdded method.

    For Each row As DataRow In dt.Rows
     row.SetAdded()
    Next row


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, September 24, 2013 12:52 PM