?
Solved

SqlAdapters and retrieving data using Asp.net 2.0 C#

Posted on 2006-03-20
2
Medium Priority
?
327 Views
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">
        <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
0
Comment
Question by:ohmstor
2 Comments
 
LVL 22

Accepted Solution

by:
Jason Scolaro earned 1400 total points
ID: 16238717
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
 

Author Comment

by:ohmstor
ID: 16239043
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 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