Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Reading data from SQL - C# and ASP.NET

Posted on 2013-06-04
1
446 Views
Last Modified: 2013-06-04
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();
                    }

    }






===========================================
0
Comment
Question by:ndoorsinteractive
1 Comment
 
LVL 9

Accepted Solution

by:
Beartlaoi earned 500 total points
ID: 39220478
You need to call sdr.Read() before you access its columns.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question