Results 1 to 12 of 12

Thread: Re: Write data to Excel VB.Net [2005] (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Write data to Excel VB.Net [2005] (Resolved)

    I am trying to use this code and am not able to get the data from the dataset into the datatable that I need for the export.

    Code:
            Dim cnn As New SqlConnection(strCon)
            Dim cmd As New SqlCommand
            Dim da As New SqlDataAdapter
            Dim ds As New DataSet
            'Try
            cmd = cnn.CreateCommand
            cmd.CommandText = "SELECT StoreId, PurchaseOrderNumber....."
            da.SelectCommand = cmd
            da.Fill(ds, "PurchaseOrders")
            dgvPoNumbers.DataSource = ds
            dgvPoNumbers.DataMember = "PurchaseOrders"
    
            Dim poTable As DataTable = ds.Tables.Add("OrderExport")
            Export("c:\myFile.xls", poTable)
    I just keep getting a blank sheet.
    Last edited by FastEddie; Mar 16th, 2007 at 07:05 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: data from the dataset

    Post split to separate new thread.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: data from the dataset

    That's because you're passing a blank table to the Export method. This line:
    vb Code:
    1. Dim poTable As DataTable = ds.Tables.Add("OrderExport")
    Creates a new DataTable named "OrderExport", adds it to your DataSet and returns a reference to it. You don't add any schema, let alone data, before calling Export.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: data from the dataset

    I can make it work by adding a second dataset (da2 instead of da).
    Code:
                da2.SelectCommand = cmd
                da2.Fill(ds2, "OrderExport")
                Dim poTable As DataTable = ds2.Tables("OrderExport")
                Export("c:\myFile.xls", poTable)
    But why can't I use the existing dataset without having to recreate it?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: data from the dataset

    Because you already have it populated with a different table.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: data from the dataset

    Just as Rob says. Look at your first code snippet in post #1. You're populating a table named "PurchaseOrders" and then trying to export data from a table named "OrderExport". Where do you put any data into "OrderExport"? Your second code snippet DOES populate a table named "ExportTable" so, lo and behold, it contains data to export. You don't need two DataSets. One DataSet can contain as many DataTables as you like. You simply have to put some data into a table before you can get any out. Does that sound like life in general?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: data from the dataset

    Ok thanks. One more question on this subjust before I mark it resolved. I want to check for records in the dataset before I do the export. Is there a way to say:
    Code:
                'Psudo Code Here
                If ds (has records)  Then
                    Dim poTable As DataTable = ds.Tables("OrderExport")
                    Dim dtDate As Date = Today.Date
                    Dim strExportName As String = _
                        "C:\mighty\mighty_" & dtDate.ToString("yyyy-MM-dd")
                Else
                    Exit Sub
                End If

  8. #8
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: data from the dataset

    Quote Originally Posted by FastEddie
    Ok thanks. One more question on this subjust before I mark it resolved. I want to check for records in the dataset before I do the export. Is there a way to say:
    Here is the way i normally check to make sure a dataset i just populated has data in it. I'm some may think its overkill but its always worked for me
    vb Code:
    1. If Not IsNothing(ds) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables("OrderExport").Rows.Count > 0 Then
    2.           Dim poTable As DataTable = ds.Tables("OrderExport")
    3.           Dim dtDate As Date = Today.Date
    4.           Dim strExportName As String = _
    5.                "C:\mighty\mighty_" & dtDate.ToString("yyyy-MM-dd")
    6.      Else
    7.           Exit Sub
    8.      End If

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: data from the dataset

    Thanks that worked perfectly.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Write data to Excel VB.Net [2005] (Resolved)

    There's a problem with that code. First it checks whether there is a DataSet, then it checks whether the DataSet has DataTables, then it checks whether the "OrderExport" table contains rows. At no point does it check whether there is a DataTable named "OrderExport". I would think that with properly written code there should always be a table with that name but if you're going to go to all the trouble of doing all that checking then you need to check for the existence of that specific table too or the last condition could crash. It should be:
    vb Code:
    1. If ds IsNot Nothing AndAlso ds.Tables.Contains("OrderExport") AndAlso ds.Tables("OrderExport").Rows.Count > 0 Then
    Note that "ds IsNot Nothing" is for 2005 only in previous versions use "Not ds Is Nothing". Please specify your version so we know. Also, don't use the IsNothing function at all.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: Write data to Excel VB.Net [2005] (Resolved)

    Quote Originally Posted by jmcilhinney
    At no point does it check whether there is a DataTable named "OrderExport".
    Thats a very good point. Normally when I use this code I don't refer to the table by name, i just refer to the first instance.
    vb Code:
    1. ds.Tables(0).Rows.Count

    Quote Originally Posted by jmcilhinney
    Also, don't use the IsNothing function at all.
    Why should i not be using the "IsNothing" function?

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Write data to Excel VB.Net [2005] (Resolved)

    Quote Originally Posted by ProphetBeal
    Why should i not be using the "IsNothing" function?
    The same reason that you shouldn't use the MsgBox function. From the MSDN help topic for the IsNothing function:
    Namespace: Microsoft.VisualBasic

    Module: Information

    Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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