473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Verifying a column exists in a datareader

RSH
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??
Thanks,
Ron

SqlConnection cnProductionSQL = new SqlConnection(" Data Source=" +
cmbSourceServer .SelectedItem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnProductionSQL .Open();

String strProductionSQ L = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdProd = new SqlCommand(strP roductionSQL,cn ProductionSQL);

SqlDataReader dtrProd = cmdProd.Execute Reader();

SqlConnection cnDevelopmentSQ L = new SqlConnection(" Data Source=" +
cmbDestinationS erver.SelectedI tem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnDevelopmentSQ L.Open();

String strDevelopmentS QL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdDev = new SqlCommand(strD evelopmentSQL, cnDevelopmentSQ L);

SqlDataReader dtrDev = cmdDev.ExecuteR eader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder() ;

sbUpdateQuery.A ppend("UPDATE [Global].[dbo].[Companies] SET ");

int Ordinal;

dtrDev.Read();

while (dtrProd.Read() )

{

for(int i = 0; i < dtrProd.FieldCo unt; i++)

{

if (dtrProd.GetNam e(i)!= "ID")

{

Ordinal = dtrDev.GetOrdin al(dtrProd.GetN ame(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.A ppend(",");

}

sbUpdateQuery.A ppend(dtrProd.G etName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.A ppend("WHERE CompanyID = '" + strSQLDestDatab ase + "'");

dtrDev.Close();

dtrProd.Close() ;

//dtrDev = cmdDev.ExecuteN onQuery(sbUpdat eQuery.ToString ());

}
Jun 16 '06 #1
3 9441
You can do in 2 ways

1) Use GetSchemaTable( ) method to get the metadata about the sqldatareader
and check the datatable or use the datatable in your loop
2) use try catch to catch the error and if there is an error the column not
exists...not a recommended method

"RSH" <wa************ *@yahoo.com> wrote in message
news:eK******** ******@TK2MSFTN GP03.phx.gbl...
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??
Thanks,
Ron

SqlConnection cnProductionSQL = new SqlConnection(" Data Source=" +
cmbSourceServer .SelectedItem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnProductionSQL .Open();

String strProductionSQ L = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdProd = new SqlCommand(strP roductionSQL,cn ProductionSQL);

SqlDataReader dtrProd = cmdProd.Execute Reader();

SqlConnection cnDevelopmentSQ L = new SqlConnection(" Data Source=" +
cmbDestinationS erver.SelectedI tem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnDevelopmentSQ L.Open();

String strDevelopmentS QL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdDev = new SqlCommand(strD evelopmentSQL, cnDevelopmentSQ L);

SqlDataReader dtrDev = cmdDev.ExecuteR eader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder() ;

sbUpdateQuery.A ppend("UPDATE [Global].[dbo].[Companies] SET ");

int Ordinal;

dtrDev.Read();

while (dtrProd.Read() )

{

for(int i = 0; i < dtrProd.FieldCo unt; i++)

{

if (dtrProd.GetNam e(i)!= "ID")

{

Ordinal = dtrDev.GetOrdin al(dtrProd.GetN ame(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.A ppend(",");

}

sbUpdateQuery.A ppend(dtrProd.G etName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.A ppend("WHERE CompanyID = '" + strSQLDestDatab ase + "'");

dtrDev.Close();

dtrProd.Close() ;

//dtrDev = cmdDev.ExecuteN onQuery(sbUpdat eQuery.ToString ());

}

Jun 16 '06 #2
Use datatables instead. Then you can check the Columns collection to see if
a particular column exists.

"RSH" <wa************ *@yahoo.com> wrote in message
news:eK******** ******@TK2MSFTN GP03.phx.gbl...
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??
Thanks,
Ron

SqlConnection cnProductionSQL = new SqlConnection(" Data Source=" +
cmbSourceServer .SelectedItem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnProductionSQL .Open();

String strProductionSQ L = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdProd = new SqlCommand(strP roductionSQL,cn ProductionSQL);

SqlDataReader dtrProd = cmdProd.Execute Reader();

SqlConnection cnDevelopmentSQ L = new SqlConnection(" Data Source=" +
cmbDestinationS erver.SelectedI tem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnDevelopmentSQ L.Open();

String strDevelopmentS QL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdDev = new SqlCommand(strD evelopmentSQL, cnDevelopmentSQ L);

SqlDataReader dtrDev = cmdDev.ExecuteR eader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder() ;

sbUpdateQuery.A ppend("UPDATE [Global].[dbo].[Companies] SET ");

int Ordinal;

dtrDev.Read();

while (dtrProd.Read() )

{

for(int i = 0; i < dtrProd.FieldCo unt; i++)

{

if (dtrProd.GetNam e(i)!= "ID")

{

Ordinal = dtrDev.GetOrdin al(dtrProd.GetN ame(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.A ppend(",");

}

sbUpdateQuery.A ppend(dtrProd.G etName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.A ppend("WHERE CompanyID = '" + strSQLDestDatab ase + "'");

dtrDev.Close();

dtrProd.Close() ;

//dtrDev = cmdDev.ExecuteN onQuery(sbUpdat eQuery.ToString ());

}

Jun 16 '06 #3
Hi,

Unfortunately DataReader does not has a Columns collection,
You have 3 options:
1- Use DataReder.GetNa me( index ) , you coudl do a method like:
int GetIndex( DataReader dr, string name)
{ for(int i=0; i< dr.FieldCount; i++)
if ( dr.GetName(i) == name ) return i;
return -1;
}

2- Use DataReader.GetS chemaTable :
if ( DataReader.GetS chemaTable.Colu mns[ name] == null ) // does
not exist

3- try to access it and catch the exception

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"RSH" <wa************ *@yahoo.com> wrote in message
news:eK******** ******@TK2MSFTN GP03.phx.gbl...
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??
Thanks,
Ron

SqlConnection cnProductionSQL = new SqlConnection(" Data Source=" +
cmbSourceServer .SelectedItem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnProductionSQL .Open();

String strProductionSQ L = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdProd = new SqlCommand(strP roductionSQL,cn ProductionSQL);

SqlDataReader dtrProd = cmdProd.Execute Reader();

SqlConnection cnDevelopmentSQ L = new SqlConnection(" Data Source=" +
cmbDestinationS erver.SelectedI tem + "; Integrated Security=SSPI; Initial
Catalog=Master" );

cnDevelopmentSQ L.Open();

String strDevelopmentS QL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatab ase + "'";

SqlCommand cmdDev = new SqlCommand(strD evelopmentSQL, cnDevelopmentSQ L);

SqlDataReader dtrDev = cmdDev.ExecuteR eader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder() ;

sbUpdateQuery.A ppend("UPDATE [Global].[dbo].[Companies] SET ");

int Ordinal;

dtrDev.Read();

while (dtrProd.Read() )

{

for(int i = 0; i < dtrProd.FieldCo unt; i++)

{

if (dtrProd.GetNam e(i)!= "ID")

{

Ordinal = dtrDev.GetOrdin al(dtrProd.GetN ame(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.A ppend(",");

}

sbUpdateQuery.A ppend(dtrProd.G etName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.A ppend("WHERE CompanyID = '" + strSQLDestDatab ase + "'");

dtrDev.Close();

dtrProd.Close() ;

//dtrDev = cmdDev.ExecuteN onQuery(sbUpdat eQuery.ToString ());

}

Jun 16 '06 #4

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

Similar topics

1
2308
by: Bigtoga | last post by:
Okay - I'm going crazy here trying to figure out how to do this - here goes: I have a DataList populated by a DataReader. In the DataReader, a column is present called "IsInCategory". I want to have a checkbox bound to that column (bit in SQL Server). I have tried everything and can't see a way to do this without createing an event...
1
2199
by: ae | last post by:
My datareader looks like this where dtrItemList is the datareader and chklExceptionList is my checkboxlist. the rows come in just fine, but I need help also including the name of the columns for my rows. I need them listed only one time on top. Any idea? Thanks in advance. while (dtrItemList.Read()) {...
6
1998
by: Opie | last post by:
What would be a more efficient way for me to determine if a record in an SQL DB table exists? Right now, I have a try/catch like this: try { if(checkcom.ExecuteScalar().ToString()==tbBillNumber.Text) { .... } }
3
2620
by: hazz | last post by:
The datareader below contains two rows of two columns but in the for loop, the values for only the first row are getting printed twice. How do I get to the values of the second row? Thanks. -hazz Dim dTable As DataTable Dim dsBlock As New DataSet Dim rowBlock As DataRow Dim myColumn As DataColumn Dim iCol As Integer = 0
3
2690
by: Thomas Hallgren | last post by:
I'm connected to a database and I want to verify that a username and password for some user is correct. I know I can verify a users existence by doing: select exists(select * from pg_user where usename = $1) but I would like to verify the correctness of the password as well. Is there a way to do that using SQL? Regards,
2
7331
by: RSH | last post by:
Hi, Iam struggling with an application where I am trying to transfer a datarow from one sql server to another instance of sql server. The schmeas may be slightly different and I am getting an exception when they are different. Is there anyway i can modify the code below so that if the schemas are different I can drop the offending column...
5
21659
by: Dylan Parry | last post by:
Hi, At the moment I use code like the following: string myString = this.dataReader.IsDBNull(2) ? null : this.dataReader.GetString(2); With a record from the DB that looks like: ..---------------------------.
1
5464
by: bindurajeesh | last post by:
I run the following code: objoledbcommand.Connection.Open() strsql = "select first_name + ' ' + last_name as fullname from tblperson where pers_entity_id = '" & ddlClient.SelectedValue & "'" objoledbcommand.CommandText = strsql objoledbdatareader = objoledbcommand.ExecuteReader txtClient.Text = objoledbdatareader.Item("first_name")...
1
12129
by: majidkorai | last post by:
Hey Guyz I am having problem when i read the data from a datareader into a data table. The whole scenario is this that I want to read data from two diffrent databases, the table strcutre is same. You can say that other one is the copy of 1st one. I want to read out the data from one table in one database and merge the results with the one...
0
7612
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...
0
7924
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. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7968
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...
0
6283
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...
1
5512
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...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
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

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.