locked
Auto Form Filling How to get data from Excel sheet (one by one) row values RRS feed

  • Question

  • User802905806 posted

    Hi., 

             Auto Form Filling      

        I created one simple windows application it contains 5 Enteries(Textbox). I like to get data(one by one) from excel with out using manual typing and also stored in to MS-Access Database. 

    So any one please let me know.

    Regards,

    Test

    Monday, July 8, 2013 7:31 AM

All replies

  • User516094431 posted

    int rows = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
    string connString = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
    // Create the connection object
    OleDbConnection oledbConn = new OleDbConnection(connString);
    try
    {
    // Open connection
    oledbConn.Open();

    // Create OleDbCommand object and select data from worksheet Sheet1
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);

    // Create new OleDbDataAdapter
    OleDbDataAdapter oleda = new OleDbDataAdapter();

    oleda.SelectCommand = cmd;

    // Create a DataSet which will hold the data extracted from the worksheet.
    DataSet ds = new DataSet();

    // Fill the DataSet from the data extracted from the worksheet.
    oleda.Fill(ds, "Employees");
    if(ds.Table[0].Rows.Count > 0)
    {
    txtbox1.Text = ds.Table[0].Row[rows][0];
    txtbox2.Text = ds.Table[0].Row[rows][1];
    txtbox3.Text = ds.Table[0].Row[rows][2];
    txtbox4.Text = ds.Table[0].Row[rows][3];
    txtbox5.Text = ds.Table[0].Row[rows][4];
    rows ++;
    }
    }
    catch
    {
    }
    finally
    {
    // Close connection
    oledbConn.Close();
    }

    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
    insert data into MS see below link:
    http://social.msdn.microsoft.com/Forums/en-US/aa1e768b-b3bb-4a99-8902-4739ce1c9c82/inserting-data-into-ms-access-using-c
    if(ds.Table[0].Rows.Count > 0)
    {
    txtbox1.Text = ds.Table[0].Row[rows][0];
    txtbox2.Text = ds.Table[0].Row[rows][1];
    txtbox3.Text = ds.Table[0].Row[rows][2];
    txtbox4.Text = ds.Table[0].Row[rows][3];
    txtbox5.Text = ds.Table[0].Row[rows][4];
    rows ++;
    }
    }

    Monday, July 8, 2013 7:42 AM