TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
YOGESH KURNE
NA
15
637
Excel sheet will upload but not store in database using Asp
Jul 15 2017 2:41 AM
Excel sheet will upload but not store in database using Asp.Net C#
i try this code
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using asthaModel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateData();
lblmessage.Text = "Current Database Data";
}
}
private void PopulateData()
{
using (asthaexcel1 ae = new asthaexcel1())
{
gvdata.DataSource = ae.information.ToList();
gvdata.DataBind();
}
}
protected void btnimport_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.ContentType == "application/vnd.ms-excel" || FileUpload1.PostedFile.ContentType == "application/vnd.openformats-officedocument.spreadsheetml.sheet")
{
try
{
string filename = Path.Combine(Server.MapPath("~/Styles"), Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.PostedFile.FileName));
FileUpload1.PostedFile.SaveAs(filename);
string constring = "";
string ext = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (ext.ToLower() == ".xls")
{
constring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source" + filename + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; ;
}
else
{
constring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source" + filename + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "Select [id],[studentname],[cource],[rollno],[password],[email],[contact] from [Sheet1$] ";
OleDbConnection con = new OleDbConnection(constring);
if (con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
OleDbCommand cmd = new OleDbCommand(query, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
da.Dispose();
con.Close();
con.Dispose();
using (asthaexcel1 ae = new asthaexcel1())
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
string id = dr["id"].ToString();
var v = ae.information.Where(a => a.id.Equals(id)).FirstOrDefault();
if (v != null)
{
v.studentname = dr["studentname"].ToString();
v.cource = dr["cource"].ToString();
v.rollno = dr["rollno"].ToString();
v.password = dr["password"].ToString();
v.email = dr["email"].ToString();
v.contact = dr["contact"].ToString();
}
else
{
ae.information.Add(new information
{
id = dr["id"].ToString(),
studentname = dr["studentname"].ToString(),
cource = dr["cource"].ToString(),
rollno = dr["rollno"].ToString(),
password = dr["password"].ToString(),
email = dr["email"].ToString(),
contact=dr["contact"].ToString()
});
}
}
ae.SaveChanges();
}
PopulateData();
lblmessage.Text = "Successfully data imported";
}
catch (Exception)
{
throw;
}
}
}
}
error is occur in
ae.information.Add(new information
{
these pahse
please reply i really thankfull to you......!
Reply
Answers (
3
)
Help with parameters passing a column to a MySql query
How to Back Up MySQL Databases From The Command Line