developer155
Programmer
Hello,
I am passing a DataReader object to a calling methid but when I try to get the data out of DataReader in the calling methid I get the error that data does not exist
I know that the connection has to be open and I did make sure that it is. I can go through the records in the methid that creates the reader, but not in the method that calls it.
Here is my code:
Public Class Conn
{
public SqlDataReader GetData(string strStatement)
{
SqlDataReader dr = null;
try
{
SqlCommand cmd = new SqlCommand(strStatement, cn1);
cn1.Open();
dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine(dr.GetString(0));//this works fine!
}
}
catch (System.Exception e)
{
System.Console.WriteLine(e.Message.ToString());
return null;
}
return dr;
}
Now this is the calling procedure:
string strPropSelect="SELECT * FROM TMYTABLE";
dr = conn.GetData(strPropSelect);
if (!dr.HasRows)//this is fine
{
System.Console.WriteLine("ERROR: Failed to load property data");
return;
}
//but it errors out below
System.Console.WriteLine(dr.GetString(0));
Thanks for any help!!!
I am passing a DataReader object to a calling methid but when I try to get the data out of DataReader in the calling methid I get the error that data does not exist
I know that the connection has to be open and I did make sure that it is. I can go through the records in the methid that creates the reader, but not in the method that calls it.
Here is my code:
Public Class Conn
{
public SqlDataReader GetData(string strStatement)
{
SqlDataReader dr = null;
try
{
SqlCommand cmd = new SqlCommand(strStatement, cn1);
cn1.Open();
dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine(dr.GetString(0));//this works fine!
}
}
catch (System.Exception e)
{
System.Console.WriteLine(e.Message.ToString());
return null;
}
return dr;
}
Now this is the calling procedure:
string strPropSelect="SELECT * FROM TMYTABLE";
dr = conn.GetData(strPropSelect);
if (!dr.HasRows)//this is fine
{
System.Console.WriteLine("ERROR: Failed to load property data");
return;
}
//but it errors out below
System.Console.WriteLine(dr.GetString(0));
Thanks for any help!!!