473,799 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a new column in a datatable from two existing column

I want to create a new column in a datatable from two existing columns. I
have no problem to create the new column using the datatable.colum ns.add
method. The problem is the value of the new column may become system.dbnull
since one of the two existing columns may have system.dbnull. How can I fix
it so the new column will get the value of the other column even the other
column is system.dbnull?

Sep 29 '07 #1
13 2604
Assuming you are using a SELECT statement to get your data, you can combine
the fields in the statement, and SQL will treat any null cells from a string
type column as empty strings...

SELECT FirstName & " " & LastName AS CustomerName FROM Customers...

In this example, if a FirstName is null and there is only a last name for a
given record, the CustomerName would be shown as only the last name. You
would map your grid column to CustomerName. Also, this example assumes both
columns are strings. You can also combine numeric columns in SQL, but use +
rather than &.
"Peter" wrote:
I want to create a new column in a datatable from two existing columns. I
have no problem to create the new column using the datatable.colum ns.add
method. The problem is the value of the new column may become system.dbnull
since one of the two existing columns may have system.dbnull. How can I fix
it so the new column will get the value of the other column even the other
column is system.dbnull?
Sep 29 '07 #2
Hi Charlie,

The datatable is returned from a .NET method so I have no control on the
SELECT statement.

"Charlie" wrote:
Assuming you are using a SELECT statement to get your data, you can combine
the fields in the statement, and SQL will treat any null cells from a string
type column as empty strings...

SELECT FirstName & " " & LastName AS CustomerName FROM Customers...

In this example, if a FirstName is null and there is only a last name for a
given record, the CustomerName would be shown as only the last name. You
would map your grid column to CustomerName. Also, this example assumes both
columns are strings. You can also combine numeric columns in SQL, but use +
rather than &.
"Peter" wrote:
I want to create a new column in a datatable from two existing columns. I
have no problem to create the new column using the datatable.colum ns.add
method. The problem is the value of the new column may become system.dbnull
since one of the two existing columns may have system.dbnull. How can I fix
it so the new column will get the value of the other column even the other
column is system.dbnull?

Oct 1 '07 #3
I don't know which .net method you are referring to, but you should still be
able to control the SELECT statement. The DataAdapter would be the most
common .net object to use to get data from an Access or SQL database. The
SELECT statement is a property of that object and can be passed as a
parameter, or set as a property.

In any case, as long as you are using a database that runs sql statements,
you should be able to re-code in a way that lets you use a SELECT statement.

"Peter" wrote:
Hi Charlie,

The datatable is returned from a .NET method so I have no control on the
SELECT statement.

"Charlie" wrote:
Assuming you are using a SELECT statement to get your data, you can combine
the fields in the statement, and SQL will treat any null cells from a string
type column as empty strings...

SELECT FirstName & " " & LastName AS CustomerName FROM Customers...

In this example, if a FirstName is null and there is only a last name for a
given record, the CustomerName would be shown as only the last name. You
would map your grid column to CustomerName. Also, this example assumes both
columns are strings. You can also combine numeric columns in SQL, but use +
rather than &.
"Peter" wrote:
I want to create a new column in a datatable from two existing columns. I
have no problem to create the new column using the datatable.colum ns.add
method. The problem is the value of the new column may become system.dbnull
since one of the two existing columns may have system.dbnull. How can I fix
it so the new column will get the value of the other column even the other
column is system.dbnull?
>
>
>
Oct 1 '07 #4
Hi Charlie,

The datatable is the one returned from this method:
http://msdn2.microsoft.com/en-us/lib...tasources.aspx
"Charlie" wrote:
I don't know which .net method you are referring to, but you should still be
able to control the SELECT statement. The DataAdapter would be the most
common .net object to use to get data from an Access or SQL database. The
SELECT statement is a property of that object and can be passed as a
parameter, or set as a property.

In any case, as long as you are using a database that runs sql statements,
you should be able to re-code in a way that lets you use a SELECT statement.

"Peter" wrote:
Hi Charlie,

The datatable is returned from a .NET method so I have no control on the
SELECT statement.

"Charlie" wrote:
Assuming you are using a SELECT statement to get your data, you can combine
the fields in the statement, and SQL will treat any null cells from a string
type column as empty strings...
>
SELECT FirstName & " " & LastName AS CustomerName FROM Customers...
>
In this example, if a FirstName is null and there is only a last name for a
given record, the CustomerName would be shown as only the last name. You
would map your grid column to CustomerName. Also, this example assumes both
columns are strings. You can also combine numeric columns in SQL, but use +
rather than &.
>
>
"Peter" wrote:
>
I want to create a new column in a datatable from two existing columns. I
have no problem to create the new column using the datatable.colum ns.add
method. The problem is the value of the new column may become system.dbnull
since one of the two existing columns may have system.dbnull. How can I fix
it so the new column will get the value of the other column even the other
column is system.dbnull?

Oct 1 '07 #5
"Peter" <Pe***@discussi ons.microsoft.c omschrieb
Hi Charlie,

The datatable is the one returned from this method:
http://msdn2.microsoft.com/en-us/lib...tasources.aspx

I do not completely understand the problem. I see, you are getting this
datatable returned. Now, you want to add another column. You say, the
problem is that there might be NULL values. Why is this a problem? What does
the value of the new column have to be?

Armin

