Saturday, March 24, 2012

Populate string array with a SQL row

Can someone tell why I get an "Invalid attempt to read when no data is present. " error?

Here is the code snipplet . . .


SqlCommand DBCommand = new SqlCommand("SELECT * FROM Addresses WHERE AddressID='"+ID.ToString()+"'", DBConn );
try
{
DBConn.Open();
dr = DBCommand.ExecuteReader( System.Data.CommandBehavior.CloseConnection ) ;

}
catch
{
values = null ;
return values ;
}
values[0]=dr["AddressStreet1"].ToString() ;
values[1]=dr["AddressStreet2"].ToString() ;
values[2]=dr["City"].ToString() ;
values[3]=dr["StateProvinceID"].ToString() ;
values[4]=dr["ZipPostalCode"].ToString() ;
values[5]=dr["CountryID"].ToString() ;

Thanks in advance!You need to read the first record of the recordset:

dr.read();
It works except the first column is gone. Why is that?
Disregard my last message - it was my error.

0 comments:

Post a Comment