//"To run this code errorfree please change/set component name what you have given at design time."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb; //Add this namepace to get object for OLEDB
public partial class addtoexcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void clearall()
{
txtnm.Text = "";
txtadd.Text = "";
txtcity.Text = "";
txtphone.Text = "";
txtemail.Text = "";
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
//Excel Connection string
//string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\\Users\\Administrator\\Desktop\ \Excel data.xlsx;Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\"";
//Access Connection string
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;; Data Source = C:\\Users\\Administrator\\Desktop\ \Acces sdata.accdb; Persist Security Info=False;";
//Excel Querystring
//string query = "Insert into [sheetname$] (Name, Address, City, Phone, EmailID) VALUES ('" + txtnm.T ext + "','" + txtadd.Text + "','" + txtcity.Text + "','" + txtphone.Text + "','" + txtemail.Text + "')";
//Access Querystring
string query = "Insert into youraccesstablename (Name, Address, City, Phone, EmailID) VALUES ('" + tx tnm.Text + "','" + txtadd.Text + "','" + txtcity.Text + "','" + txtphone.Text + "','" + txtemail.Text + "')";
OleDbConnection con = new OleDbConnection(constr);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
OleDbCommand cmd = new OleDbCommand(query,con);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
Response.Write("<script>alert('Data Inserted Successfully.')</script>");
}
else
{
Response.Write("<script>alert('Daata not Inserted successfully.')</script>");
}
con.Close();
clearall();
}
}