protected void Button2_Click(object sender, EventArgs e)
{
//SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\projects\importexcelfile\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
//SqlCommand cmd = new SqlCommand("insert into demo values '" + +"'", con);
string ConStr = "";
string ext = Path.GetExtension(FileUpload1.FileName).ToLower();
string path = Server.MapPath("~/excel/"+FileUpload1.FileName);
FileUpload1.SaveAs(path);
Label1.Text = FileUpload1.FileName ;
if (ext.Trim() == ".xls")
{
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (ext.Trim() == ".xlsx")
{
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet1$]";
OleDbConnection conn = new OleDbConnection(ConStr);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
insert ////
protected void Button3_Click1(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
if (g1.Cells[1].Text == "rr")
{
string connStr1 = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\projects\importexcelfile\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
SqlConnection con1 = new SqlConnection(connStr1);
SqlCommand cmd1;
cmd1 = new SqlCommand("insert into demo2(id,name,mobile,address) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con1);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
}
else
{
string connStr = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\projects\importexcelfile\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd;
cmd = new SqlCommand("insert into demo(id,name,mobile,address) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Label1.Text = "Records inserted successfully";
}
insert ////
protected void Button3_Click1(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
if (g1.Cells[1].Text == "rr")
{
string connStr1 = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\projects\importexcelfile\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
SqlConnection con1 = new SqlConnection(connStr1);
SqlCommand cmd1;
cmd1 = new SqlCommand("insert into demo2(id,name,mobile,address) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con1);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
}
else
{
string connStr = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\projects\importexcelfile\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd;
cmd = new SqlCommand("insert into demo(id,name,mobile,address) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Label1.Text = "Records inserted successfully";
}
No comments:
Post a Comment