473,765 Members | 2,010 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(sql SelectPersonal, conn);
OdbcDataReader PerReader = command.Execute Reader();

while (PerReader.Read ())
{
int address_id = PerReader.GetOr dinal("address_ id");
int picture_id = PerReader.GetOr dinal("picture_ id");
int last_name = PerReader.GetOr dinal("last_nam e");
int first_name = PerReader.GetOr dinal("first_na me");
int middle_name = PerReader.GetOr dinal("middle_n ame");

txtLastName.Tex t = PerReader.GetSt ring(last_name) .ToString();
txtFirstName.Te xt = PerReader.GetSt ring(first_name ).ToString();
txtMiddleName.T ext = PerReader.GetSt ring(middle_nam e).ToString();
//GetInt32 because it is a number
txtAddressID.Te xt = PerReader.GetIn t32(address_id) .ToString();
}

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

While myReader.Read()
if not IsDBNull(myRead er.Item("prodNu mber")) 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 1330
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.goo glegroups.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(sql SelectPersonal, conn);
OdbcDataReader PerReader = command.Execute Reader();

while (PerReader.Read ())
{
int address_id = PerReader.GetOr dinal("address_ id");
int picture_id = PerReader.GetOr dinal("picture_ id");
int last_name = PerReader.GetOr dinal("last_nam e");
int first_name = PerReader.GetOr dinal("first_na me");
int middle_name = PerReader.GetOr dinal("middle_n ame");

txtLastName.Tex t = PerReader.GetSt ring(last_name) .ToString();
txtFirstName.Te xt = PerReader.GetSt ring(first_name ).ToString();
txtMiddleName.T ext = PerReader.GetSt ring(middle_nam e).ToString();
//GetInt32 because it is a number
txtAddressID.Te xt = PerReader.GetIn t32(address_id) .ToString();
}

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

While myReader.Read()
if not IsDBNull(myRead er.Item("prodNu mber")) 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.goo glegroups.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.Tex t = (string)PerRead er["last_name"];

Nov 19 '05 #5

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

Similar topics

4
2620
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."): objTable.Columns(s_name_field_to_delete).delete AND SAME ERROR WITH: objTable.Columns.delete s_name_field_to_delete AND SAME ERROR WITH: set objColumn = objTable.Columns(s_name_field_to_delete)
24
2519
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 Microsoft policy to globalize all tag names. Is there any method for cross-browser fix without using getelementbyid?
3
9494
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 below but I get an exception thrown because the column doesn't exist in Datareader B. I'm not opposed to using another method but the same thing happened when using a Dataset. How do I do this??
5
25581
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 As Integer Dim colName(99) As String Dim colType(99) As String cn.Open() tbl = cn.GetSchema("Orders") 'Orders is a table in the
0
1632
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 problem is that as we create new databases and tables, we have never paid attention to column ordering. Who cares right? Well triggers seem to care if you need it to key off a change in a specific column in a specific data table. I do not see any...
0
9568
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
9399
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
9957
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
8832
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...
1
7379
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
6649
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
5276
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
3924
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 we have to send another system
3
2806
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.