|
-
Oct 19th, 2009, 10:20 PM
#1
Thread Starter
New Member
[RESOLVED] What's wrong with my code??
Hi..
Im trying to upload an excel file and generate it in datagrid. When I clicked the button generate, nothing has been populated. Can you take a look at my code and check it please..
vb.net Code:
Imports System.Data
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
If fu_Load.HasFile Then
LoadSheets()
End If
End Sub
Private Sub LoadSheets()
Dim Con As New OleDb.OleDbConnection
Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fu_Load.HasFile & ";Extended Properties=""Excel 8.0;HDR=No;"""
Try
Con.Open()
Dim DT As New DataTable("Sheets")
DT = Con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
' Clear controls in placeholder
PH.Controls.Clear()
' Add controls
For Each Row As DataRow In DT.Rows
Dim Control As DGControl = LoadControl("DGControl.ascx")
' Set Name
Control.TabName = Row.Item("Table_Name")
' Set Data
Control.Data = Get_ExcelData(Con, Row.Item("Table_Name"))
PH.Controls.Add(Control)
PH.Controls.Add(New LiteralControl("<br />"))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Function Get_ExcelData(ByVal Con As OleDb.OleDbConnection, ByVal SheetName As String) As DataTable
Dim DT As New DataTable("Data")
Dim DA As New OleDbDataAdapter("Select * from [" & SheetName & "]", Con)
DA.Fill(DT)
Return DT
End Function
End Class
Thanks alot!
---Jhimz
Last edited by Hack; Oct 20th, 2009 at 06:54 AM.
Reason: Added Highlight Tags
-
Oct 20th, 2009, 12:44 AM
#2
Re: What's wrong with my code?? (Move thread to Asp.net Forum)
Everything looks good expect this one
PHP Code:
Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & fu_Load.HasFile & ";Extended Properties=""Excel 8.0;HDR=No;"""
I doubt you are using the FileUpload Component and you are is Asp.net Application.
You need to follow this logic.
Save the uploaded file in temp directory using
PHP Code:
'Get the Temp file name and save it in server temp directory Dim _TempFileName As String = System.IO.Path.GetTempFileName FileUpload1.SaveAs(TempFileName )
And change the connection to
PHP Code:
Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & TempFileName & ";Extended Properties=""Excel 8.0;HDR=No;"""
Now open the connection and do the operation and after everything done, delete the file from server temp directory
Last edited by danasegarane; Oct 20th, 2009 at 12:49 AM.
Please mark you thread resolved using the Thread Tools as shown
-
Oct 20th, 2009, 06:44 AM
#3
Re: What's wrong with my code??
Hey,
Or if you are using ASP.Net, tell us, and your thread can be moved over to the ASP.Net Forum.
Also, when you are posting code into the forum, can you remember to surround it in CODE tags, it makes it a lot easier to read!!
Gary
-
Oct 20th, 2009, 06:52 AM
#4
Re: What's wrong with my code??
Look at this Gary..
Code:
Imports System.Data
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
It is Asp.net Only
Please mark you thread resolved using the Thread Tools as shown
-
Oct 20th, 2009, 06:55 AM
#5
Re: What's wrong with my code??
Hey Dana,
You are quite correct, I missed that, was assuming the Connection String had been copied from somewhere else.
I will ask for the thread to be moved.
Gary
-
Oct 20th, 2009, 06:55 AM
#6
Re: What's wrong with my code??
 Originally Posted by danasegarane
It is Asp.net Only
Moved To ASP.NET
-
Oct 20th, 2009, 09:17 PM
#7
Thread Starter
New Member
Re: [RESOLVED] What's wrong with my code??
Many thanks for your help danasegarane. I've tried it and it worked..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|