-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.

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.