SqlAdapters and retrieving data using 2.0 C#

Posted on 2006-03-20
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,

Question by:ohmstor
    LVL 22

    Accepted Solution

    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

    Author Comment

    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?


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now