473,654 Members | 3,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Cl ear(t.PrimaryKe y, 0, 1)
If t.Columns.Conta ins("MYKEY") Then
If t.Columns.CanRe move(t.Columns( "MYKEY")) Then
t.Columns.Remov e("MYKEY")
End If
End If

Nov 18 '05 #1
11 20647
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.Conta ins("KEY") Then
If t.Columns.CanRe move(t.Columns( "KEY")) Then
dim col as DataColumn = t.Columns("KEY" )
t.Columns.Remov e(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(DAtaS et) 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
incomprehensibl e!
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(DataS et) 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.Cl ear(t.PrimaryKe y, 0, 1)

col = t.Columns("COL_ KEY")

t.Columns.Remov e(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.Remov e(col)
=============== ===========

And I found the PrimaryKey.Clea r(..) ( 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

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

Similar topics

7
3834
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: >>> tbl.Fields() What I would like to do is be able to extract the field names into a single, separate list. It should look like:
2
19304
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
5399
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 example first:
0
1051
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
3088
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 like the middle column just to use up that space instead. Any way this is possible? Thanks for your help, it is highly appreciated!
1
4558
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
21230
by: sureshv | last post by:
I want to drop primary key from table how is it possible?
0
1288
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 sheet. so if a new client comes in I have to update Column A one by one in all the sheets to avoid messing up the rows for existing clients. is there a way through which i can map column A to all my sheets, so that if i have to add a new row for...
0
8375
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8593
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.