I have created a datasource to connect with SQL Server database. It works fine when I connected it with GridView. I need to read certain item (say FirstName) and store the value to a variable.
How can I use this datasource? Could you give me the statements for that?
Thanks
The
SqlDataSourceis intended as what the name implies – a data source for data binding. It is not a way to get individual values from a database table.If you need to read a single value, you should use straight ADO.NET –
SqlConnectionandSqlCommand– to read that value – something like:The
ExecuteScalarcall works only if you want to read a single row, single column value – like here. Otherwise you need to use either theSqlDataReader, or use theDataTableand aSqlDataAdapterto fill that data table (if you have multiple rows).Update: if you want to use a
SqlDataAdapterinstead – do this:When you call this method, you’ll get back a
DataTablethat contains the columns you’ve defined in your SQL statement, and all rows from the database table.Now, you can iterate over the rows and get the
FirstNamevalue for each row: