Results 1 to 7 of 7

Thread: [RESOLVED] What's wrong with my code??

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    11

    Resolved [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:
    1. Imports System.Data
    2. Imports System.Data.OleDb
    3.  
    4. Partial Class _Default
    5.     Inherits System.Web.UI.Page
    6.  
    7.     Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
    8.         If fu_Load.HasFile Then
    9.             LoadSheets()
    10.         End If
    11.     End Sub
    12.  
    13.     Private Sub LoadSheets()
    14.         Dim Con As New OleDb.OleDbConnection
    15.         Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fu_Load.HasFile & ";Extended Properties=""Excel 8.0;HDR=No;"""
    16.  
    17.         Try
    18.             Con.Open()
    19.  
    20.             Dim DT As New DataTable("Sheets")
    21.             DT = Con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    22.  
    23.             ' Clear controls in placeholder
    24.             PH.Controls.Clear()
    25.  
    26.             ' Add controls
    27.             For Each Row As DataRow In DT.Rows
    28.                 Dim Control As DGControl = LoadControl("DGControl.ascx")
    29.  
    30.                 ' Set Name
    31.                 Control.TabName = Row.Item("Table_Name")
    32.  
    33.                 ' Set Data
    34.                 Control.Data = Get_ExcelData(Con, Row.Item("Table_Name"))
    35.  
    36.                 PH.Controls.Add(Control)
    37.                 PH.Controls.Add(New LiteralControl("<br />"))
    38.             Next
    39.  
    40.         Catch ex As Exception
    41.             MsgBox(ex.Message)
    42.         End Try
    43.     End Sub
    44.  
    45.     Private Function Get_ExcelData(ByVal Con As OleDb.OleDbConnection, ByVal SheetName As String) As DataTable
    46.         Dim DT As New DataTable("Data")
    47.         Dim DA As New OleDbDataAdapter("Select * from [" & SheetName & "]", Con)
    48.         DA.Fill(DT)
    49.  
    50.         Return DT
    51.     End Function
    52. End Class
    Thanks alot!

    ---Jhimz
    Last edited by Hack; Oct 20th, 2009 at 06:54 AM. Reason: Added Highlight Tags

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  4. #4
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    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

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: What's wrong with my code??

    Quote Originally Posted by danasegarane View Post
    It is Asp.net Only
    Moved To ASP.NET

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    11

    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
  •  



Click Here to Expand Forum to Full Width