Oct 1 '07 #6
You could write a function that takes a single parameter--Datatable (the one
returned by GetDataSources) . In the function, create a new datatable (that
will be the return value) and populate it in nested loops using the values
from the GetDataSources datatable. In the second loop you would combine the
columns you want to combine into a single column. That's where you could
test for isdbnull.

"Peter" wrote:
Hi Charlie,

The datatable is the one returned from this method:
http://msdn2.microsoft.com/en-us/lib...tasources.aspx
"Charlie" wrote:
I don't know which .net method you are referring to, but you should still be
able to control the SELECT statement. The DataAdapter would be the most
common .net object to use to get data from an Access or SQL database. The
SELECT statement is a property of that object and can be passed as a
parameter, or set as a property.

In any case, as long as you are using a database that runs sql statements,
you should be able to re-code in a way that lets you use a SELECT statement.

"Peter" wrote:
Hi Charlie,
>
The datatable is returned from a .NET method so I have no control on the
SELECT statement.
>
"Charlie" wrote:
>
Assuming you are using a SELECT statement to get your data, you can combine
the fields in the statement, and SQL will treat any null cells from a string
type column as empty strings...

SELECT FirstName & " " & LastName AS CustomerName FROM Customers...

In this example, if a FirstName is null and there is only a last name for a
given record, the CustomerName would be shown as only the last name. You
would map your grid column to CustomerName. Also, this example assumes both
columns are strings. You can also combine numeric columns in SQL, but use +
rather than &.


"Peter" wrote:

I want to create a new column in a datatable from two existing columns. I
have no problem to create the new column using the datatable.colum ns.add
method. The problem is the value of the new column may become system.dbnull
since one of the two existing columns may have system.dbnull. How can I fix
it so the new column will get the value of the other column even the other
column is system.dbnull?
>
>
>
Oct 1 '07 #7
Hi Armin,

I add the new column based on existing columns. If one of the based columns
contains null (dbnull), the new column will contain null regardless the value
in other based columns.

"Armin Zingler" wrote:
"Peter" <Pe***@discussi ons.microsoft.c omschrieb
Hi Charlie,

The datatable is the one returned from this method:
http://msdn2.microsoft.com/en-us/lib...tasources.aspx


I do not completely understand the problem. I see, you are getting this
datatable returned. Now, you want to add another column. You say, the
problem is that there might be NULL values. Why is this a problem? What does
the value of the new column have to be?

Armin

Oct 1 '07 #8
"Peter" <Pe***@discussi ons.microsoft.c omschrieb
Hi Armin,

I add the new column based on existing columns. If one of the based
columns contains null (dbnull), the new column will contain null
regardless the value in other based columns.
What is a "column based on existing columns"?
Armin
Oct 2 '07 #9
"Peter" <Pe***@discussi ons.microsoft.c omschrieb
Hi Armin,

What I mean is the value of the new column will have the combined
value of existing columns I specified using datatable.colum ns.add().
What's a "combined value"? Sorry for asking if that's too obvious.

"Armin Zingler" wrote:
>"Peter" <Pe***@discussi ons.microsoft.c omschrieb
Hi Armin,

I add the new column based on existing columns. If one of the based
columns contains null (dbnull), the new column will contain null
regardless the value in other based columns.

What is a "column based on existing columns"?
Armin
Oct 2 '07 #10

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

Similar topics

2
8567
by: Jacob | last post by:
A simple question: I've read a couple of things that indicates I can create a new table in my database using some SQL command. Is this correct? I would like to load a database into a DataSet, make changes to the existing DataTable(s), add NEW DataTable(s) if necessary and save the changes back to the database. I'm stuck on this problem. Any documentation on this would be great. Thanks,
4
2414
by: Bill Todd | last post by:
Is there any way to display the RowState of each row in a computed column in the DataTable? -- Bill
6
21479
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one 'column' in the ArrayList, the other is automatically sorted and so I can use IndexOf without fear. This is because I will be going through 20,000 some odd records and don't want to use a Lookup type of table/query; I figure an ArrayList.IndexOf method...
3
14317
by: MrNobody | last post by:
I've read that the expression property for DataColumns is used to "Sets or retrieves the expresssion used to filter rows, calculate the values in a column, or create an aggregate column.". I have seen examples on how to filter a column, but how would I filter out an entire row depending on the value of a column? For example, if I wanted to filter out every row in a multi-column table where the "Is Fubar" column equals "true", how would...
1
1911
by: Prince | last post by:
Hi all, Can anyone tell me how to create auto generated values in a DataTable column? Looking forward for the reply.... Thanx in advance...
1
1505
by: jg | last post by:
I need to create a DataTable object, but I dont want to add columns programatically, the DataTable should have columns based on a Table in my SQL database. How can I create a DataTable with column structure based on a SQL database? Thanks
0
1088
by: BostonNole | last post by:
I am trying to find a way to create a new DataTable from an existing DataTable by selecting only certain columns and with a conditional. For example I want to do something like this: Dim myOriginalDataTable as DataTable Dm myNewDataTable as DataTable myNewDataTable = myOriginalDataTable(select column1, column3, column4 where column5 = 1)
4
2170
by: mojeza | last post by:
I would like to create generic object which will be used for store of single row of DataTable. Lets say I create class as follow: Public Class Participant Public ParticipantID As Int64 Public LastName As String Public FirstName As String End Class then I get a data from the database to DataTable and create array of
1
3383
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons: 1) export to excel button exports the visible columns from the datagridview to excel (this works fine)
0
9685
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
9538
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,...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
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...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
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
5461
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...
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
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.