Accessing data from my SqlDataSource in the code behind C# .net

14,089

First, you really should graduate from using SqlDataSource to the classes available in the SqlClient namespace at some point so keep that in mind.

Here's the code to do it though:

DataView dview = CType(yourSqlDataSource.Select(DataSourceSelectArguments.Empty), DataView);
string str = "";

foreach(DataRow drow dview.Table.Rows)
{
    str += drow("yourcol1").ToString() + "<br />";
}
Share:
14,089
Admin
Author by

Admin

Updated on June 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a contact page where there is a datalist of people and if you click on one of them you get a contact form that I want to send to that particular person.

    I use sqldatasource dscontactemail to get information about that person to place on the contact form but how do I get information out of dscontactemail from the code behind for when I'm ready to send that mail?

    I put a formview on the page to display the person's picture and I can get whatever I want from that dscontactemail with <%#Eval("email") for example, but how do I get that from the code behind?

    I tried a hidden field but it didn't work.

    Any other ways to access the SqlDataSource on a page from the code behind?

  • Jack Marchetti
    Jack Marchetti almost 15 years
    I agree on the graduating part. Not a fan of using DataView though.
  • Anas Naim
    Anas Naim about 10 years
    But there is one problem, CType is in VB not C#