0

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

1 Answer 1

3

The SqlDataSource is 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 - SqlConnection and SqlCommand - to read that value - something like:

string sqlStmt = "SELECT FirstName FROM dbo.YourTable WHERE ID = @ID";

using(SqlConnection conn = new SqlConnection(your-connection-string-here-))
using(SqlCommand cmd = new SqlCommand(sqlStmt, conn))
{
   cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 4044;

   conn.Open();
   string firstName = cmd.ExecuteScalar().ToString(); 
   conn.Close();
}

The ExecuteScalar call works only if you want to read a single row, single column value - like here. Otherwise you need to use either the SqlDataReader, or use the DataTable and a SqlDataAdapter to fill that data table (if you have multiple rows).

Update: if you want to use a SqlDataAdapter instead - do this:

public DataTable LoadData()
{
   DataTable result = new DataTable();

   string sqlStmt = "SELECT ID, FirstName, LastName, Country " + 
                    "FROM dbo.YourTable";

   using(SqlConnection conn = new SqlConnection(your-connection-string-here-))
   using(SqlCommand cmd = new SqlCommand(sqlStmt, conn))
   {
       SqlDataAdapter dap = new SqlDataAdapter(cmd);
       dap.Fill(result);
   }

   return result;
}

When you call this method, you'll get back a DataTable that contains the columns you've defined in your SQL statement, and all rows from the database table.

DataTable myData = LoadData();

Now, you can iterate over the rows and get the FirstName value for each row:

foreach(DataRow row in myData.Rows)
{
     string firstName = row["FirstName"].ToString();
     // do whatever you need to do with the first name
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is something like ds.Tables["tablename"].Rows[rownumber]["columnname"] possible? Even this did not work but someone recommended it to me.
@furqan sehgal: if you use a SqlDataAdapter and fill a DataTable - yes! With a SqlDataSource - no.
Please advise how to add sqlDataAdapter and Datatable and how to bind them. Is there somehting like binding source we have in winforms. Also please give statement that I would need to do my required operation.
@furqan sehgal: updated my response - what do you need to do with the "FirstName" value(s) after you've loaded it??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.