-1

I am trying to fetch the data from a SQL Server database table through Asp.net. I have used the query to Select the first row of the table using below code

SqlCommand cmd = new SqlCommand("Select top 1 * from Table1 Order by First_name", con);

How am I able to fetch the data from the first row of column named "attendance"?

I know it is something to do with the SqlDataReader, but I am not sure how to use it. My aim is to add two columns of the first row "attendance" and "Percentage" which is of type float. Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Code123
  • 99
  • 7
  • 1
    There is something called [Google...](https://www.google.nl/search?source=hp&ei=WB-VWveFO4LRwAKC4rCwBw&q=c%23+sql+tutorial&oq=c%23+sql+tutorial&gs_l=psy-ab.3...1092.9665.0.9938.0.0.0.0.0.0.0.0..0.0....0...1c.1.64.psy-ab..0.0.0....0.YFKHTIFZlis) – VDWWD Feb 27 '18 at 09:05
  • @ VDWWD - https://www.codeproject.com/Messages/3848003/Re-MoveNext-is-not-moving-next.aspx – Code123 Mar 01 '18 at 08:43

1 Answers1

1

ADO.NET has lots of nuances and edge cases; while you can do this with ExecuteDataReader / ExecuteScalar, you might find it easier to defer all this stuff to a tool like "dapper". Then you can do things simply like:

class YourType {
    // properties that match your columns
    public string Name {get;set;}
    public double Attendance {get;set;}
}
...
var row = con.QuerySingleOrDefault<YourType>(
    "Select top 1 * from Table1 Order by First_name");

this also allows simple but correct parameterization, and a whole range of other features that frequently cause problems - with the library dealing with all the ugly implementation details of ADO.NET.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900