473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Howto remove a primary key column in a datatable?

Hi,

I understand you must first remove the constraint. The following bit does
not work (where t is a DataTable). It gives a 'reference not set to an
object' error

t.PrimaryKey.Clear(t.PrimaryKey, 0, 1)
If t.Columns.Contains("MYKEY") Then
If t.Columns.CanRemove(t.Columns("MYKEY")) Then
t.Columns.Remove("MYKEY")
End If
End If

Nov 18 '05 #1
11 20600
Hi ,

Thanks for your posting. As for the problem you mentioned, it seems that
the "non reference exception" is due to we didn't provide a correctly
ColumnName or DataColumn instance. I've tried do some tests locally and we
can remove a certain datacolumn from datatable as long as the CanRemove
return true.

I think you can try provide the DataColumn object instance into the Remove
method instead of the columnName to see whether it helps. For example:

If t.Columns.Contains("KEY") Then
If t.Columns.CanRemove(t.Columns("KEY")) Then
dim col as DataColumn = t.Columns("KEY")
t.Columns.Remove(col)
End If
End If

If you have anyother findings, please also feel free to post here. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2
All Right, Yeah, I'll regard that as a quick fix, but I am sure that there's
something more to it.

I never needed to get the instance of the column when trying to remove it.
It has to do with it being a Primary Key Column, and removing the Primary Key
seems to hurt the column name somehow. I have other pieces of code where I
get a 'Column not found' error after removing the Primary Key from the Keys
collection. E.g. the t.AcceptChanges also gets confused after removing a
Primary Key.

Do we have a bug here?
Nov 18 '05 #3
Hi Martin,

I've done some further test on myside and seems the Remove method which
take the columnName as param also works well. Is the DataTable(DAtaSet) in
your app retrieved from the DataBase and is the table has a single columns
PK or composite PK? I've test on DataTable retrieved from database or
programly built on the fly which contains a single column PK. I remove the
PK setting and then remove the PK column via Colums.Remove("ColumnName") ,
that worked ok. So I'm not sure whether there're anything else particular
in your app. Would also try the thing I mentioned above or provide a simple
demo code sample so that I can try repro it on my side?
Also, if you have any other concerns, please feel free to let me know.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4
incomprehensible!
I've done some further test on myside and seems the Remove method which
take the columnName as param also works well. In my code it does not ...
Is the DataTable(DataSet) in your app retrieved from the DataBase
and is the table has a single columns PK or composite PK?

Programatic table, single column PK

I'll send you my test application
Nov 18 '05 #5
Hi Martin,

I've recieved your mail and the attached test app, I'll made some tests on
my local side and will update you when I got some values. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6
Hi Martin,

After some testing , I've got some results and here is the thing I've found:

==========================
t = GetMyTable()

t.PrimaryKey.Clear(t.PrimaryKey, 0, 1)

col = t.Columns("COL_KEY")

t.Columns.Remove(col)
===========================

The above code will throw NullReference exception , and the following code
worked well

=======================
t = GetMyTable()

t.PrimaryKey = Nothing

col = t.Columns("COL_KEY")

t.Columns.Remove(col)
==========================

And I found the PrimaryKey.Clear(..) ( infact it is Array.Clear) will set
the items(specified via the index range) to Nothing. But the PrimaryKey
array's length is still 1. So I'm think this should be the cause. But still
not sure about the root cause. Currently I'm doing some further research on
this and will let you know if I got any progresses. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #7
Actually I had tried [ t.PrimaryKey = Nothing ] in my code earlier, but I
got errors on [ t.Acceptchanges ] after manipulating the data in the table.

I believed it had to do with the way I cleared the Primary Key, and then
started looking for another way of clearing the key. Then I found the
Primary.Clear method. Actually I was suprised it worked this way since it
asks for a System.Array as the first argument, and although the PrimaryKey is
in fact an array of type Data.DataColumn it looks a bit tricky to me.

Nevertheless, I'll start using [ t.PrimaryKey = Nothing ] and hope I do
not run into the earlier errors that may show up because of that. I'll keep
you informed.
Nov 18 '05 #8
Thanks for your followup Martin,

If you find any further issues on this, please feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #9
Hi Steven,

In you post you wrote:
....But the PrimaryKey array's length is still 1. So I'm think this should be
the cause. but still not sure about the root cause ...

Just out of curiosity; were you able to trace this further? I remains a bit
spooky to me.
Nov 18 '05 #10
Hi Martin,

En, yes, I'm also wondering the remain "1" length in the pk collection.
Currently I still haven't got any further info, but I'll hold on this issue
and let you know if I got any feed back from the DEV guys. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #11
Hi Martin,

After some further research and consulting to the dev guys, they've also
noticed the problem and currently the workaround is just as we mentioned in
the above message:

Use the datatable.PrimaryKey = NOthing rather than Array.Clear to drop the
pk.
If there is constraints on the datatable, do explicitly remove the
constraint

Then, we can delete the pk column.

Also, we'll improve this behavior in the furture version, at least to
provide some more informative and useful message so as not to mislead the
developer. Also, I'm sorry for any inconvenience it has brought you.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Greg Brunet | last post by:
I'm writing some routines for handling dBASE files. I've got a table (DBF file) object & field object already defined, and after opening the file, I can get the field info like this: >>>...
2
by: Bruce A. Julseth | last post by:
I have a dataset that I want to remove the first column. Can I do this without "Select"ing again? If so, how. Thanks....
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
0
by: Carlos Barini | last post by:
I need to change the "Column.BackColor" when the user clicks the ColumnHeader. How can I do that? Tks. Carlos Barini. Brazil.
31
by: Sarita | last post by:
Hello, this might sound stupid, but I got a really nice homepage template which unfortunately is a 3-Column Fixed Width CSS format. Now I don't have any content for the right column and would...
1
by: Ronald S. Cook | last post by:
I have a DataTable like Id Code Value 1 TAB Inventory 2 TAB Marketing 3 GRP Pens 4 ICO Lot I would like to remove all rows where Code=TAB since I'm done with that part.
1
by: sureshv | last post by:
I want to drop primary key from table how is it possible?
0
by: dasyk3 | last post by:
Ok here is the situation, i have a worksheet with multiple sheets in it and Column A in all the sheets has Client Names and the rest of the columns have diff types of data about them depending on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.