Tuesday 14 October 2008

Uploading Spread into ASp.Net and inserting Data in a spread sheet into Data BAse

Uploading a file using FileUploader


String Name = FileUpload1.FileName.ToString();
if (FileUpload1.PostedFile != null)
{
fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
SaveLocation = Server.MapPath(fn);
FileUpload1.PostedFile.SaveAs(SaveLocation);
}

Getting connection to Spread Sheet and SQL to get the data from spread sheet and insert into SQl TABLE(Table NAme: Photo Finding)


String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath(fn) + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection con = new OleDbConnection(sConnectionString);
SqlConnection con1 = new SqlConnection("data source=DCSQL2;initial catalog=gGilesDW;integrated security=SSPI;persist security info=False;Trusted_Connection=Yes");
OleDbDataReader oledbrdr = null;
try
{
con.Open();
// Create OleDbCommand object and select data from worksheet Sheet1
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", con);
oledbrdr = cmd.ExecuteReader();
con1.Open();
while (oledbrdr.Read())
{
String Status = Convert.ToString(oledbrdr["Status"]);
String id = Convert.ToString(oledbrdr["ID"]);
query = "INSERT INTO PhotoFinding" + "(Status,[ID]) " + " VALUES ('" + Status + "','" + id + "')";
SqlCommand sqlcmd = new SqlCommand(query, con1);
i = sqlcmd.ExecuteNonQuery();
}
if (i == 1)
{
Label2.Text = "Hai Data Base is Updated Sucessfully";
}
else
{
Label2.Text = "The Process of Updating Failed";
}
}

No comments:

Free Hit Counter