SqlAdapters and retrieving data using Asp.net 2.0 C#

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">
        <SelectParameters>
            <asp:QueryStringParameter Name="UserName" QueryStringField="UserName" Type="String" />
        </SelectParameters>
 </asp:SqlDataSource>



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,

Ohmstor
ohmstorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ohmstorAuthor Commented:
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?

Thanx
Ohmstor
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.