Solved

Reading data from SQL - C# and ASP.NET

Posted on 2013-06-04
1
443 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

770 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