Monday, March 19, 2012

Displaying select results on the page

I have the following select statement on my page:

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:prbc_hrConnectionString %>" SelectCommand="SELECT emp_lname +', ' + emp_fname + ' ' + emp_minitial + '.' AS emp_fullname FROM employee WHERE (emp_username = @.emp_username)"> <SelectParameters> <asp:SessionParameter Name="emp_username" SessionField="Username" Type="String" /> </SelectParameters>

I want to say

"Welcome, emp_fullname"

at the top of the page, but can't figure out how to write the results to the page. I am moving to ASP.NET 2.0 from PHP and am banging my head against the wall trying to figure out how to do these little things.

I appreciate any help you can give.

I am assuming that you don't want to use the sql membership/profile system, and want to implement your own. You should read more about those to see if they would be a better fit for you, as both do a lot of things automatically for you already.

However, to solve your immediate problem, since the sqldatasource will return a set of records, you must use a control that knows how to handle multiple records and iterate through them. Those controls would be things like formview, detailsview, datarepeater, datalist, and gridview (I'm sure I missed a few). I'll show you how to use the datarepeater.

Drop the datarepeater on your form. Change the DataSourceID to SqlDatasource1. Switch to the source view, and add an item template with what you want to display for each record (In your case, only 1). So something like this should be in your .aspx file:

<asp:RepeaterID="Repeater1"runat="server"DataSourceID="SqlDatasource1">

<ItemTemplate><%# Eval("Username") %></ItemTemplate>

</asp:Repeater>

You can do the same thing similarly using a datalist. Drop datalist control on your page, assign the datasource. Refresh the schema. Edit item template, which should already contain a couple labels.

With that said, what I would probably do if I needed to display the username like that (Possibly across multiple pages), I would assign another session variable to hold the emp_fullname. Then on any page you wanted to display it, drop a label on it, and programmatically set the label contents during page load like:

label1.text=Session("emp_fullname").

Since the emp_fullname shouldn't change during a session, and is probably used on multiple pages, then you only need to hit the database when they first log in. It's much more efficient than hitting it every time. Although you could enable the sqldatasource's caching to help with that as well, it's still much more work for asp.net to instantiate a sqldatasource, databind it to a data control, etc.

If you still want to use the sqldatasource control (because you'd like to enable caching, etc), then you can also programmatically retrieve the dataset in page_load and set a label as well. I believe the syntax would be similiar to (This might not run/be syntaxically 100% correct):

if not ispostback() then

dim dsv as datasourceview=sqldatasource1.select(datasourcearguements.empty)

label1.text=dsv.ToTable.Rows(0).Column(0)

end if

The .ToTable.Rows(0).Column(0) I'm not really sure about, but you can access it, just probably not exactly that way.

No comments:

Post a Comment