Solved

Problem adding a column to a DataTable on second occurence of New DataTable()

Posted on 2006-10-25
2
253 Views
Last Modified: 2010-04-23
Hello,

Here's my sequence

Public Class frmMain
    Inherits System.Windows.Forms.Form

    Private dsADP As New DataSet
    Private tblTA_ADP_Hrs As DataTable

.....

    Private Sub BindData()

'fill table with sql statement

   daADP.SelectCommand = New Odbc.OdbcCommand(sSQLTA_ADPHrs, dcADP)
            daADP.Fill(dsADP, "tblTA_ADP_Hrs")

......

'Add column to table

Dim dcHours As New DataColumn
                dcHours.DataType = System.Type.GetType("System.Double")
                dcHours.AllowDBNull = True
                dcHours.ColumnName = "Regular_Hours"
                dsADP.Tables("tblTA_ADP_Hrs").Columns.Add(dcHours)             (this is the line it dies on second time through)


This column adds fine, but if I run my program a second time (fill a datagrid and create csv file) and then attempt to BindData() again it fails when trying to add this column.  I have a Try - Catch loop that dumps out to my error string.

At the end of my create file routine, I'm trying to "refresh" all tables, relations, and columns but still get error.  I have a feeling it thinks the column is already there??

dsADP.Tables("tblTA_ADP_Hrs").Rows.Clear()
        dsADP.Tables("tblTA_ADP_Hrs").Dispose()
        dsADP.Relations.Clear()
        dgJBTimeAttendance.Refresh()


Any thoughts would be greatly appreciated.

Thanks,
JMO9966

0
Comment
Question by:JMO9966
2 Comments
 
LVL 10

Accepted Solution

by:
gangwisch earned 250 total points
ID: 17807015
it's because you can not have two of the same columns named the same thing. this is the line that is killing you
dcHours.ColumnName = "Regular_Hours"
maybe you can try doing a  dsADP.Tables("tblTA_ADP_Hrs").Columns.remove("Regular_Hours") before adding
0
 

Author Comment

by:JMO9966
ID: 17807107
Thanks Gangwisch!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question