We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


SqlAdapters and retrieving data using Asp.net 2.0 C#

ohmstor asked
Medium Priority
Last Modified: 2008-01-16
Hello Experts,

I am trying to retrieve data from a database, however I am unable to do that. I am trying to use a SqlDataSource and then either a datareader or dataset to retrieve information. Currently it's set to use a datareader.

I currently have the following code for a SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyLocalSQLServer %>"
        ProviderName="<%$ ConnectionStrings:MyLocalSQLServer.ProviderName %>" SelectCommand="SELECT [UserId] FROM [vw_aspnet_Users] WHERE ([UserName] = @UserName)" DataSourceMode="DataReader">
            <asp:QueryStringParameter Name="UserName" QueryStringField="UserName" Type="String" />

Once I click a button I run the below code:
System.Data.SqlClient.SqlDataReader reader = (System.Data.SqlClient.SqlDataReader) SqlDataSource1.Select(DataSourceSelectArguments.Empty);

        if (reader.Read())
            Labelout.Text = reader[0].ToString();

However reader is always null and I believe it's because I need to send in a value for my query. How do I send the value? I also do not want to "bind" by database. I would also appreciate if someone could tell how to also after retrieving data on how to modify it.

Thank You,

Watch Question

Top Expert 2006
Hi ohmstor,

To access the SelectParameters, try the following code:

SqlDataSource1.SelectParameters["UserName"].DefaultValue = "Something";
System.Data.SqlClient.SqlDataReader reader = (System.Data.SqlClient.SqlDataReader) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
if (reader.Read())
    Labelout.Text = reader[0].ToString();

Also, you won't be able to change the values directly on the DataReader, since the object is only used for retrieving values, whereas the DataSet would allow you to retrieve the data and manipulate it.

Good luck!
-- Jason

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanx Jason it worked!

In another section of my program I will need to modify the contents of a database table. How can I do that most efficiently? Do I also create another SqlDataSource which is set to use datasets? How can I use datasets to read and modify data?

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.