I am retrieving data from SQL Server using code that has worked in other projects. When getting to the ExecuteReader() section, it all works fine.... BUT if you step over into the using block, the reader goes from having data in the returned section to saying
enumeration yielded no results
This only needs to return 1 row.
SO when I step past this line:
using (SqlDataReader reader = cmd.ExecuteReader())
The object is populated data from the query. The values match that in the table.
When I go one more step I hover over reader in the above line and it says
enumeration yielded no results
I cannot see any issues with my code and I'm stuck.
To make matters more confusing if I search for a ordinal position by name it works. Meaning the metadata of the column names seems to be available but not the data.
All I want to do is retrieve a row of data from a SQL Server table and populate an object class defined in my app.
I have tried returning a simple select all, removing the parameters etc and it seems to not have any effect.
public ConnectionDetails GetCustomerConnectionDetails(string Customer_GUID)
{
ConnectionDetails _conDetails = new ConnectionDetails();
try
{
using (SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["someconstring"].ToString()))
using (SqlCommand cmd = new SqlCommand(@"select Customer_ID ,Customer_Connection_Alias ,Customer_GUID from dbo.Connections where Customer_GUID = @Customer_GUID", sqlConn))
{
//[Customer_ID] ,[Customer_Connection_Alias] ,[Customer_GUID]
cmd.Parameters.Add("@Customer_GUID", SQL.SqlDbType.NVarChar).Value = Customer_GUID;
sqlConn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
_conDetails.Customer_ID = reader.SqlStringValueNullCheck(reader.GetOrdinal("Customer_ID"));
_conDetails.Customer_Alias = reader.SqlStringValueNullCheck(reader.GetOrdinal("Customer_Connection_Alias"));
_conDetails.Customer_GUID = reader.SqlStringValueNullCheck(reader.GetOrdinal("Customer_GUID"));
_conDetails.Connection_Status = reader.SqlStringValueNullCheck(reader.GetOrdinal("Connection_Status"));
_conDetails.Connection_Date_Time = reader.SqlDateTimeValueNullCheck(reader.GetOrdinal("Connection_Date_Time"));
_conDetails.Connection_GUID = reader.SqlStringValueNullCheck(reader.GetOrdinal("Connection_GUID"));
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return _conDetails;
}
Any help would be appreciate. Cheers!
reader.Read()reads the next row of data in memory. And you can access the data of that row only by using various GetMethods such asGetString,GetInt32etc. Have look here