Link to home
Start Free TrialLog in
Avatar of ndoorsinteractive
ndoorsinteractive

asked on

Reading data from SQL - C# and ASP.NET

I'm trying to read data from MS-SQL and assign returned the value in the ASP.net page which is text field.

It keeps showing like
"Invalid attempt to read when no data is present.."
Exception Details: System.InvalidOperationException: Invalid attempt to read when no data is present.

when I query those statement in MS-SQL, data is surely there..

I'm trying to figure it out. I could not resolve yet..
Can I assign like

EmployeeFirstName.Text = sdr["fname"] == DBNull.Value ? String.Empty : ((string)sdr["fname"]).Trim();

Thanks in advance.

my current code in c# FYI
===========================================
using (var cmd = conn.CreateCommand())
      {
         conn.Open();
         cmd.CommandText = "SELECT * FROM [Employee] with (nolock) where eID=@eID";
                    cmd.Parameters.Add(new SqlParameter("eID", SqlDbType.Int, 0) { Value = eIDOf.SelectedItem.Value });
                    cmd.Prepare();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        EmployeeFirstName.Text = sdr["fName"] == DBNull.Value ? String.Empty : ((string)sdr["fName"]).Trim();
                        sdr.Close();
                    }

    }






===========================================
ASKER CERTIFIED SOLUTION
Avatar of Beartlaoi
Beartlaoi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial