Click here to Skip to main content
15,896,118 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: designing of ASP.NET project. Pin
eyeseetee21-Oct-08 23:09
eyeseetee21-Oct-08 23:09 
AnswerRe: designing of ASP.NET project. Pin
Brij21-Oct-08 23:38
mentorBrij21-Oct-08 23:38 
AnswerRe: designing of ASP.NET project. Pin
N a v a n e e t h22-Oct-08 2:06
N a v a n e e t h22-Oct-08 2:06 
QuestionFindControl Problem Pin
Sneha Bisht21-Oct-08 22:11
Sneha Bisht21-Oct-08 22:11 
AnswerRe: FindControl Problem Pin
_AK_21-Oct-08 22:23
_AK_21-Oct-08 22:23 
GeneralRe: FindControl Problem Pin
Sneha Bisht21-Oct-08 22:34
Sneha Bisht21-Oct-08 22:34 
GeneralRe: FindControl Problem Pin
_AK_21-Oct-08 22:37
_AK_21-Oct-08 22:37 
QuestionAuto format an Excel File and import it into Sql Server 2000 [modified] Pin
Nitin Raj21-Oct-08 22:04
Nitin Raj21-Oct-08 22:04 
hi,

I am trying to autoformat an Excel (.xls) file and then import it into Sql server 2000 table using C# in Asp.net. But when i try to autoformat the excel file i get the exception "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Someone please help me to solve this problem

Here is the code.

sing System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Data.OleDb;

using System.Data.Odbc;

using System.Text;

using Microsoft.Office.Interop.Excel;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

SqlConnection Cn = new SqlConnection("server=193.14.45.137; database= NitDB; User ID=re; Password= g5");

SqlCommand Cm = new SqlCommand("Select * from ExcelFile", Cn);

Cn.Open();

SqlDataReader St = Cm.ExecuteReader();

gdExcelFile.DataSource = St;

gdExcelFile.DataBind();

St.Close();

Cn.Close();

}

public static void PrintProgressBar()

{

StringBuilder sb = new StringBuilder();

sb.Append("<div id='updiv' style='Font-weight:bold;font-size:11pt;Left:320px;COLOR:black;font-family:verdana;Position:absolute;Top:140px;Text-Align:center;'>");

sb.Append("&nbsp;<script> var up_div=document.getElementById('updiv');up_div.innerText='';</script>");

sb.Append("<script language=javascript>");

sb.Append("var dts=0; var dtmax=10;");

sb.Append("function ShowWait(){var output;output='Reading data from excel...PLEASE WAIT!';dts++;if(dts>=dtmax)dts=1;");

sb.Append("for(var x=0;x < dts; x++){output+='';}up_div.innerText=output;up_div.style.color='red';}");

sb.Append("function StartShowWait(){up_div.style.visibility='visible';ShowWait();window.setInterval('ShowWait()',100);}");

sb.Append("StartShowWait();</script>");

HttpContext.Current.Response.Write(sb.ToString());

HttpContext.Current.Response.Flush();

}

public static void ClearProgressBar()

{

StringBuilder sbc = new StringBuilder();

sbc.Append("<script language='javascript'>");

sbc.Append("alert('Data exported successfully');");

sbc.Append("up_div.style.visibility='hidden';");

sbc.Append("history.go(-1)");

sbc.Append("</script>");

HttpContext.Current.Response.Write(sbc);

}

protected void btnExport_Click(object sender, EventArgs e)

{



PrintProgressBar();

{

OdbcConnection connection;

SqlBulkCopy bulkCopy;

string ConnectionString = @"server=323.65.43.134;database= NitDB; User ID=yu; Password=g0";

string connstr = @"Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + fyle.Value;

using (connection = new OdbcConnection(connstr))

{

string[] temp;

string fn1;

fn1 = System.IO.Path.GetFileName(fyle.PostedFile.FileName); \\ fyle is the Html File upload control

temp = fn1.Split('.');

object missing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Excel.Application excel = default(Microsoft.Office.Interop.Excel.Application);

Microsoft.Office.Interop.Excel.Workbook wb = default(Microsoft.Office.Interop.Excel.Workbook);

excel = new Microsoft.Office.Interop.Excel.Application();

// I get the exception at the below statement.

wb = excel.Workbooks.Open(fn1, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);

excel.Visible = true;

//wb.Activate();

excel.Cells.Select();

excel.Columns.AutoFit();

excel.Rows.AutoFit();



OdbcCommand command = new OdbcCommand("Select * FROM [" + temp[0].ToString() + "$]", connection);

connection.Open();

using (OdbcDataReader dr = command.ExecuteReader())

{

using (bulkCopy = new SqlBulkCopy(ConnectionString))

{

bulkCopy.DestinationTableName = "ImportedExcel";

bulkCopy.WriteToServer(dr);

}

dr.Close();

}

}

bulkCopy.Close();

connection.Close();

}

ClearProgressBar();

}


}

Nitin Raj Bidkikar

modified on Wednesday, October 22, 2008 9:11 AM

QuestionA Session Question Pin
Xiaoming Qian21-Oct-08 21:45
Xiaoming Qian21-Oct-08 21:45 
AnswerRe: A Session Question Pin
eyeseetee21-Oct-08 22:08
eyeseetee21-Oct-08 22:08 
QuestionUsing RFID in asp.net Pin
mukesh.mr0321-Oct-08 21:29
mukesh.mr0321-Oct-08 21:29 
Questionapplication variables question! Pin
mr_muskurahat21-Oct-08 20:39
mr_muskurahat21-Oct-08 20:39 
AnswerRe: application variables question! Pin
Sandeep Akhare21-Oct-08 20:43
Sandeep Akhare21-Oct-08 20:43 
GeneralRe: application variables question! Pin
mr_muskurahat21-Oct-08 20:53
mr_muskurahat21-Oct-08 20:53 
GeneralRe: application variables question! Pin
Sandeep Akhare21-Oct-08 21:07
Sandeep Akhare21-Oct-08 21:07 
GeneralRe: application variables question! Pin
mr_muskurahat22-Oct-08 0:41
mr_muskurahat22-Oct-08 0:41 
GeneralRe: application variables question! Pin
Sandeep Akhare22-Oct-08 1:20
Sandeep Akhare22-Oct-08 1:20 
GeneralRe: application variables question! Pin
mr_muskurahat22-Oct-08 2:22
mr_muskurahat22-Oct-08 2:22 
GeneralRe: application variables question! Pin
mr_muskurahat22-Oct-08 0:44
mr_muskurahat22-Oct-08 0:44 
GeneralRe: application variables question! Pin
Sandeep Akhare22-Oct-08 1:22
Sandeep Akhare22-Oct-08 1:22 
GeneralRe: application variables question! Pin
N a v a n e e t h22-Oct-08 2:09
N a v a n e e t h22-Oct-08 2:09 
GeneralRe: application variables question! Pin
Sandeep Akhare22-Oct-08 3:37
Sandeep Akhare22-Oct-08 3:37 
GeneralRe: application variables question! Pin
N a v a n e e t h22-Oct-08 17:30
N a v a n e e t h22-Oct-08 17:30 
GeneralRe: application variables question! [modified] Pin
Sandeep Akhare22-Oct-08 21:11
Sandeep Akhare22-Oct-08 21:11 
GeneralRe: application variables question! Pin
mr_muskurahat22-Oct-08 18:21
mr_muskurahat22-Oct-08 18:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.