Solved

Reading data from SQL - C# and ASP.NET

Posted on 2013-06-04
1
448 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
[X]
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
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

690 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