Results 1 to 9 of 9

Thread: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2011
    Location
    Minneapolis
    Posts
    39

    Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    Hi:

    I had some code working to my satisfaction. As a final step, I added a reference to 'Microsoft.Office.Interop.Exce', and now my Data Tables produce errors. Apparently, I am no longer allowed to instantiate a DataTable.

    Code:
    Imports System.Data.OleDb
    Imports Microsoft.Office.Interop.Excel
    
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ds1 As String = "\\stylmark.com\fs-styl\Public\HISTORY\icedet 12mos122912.xlsx"
            Dim ss1 As String = "SELECT [Item], [SONbr], [OrdDate], [Eng], [Cfg#Rev] FROM [MASTER$] " & _
                                "ORDER BY [Item] DESC "
            Dim dt1 As New DataTable
            dt1.Columns.Add("Item", GetType([String]))
    ---

    I have n attachment that illustrates the problem.

    Thanks in advance for the help.
    Attached Images Attached Images  

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    "DataTable" is used in the Microsoft.Office.Interop.Excel namespace. If you are trying to use the data table that we are all familiar with while importing the Microsoft.Office.Interop.Excel namespace then declare your table using its fully qualified name i.e. System.Data.DataTable
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    Imports Microsoft.Office.Interop.Excel creates ambiguities between classes with the same names at this level. Do you mean DataTable or Excel.DataTable? And how is the compiler supposed to know? Step back to Microsoft.Office.Interop and define Excel types explicitly eg. Excel.Application
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2011
    Location
    Minneapolis
    Posts
    39

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    Thanks for the information.

    I have one more small question. In this code:
    Code:
    If dr1.Item("Eng").ToString = "Y" Then
    
                            Try
                                dt3.Rows.Add.Item(0) = dr1.Item("Item")
    
                            Catch ex As Exception
    
                            End Try
    
                            Try
                                ' dt3.Rows.Add.Item(1) = dr1.Item("SONbr")
                                dt3.Rows.Item(1) = "test"
    The last line (directly above) is wrong. If I repeat the syntax:
    Code:
    dt3.Rows.Add.Item(0) = dr1.Item("SONbr")
    then I get a new row for every column. I have about seven columns per row. How can I control this?

    For an overview, I an manually populating a data table. Everything is working except for the data format.

    Thanks again!

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    you can't have a data table row that does not contain every column in the data table. It's just how it works.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2011
    Location
    Minneapolis
    Posts
    39

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    That's great, because this is not what I'm looking for. In the data above, if dt3.Rows.Add.Item(x) is used, I am getting the staggered rows/columns:
    ROW0 ROW1 ROW2 ROW3 ROW4
    1 DATA 1
    2 ----- DATA 2
    3 ----- ----- DATA 3
    4 ----- ----- ----- DATA 4
    5 ----- ----- ----- ----- DATA 5

    I would like all of this to end up on one row.

    Thanks

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    vb.net Code:
    1. dt3.Rows.Add.Item(0) = dr1.Item("Item") ' means add a row with a value in column 0

    One method of building a datatable from scratch ...

    vb.net Code:
    1. Dim dsTable As DataTable = New DataTable
    2.  
    3.  
    4.    
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         For i = 0 To 3
    8.             dsTable.Columns.Add() ' you already did this bit to get your 7 columns
    9.         Next
    10.  
    11.        
    12.             Dim row As DataRow = dsTable.NewRow ' create a new row
    13.  
    14.             With row                   ' insert values into each column
    15.                 .SetField(0, "This")
    16.                 .SetField(1, "That")
    17.                 .SetField(2, "The Other")
    18.                 .SetField(3, "Whatever")
    19.             End With
    20.  
    21.             dsTable.Rows.Add(row) ' add the row to the table
    22.        
    23.  
    24.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2011
    Location
    Minneapolis
    Posts
    39

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    Thank you...

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Adding a reference to Microsoft.Office.Interop.Excel disables New DataTables

    I'm curious about that try...catch you have in there. Is that real? What are you trying to prevent? Has dunfiddlin's answer gotten around that problem?

    I really dislike empty Catch blocks. The only time they ever made sense to me was a certain problem with some UDP communication work I did a long time ago where meaningless errors were occasionally thrown.
    My usual boring signature: Nothing

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