473,511 Members | 14,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a way to get a column by name instead of ordinal?

Hi, using C#, I have to do this to get the database columns in a
reader. Can it be done in one step?:
OdbcCommand command = new OdbcCommand(sqlSelectPersonal, conn);
OdbcDataReader PerReader = command.ExecuteReader();

while (PerReader.Read())
{
int address_id = PerReader.GetOrdinal("address_id");
int picture_id = PerReader.GetOrdinal("picture_id");
int last_name = PerReader.GetOrdinal("last_name");
int first_name = PerReader.GetOrdinal("first_name");
int middle_name = PerReader.GetOrdinal("middle_name");

txtLastName.Text = PerReader.GetString(last_name).ToString();
txtFirstName.Text = PerReader.GetString(first_name).ToString();
txtMiddleName.Text = PerReader.GetString(middle_name).ToString();
//GetInt32 because it is a number
txtAddressID.Text = PerReader.GetInt32(address_id).ToString();
}

In VB.NET I did it all in one step like this:

While myReader.Read()
if not IsDBNull(myReader.Item("prodNumber")) then
txtProduct.text = myReader.Item("prodNumber")
end if
Also, do you know how to do the IsDBNull test in C# like this VB.NET
above? Thank you for your time.

Nov 19 '05 #1
4 1323
If you can index by name in VB, you can index by name in C# too. It is the
exact same class you are using.

You can check if an object equals to DBNull.Value, which is the equivalent
of calling the IsDBNull function.

<ne***********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi, using C#, I have to do this to get the database columns in a
reader. Can it be done in one step?:
OdbcCommand command = new OdbcCommand(sqlSelectPersonal, conn);
OdbcDataReader PerReader = command.ExecuteReader();

while (PerReader.Read())
{
int address_id = PerReader.GetOrdinal("address_id");
int picture_id = PerReader.GetOrdinal("picture_id");
int last_name = PerReader.GetOrdinal("last_name");
int first_name = PerReader.GetOrdinal("first_name");
int middle_name = PerReader.GetOrdinal("middle_name");

txtLastName.Text = PerReader.GetString(last_name).ToString();
txtFirstName.Text = PerReader.GetString(first_name).ToString();
txtMiddleName.Text = PerReader.GetString(middle_name).ToString();
//GetInt32 because it is a number
txtAddressID.Text = PerReader.GetInt32(address_id).ToString();
}

In VB.NET I did it all in one step like this:

While myReader.Read()
if not IsDBNull(myReader.Item("prodNumber")) then
txtProduct.text = myReader.Item("prodNumber")
end if
Also, do you know how to do the IsDBNull test in C# like this VB.NET
above? Thank you for your time.

Nov 19 '05 #2
There is no myReader.Item in C# like in VB.NET that I can find. Maybe
it is called something else?

Nov 19 '05 #3
You can index directly into it:

myReader["myCol"]

In VB this would be:

myReader("myCol")

The Item thing is because VB doesn't support indexers in the same way as C#.
So they defined a default property named Item. But in C# you have the
concept of an indexer.

<ne***********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
There is no myReader.Item in C# like in VB.NET that I can find. Maybe
it is called something else?

Nov 19 '05 #4
Thanks I was able to do this:

txtLastName.Text = (string)PerReader["last_name"];

Nov 19 '05 #5

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

Similar topics

4
2608
by: Scotter | last post by:
Hi folx - Here is the code I'm trying (but it errors out with "Item cannot be found in the collection corresponding to the requested name or ordinal."): ...
24
2479
by: Chameleon | last post by:
This code does not working in Mozilla. Works fine in IE. -------------- <input type=text value=100 name=textbox> <script> alert(textbox); </script> -------------- This perhaps, because of...
3
9421
by: RSH | last post by:
Hi, I have a situation where I have two datareaders, and I want to make sure any given field from Datareader A exists in Datareader B before I can do anything with that column. I tried the code...
5
25518
by: Ken | last post by:
I'm trying to run a loop to capture column property information from a table in my datasource. Can anybody see where this is going wrong? Dim tbl As New DataTable Dim col As DataColumn Dim x...
0
1615
by: blitzenn | last post by:
I am attempting to use triggers to add some simple auditing to my database structure (SQL Server 2005). I am running an ASP with this so I have many databases (dozens) with identical structure. My...
0
7252
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,...
0
7153
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
7432
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
7517
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...
0
5676
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,...
0
4743
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.