VB.NET - vb.net with excel upload

Asked By kiruba .e on 22-Jun-12 07:45 AM
Earn up to 20 extra points for answering this tough question.
Hi,

I got this error, while uploading excel sheet in access database.  First i get data from excel and assign it to datagridview.

Dim excelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + TxtFileName.Text + ";Extended Properties='Excel 4.0;HDR=YES;'"
        Dim xlsConn As OleDb.OleDbConnection = New OleDb.OleDbConnection(excelConnectionString)
        Dim command As OleDb.OleDbCommand = New OleDb.OleDbCommand("select * from [Sheet1$] ", xlsConn)
        Dim daexcel As OleDbDataAdapter
        daexcel = New OleDbDataAdapter
        daexcel.SelectCommand = command
        Dim dsexcel As New DataSet
        xlsConn.Open()
        daexcel.Fill(dsexcel) '----------------------------------------->In this line i got "The connection for viewing your linked Microsoft Excel worksheet was lost."
        Grdbirthday.DataSource = dsexcel.Tables(0)
        xlsConn.Close()

How to rectify it?

Regards
Kiruba.e
dipa ahuja replied to kiruba .e on 22-Jun-12 08:18 AM
Try this code : private void button2_Click(object sender, EventArgs e)
{
  string ExcelConstr = @"Provider=Microsoft.ACE.OLEDB.12.0";
  ExcelConstr += "Data Source=d:\book1.xls;Extended Properties=Excel 12.0";
 
  string SqlConstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;";
  SqlConstr += "Integrated Security=True;User Instance=True";
 
  using (OleDbConnection con = new OleDbConnection(ExcelConstr))
  {
    con.Open();
    OleDbCommand com = new OleDbCommand("Select * from [Sheet1$]", con);
    OleDbDataReader dr = com.ExecuteReader();
    using (SqlConnection sqlcon = new SqlConnection(SqlConstr))
    {
      sqlcon.Open();
      using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
      {
        bulkCopy.DestinationTableName = "Table1";
        bulkCopy.ColumnMappings.Add("srno", "srno");
        bulkCopy.ColumnMappings.Add("amount", "amount");
        bulkCopy.WriteToServer(dr);
      }
    }
    dr.Close();
    dr.Dispose();
  }
  MessageBox.Show("successfully imported!");
  //display the imported data in the datagrid
  SqlDataAdapter da = new SqlDataAdapter("select * from Table1", SqlConstr);
  DataTable dt = new DataTable();
 
  da.Fill(dt);
 
  dataGridViewX1.DataSource = dt;
}
 
wally eye replied to kiruba .e on 22-Jun-12 10:29 AM
Have you verified that you have an active connection?  I assume TxtFileName.text has the right path in it, have you verified that?
[)ia6l0 iii replied to kiruba .e on 22-Jun-12 12:47 PM
First thing for you to do is to check if the file that you provided in "TxtFileName.Text" is Open. The file that is being read by the Oledb connection, should not be open. 

And remember to close all instances of Excel via the task manager, just to make sure that this is not an old cause. Please pass the excel file instance to this method to effectively dispose it. 

Private Sub NAR(o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
Finally
o = Nothing
End Try
End Sub


Hope this helps.
Jitendra Faye replied to kiruba .e on 25-Jun-12 02:22 AM

Using sqlBulk Class you can export excel file to DataBase.

Use this code-


protected void btnSend_Click(object sender, EventArgs e)
{
String strConnection = "Data Source=MySystem;Initial Catalog=MySamplesDB;Integrated Security=True";

//file upload path
string path = fileuploadExcel.PostedFile.FileName;

//Create connection string to Excel work book
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";

//Create Connection to Excel work book
OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);

//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation] from [Sheet1$]",excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "Excel_table";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
}

Try this code and let me know.

 

