473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 strProductionSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdProd = new SqlCommand(strProductionSQL,cnProductionSQL);

SqlDataReader dtrProd = cmdProd.ExecuteReader();

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

cnDevelopmentSQL.Open();

String strDevelopmentSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdDev = new SqlCommand(strDevelopmentSQL, cnDevelopmentSQL);

SqlDataReader dtrDev = cmdDev.ExecuteReader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder();

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

int Ordinal;

dtrDev.Read();

while (dtrProd.Read())

{

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

{

if (dtrProd.GetName(i)!= "ID")

{

Ordinal = dtrDev.GetOrdinal(dtrProd.GetName(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.Append(",");

}

sbUpdateQuery.Append(dtrProd.GetName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.Append("WHERE CompanyID = '" + strSQLDestDatabase + "'");

dtrDev.Close();

dtrProd.Close();

//dtrDev = cmdDev.ExecuteNonQuery(sbUpdateQuery.ToString());

}
Jun 16 '06 #1
3 9384
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**************@TK2MSFTNGP03.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 strProductionSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdProd = new SqlCommand(strProductionSQL,cnProductionSQL);

SqlDataReader dtrProd = cmdProd.ExecuteReader();

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

cnDevelopmentSQL.Open();

String strDevelopmentSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdDev = new SqlCommand(strDevelopmentSQL, cnDevelopmentSQL);

SqlDataReader dtrDev = cmdDev.ExecuteReader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder();

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

int Ordinal;

dtrDev.Read();

while (dtrProd.Read())

{

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

{

if (dtrProd.GetName(i)!= "ID")

{

Ordinal = dtrDev.GetOrdinal(dtrProd.GetName(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.Append(",");

}

sbUpdateQuery.Append(dtrProd.GetName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.Append("WHERE CompanyID = '" + strSQLDestDatabase + "'");

dtrDev.Close();

dtrProd.Close();

//dtrDev = cmdDev.ExecuteNonQuery(sbUpdateQuery.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**************@TK2MSFTNGP03.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 strProductionSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdProd = new SqlCommand(strProductionSQL,cnProductionSQL);

SqlDataReader dtrProd = cmdProd.ExecuteReader();

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

cnDevelopmentSQL.Open();

String strDevelopmentSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdDev = new SqlCommand(strDevelopmentSQL, cnDevelopmentSQL);

SqlDataReader dtrDev = cmdDev.ExecuteReader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder();

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

int Ordinal;

dtrDev.Read();

while (dtrProd.Read())

{

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

{

if (dtrProd.GetName(i)!= "ID")

{

Ordinal = dtrDev.GetOrdinal(dtrProd.GetName(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.Append(",");

}

sbUpdateQuery.Append(dtrProd.GetName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.Append("WHERE CompanyID = '" + strSQLDestDatabase + "'");

dtrDev.Close();

dtrProd.Close();

//dtrDev = cmdDev.ExecuteNonQuery(sbUpdateQuery.ToString());

}

Jun 16 '06 #3
Hi,

Unfortunately DataReader does not has a Columns collection,
You have 3 options:
1- Use DataReder.GetName( 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.GetSchemaTable :
if ( DataReader.GetSchemaTable.Columns[ 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**************@TK2MSFTNGP03.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 strProductionSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdProd = new SqlCommand(strProductionSQL,cnProductionSQL);

SqlDataReader dtrProd = cmdProd.ExecuteReader();

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

cnDevelopmentSQL.Open();

String strDevelopmentSQL = "SELECT * FROM [Global].[dbo].[Companies] WHERE
CompanyID ='" + strSQLDestDatabase + "'";

SqlCommand cmdDev = new SqlCommand(strDevelopmentSQL, cnDevelopmentSQL);

SqlDataReader dtrDev = cmdDev.ExecuteReader();
if (dtrDev.HasRows == true)

{

StringBuilder sbUpdateQuery = new StringBuilder();

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

int Ordinal;

dtrDev.Read();

while (dtrProd.Read())

{

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

{

if (dtrProd.GetName(i)!= "ID")

{

Ordinal = dtrDev.GetOrdinal(dtrProd.GetName(i));

if (Ordinal > 0)

{

if (i != 1)

{

sbUpdateQuery.Append(",");

}

sbUpdateQuery.Append(dtrProd.GetName(i) + "='" + dtrProd[i].ToString() +
"'");

}

}

}

}

sbUpdateQuery.Append("WHERE CompanyID = '" + strSQLDestDatabase + "'");

dtrDev.Close();

dtrProd.Close();

//dtrDev = cmdDev.ExecuteNonQuery(sbUpdateQuery.ToString());

}

Jun 16 '06 #4

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

Similar topics

1
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...
1
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...
6
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 {...
3
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...
3
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...
2
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...
5
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
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 & "'"...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...

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.