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.InvalidOperationExc eption: 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"]).Tri m();
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"]).Tri m();
sdr.Close();
}
}
========================== ========== =======
It keeps showing like
"Invalid attempt to read when no data is present.."
Exception Details: System.InvalidOperationExc
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"]).Tri
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"]).Tri
sdr.Close();
}
}
==========================
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.