Solved

Reading data from SQL - C# and ASP.NET

Posted on 2013-06-04
1
441 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
Comment Utility
You need to call sdr.Read() before you access its columns.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 discusses moving either the default database or any database to a new volume.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now