help
excel file ASP.NET 28-Oct-12 07:42 PM hi good morning everybody how to export a excel file to the sql server using asp.net page for that are the excel table and sql table same column and row ? pls help me thankyou you can importing excel into sql server using SqlBulkCopy protected void Button1_Click( object sender, EventArgs e) { / / Create connection string to Excel work book string excelConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C: \ Details.xls; Extended Properties = " "Excel 8.0;HDR = YES;" "" ; / / Create Connection to Excel work book OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); / / Create OleDbCommand to fetch data from Excel OleDbCommand cmd = new OleDbCommand
hi frds Excel created using in Table format How to get data from this excel sheet pls help Thanks in advance Use simply this code to get data from excel sheet- / / Create connection string to Excel work book string excelConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0;Data Source = " + path + ";Extended Properties = Excel 12.0;Persist Security Info = False"; / / Create Connection to Excel work book OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); / / Create OleDbCommand to fetch data from Excel OleDbCommand cmd = new OleDbCommand
Hola Tengo datos en un DataGridView, esos datos los quiero pasar a un archivo de Excel 2003 y a Excel 2007, ¿Como le hago para lograr esto con una conexion OleDB? VB.NET - Spanish Discussions OleDbConnection (1) OleDbCommand (1) Excel 2003 (1) Excel 2007 (1) ExecuteNonQuery (1) DataTable (1) VB (1) Xml (1) Hola: Si el control DataGridView
hi, Is it possible to import excel sheet into sql database if so than please give me few examples thanks HI Just a little bit of code transfers the data from the Excel Spreadsheet into the SQL Server Database Table: / / Connection String to Excel Workbook string excelConnectionString = @" Provider = Microsoft .Jet.OLEDB.4.0;Data Source = Book1.xls;Extended Properties = ""Excel 8.0;HDR = YES;"" " ; / / Create Connection to Excel Workbook using (OleDbConnection connection = new OleDbConnection(excelConnectionString)) { OleDbCommand command = new OleDbCommand ( " Select ID, Data FROM [Data$] " , connection); connection.Open(); / / Create DbDataReader
I have written this code to insert Excel Data to SQL Server. But when I insert same excel file adding One More Row to Excel file it shows Primary Key violation. protected void Button1_Click( object sender, EventArgs e) { if (FileUpload1 FileName.Trim() = = "" ) { lblMessage.Text = "Upload valid excel file" ; } else { string path = FileUpload1.PostedFile.FileName; string excelConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " +path+ ";Extended Properties = Excel 8.0" ; OleDbConnection excelConnection = new OleDbConnection (excelConnectionString); try { OleDbCommand cmd = new OleDbCommand ( "select ID, Name, Sex from [Sheet1
save the excel sheet into database ASP.NET 28-Oct-12 07:47 PM Am getting error while am saving a excel sheet into database. "External table is not in the expected format." string strpath = @"Provider = Microsoft Jet.OleDb.4.0; data source = " + path + ";Extended Properties = Excel 8.0;"; OleDbConnection con = new OleDbConnection(strpath); OleDbCommand cmd = new OleDbCommand("select *from [sheet1$]", con); con.Open(); OleDbDataReader dr = cmd.ExecuteReader(); I wrote the above code me reply You can do this by using Class. just use below code to send excel data to sql server. . . . . Design your aspx page like this <html xmlns = "http: / / www.w3
how to export excel sheet into Access using c# C# .NET 28-Oct-12 07:44 PM hell sir 1 button . . if u click the button means the datagrid table is save into the excel sheet . . . know that excel sheet is convert into MS access please say how to convert and send the example for this thanks Bhanuprakash Hi, try like this using System.Data; using System.Data.OleDb; OleDbConnection con = new OleDbConnection(@ "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = C: \ Book1.xls;Extended Properties = Excel 8.0" ); OleDbDataAdapter da = new OleDbDataAdapter( "select * from MyObject" , con); DataTable dt = new DataTable(); da Read the DataTable Item and Insert it One by One into Database using Insert Query * / oledbConnection con = new oledbConnection( "ConnectionStr" ); con.Open(); foreach (DataRow r in dt.Rows) { / * Inset Logic * / oledbCommand comm = new oledbCommand
Hi, I seem to have found a bug for which there seems not to be constring = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = d: \ inetpub \ test.xlsx;Extended Properties = \ "Excel 12.0 Xml;HDR = YES; \ ""; / / string constring = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = d: \ inetpub \ test.xls;Extended Properties = \ "Excel 8.0;HDR = Yes; \ ""; OleDbConnection oleDBConnection = new OleDbConnection(constring oleDBConnection.Open(); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(); try { string createTable = "CREATE TABLE [Export] (test1 LONGTEXT, test2 LONGTEXT
I want to fill data in to text boxes from excel sheet. And I want to write results and data to excel sheet. How to write this? HI read an excel using System; using System.IO; using System.Reflection; using NUnit.Framework; using ExcelTools = Ms.Office; using Excel = Microsoft.Office.Interop.Excel; namespace Tests { [TestFixture] public class ExcelSingle { [Test] public void ProcessWorkbook() { string file = @"C: \ Users \ Chris \ Desktop \ TestSheet.xls"; Console.WriteLine(file); Excel.Application excel = null; Excel.Workbook wkb = null; try { excel = new Excel.Application(); wkb = ExcelTools.OfficeUtil
Reading Excel C# .NET 28-Oct-12 07:44 PM I have an excel which has two worksheets. the excel is in xlsx format. I am reading this excel using the following connection string string xConnStr = "Provider = Microsoft.ACE.OLEDB.12.0;" + "Data Source filename + ";" + "Extended Properties = \ "Excel 8.0;IMEX = 1;HDR = NO; \ ""; and having the two worksheet values in two different has 20 rows and Worksheet2 datatable has 10 rows. When i am converting the ablove excel to .xls format and then try to read with the same connectionstring, Worksheet1 datatable has Why is there a difference in reading only one worksheet? Hi you can read the Excel File like this protected void Page_Load(Object Src, EventArgs E) { string strConn; strConn = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = C: \ exceltest.xls;" + "Extended Properties = Excel 8.0;" ; / / You must use the $ after the object you reference in the spreadsheet OleDbDataAdapter