.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Insert records into excel through textboxes

Posted By:satyapriyanayak       Posted Date: May 03, 2014    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

In this Code Snippets we will know how to Insert records into excel through textboxes
 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

namespace Insert_data_into_excel_asp
{
    public partial class _Default : System.Web.UI.Page
    {
        OleDbConnection con;
        OleDbCommand com;
        string str;

        protected void btn_save_Click(object sender, EventArgs e)
        {
            try
            {

                com = new OleDbCommand();
                con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='c:\\file\\test.xlsx';Extended Properties=Excel 8.0;");
                con.Open();
                com.Connection = con;
                str = "Insert into [Sheet1$] (EID,EName,Designation) values('" + txt_id.Text + "','" + txt_name.Text + "','" + txt_designation.Text + "')";
                com.CommandText = str;
                com.ExecuteNonQuery();
                con.Close();
                Response.Write("Records inserted successfully");
                clear();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void clear()
        {
            txt_id.Text = "";
            txt_name.Text = "";
            txt_designation.Text = "";
        }
       
    }
}

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend