Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2009, 06:53 PM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default Working with a database

I have a database with three tables, CITIES (Id, Name), MEMBERS (Id, Name, Email), COMMENTS (Id, CityId, Comments, MemberId). On the site, there is a drop down which users can select a City. Based on the city seletion, I would like all the comments for that city to appear. How do I output the Member's Name (not Id), Email Address, and Comments in a customizable table for the users to see?


I am new to both ASP.NET and databases, so any information would be greatly appreciated.
 
Old February 26th, 2009, 12:08 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The SQL Code is trivial really. Something like this would do:

sql Code:
SELECT
       m.Name,
       c.Comments
   FROM Members m
   INNER JOIN Comments c ON m.ID = c.MemberID
   WHERE c.CityID = @CityID

After executing this SQL you could simply populate a data table and then bind it to a gridview something like:

gridView1.DataSource = somedatatable;
gridView1.DataBind();


hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old February 27th, 2009, 11:42 AM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default Almost but not quite

I have the following code on my page:

<
asp:DropDownListID="DropDownList1"runat="server"AutoPostBack="True"
DataSourceID="SqlDataSource1"DataTextField="Country"DataValueField="Id"AppendDataBoundItems="true">
<asp:ListItemValue="">Please select a county</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:SiteConnectionString1 %>"SelectCommand="SELECT [Country], [Id] FROM [Countries] ORDER BY [Country]">
</asp:SqlDataSource>
<asp:DropDownListID="DropDownList2"runat="server"DataSourceID="SqlDataSource2"DataTextField="Name"DataValueField="Id"AppendDataBoundItems="true"AutoPostBack="True"Visible="False">
</asp:DropDownList>
<asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:SiteConnectionString1 %>"SelectCommand="SELECT [Name], [CountryId], [Id] FROM [Cities] WHERE ([CountryId] = @CountryId) ORDER BY [Name]">
<SelectParameters>
<asp:ControlParameterControlID="DropDownList1"Name="CountryId"PropertyName="SelectedValue"Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:RepeaterID="Repeater1"runat="server">
<ItemTemplate>
<pclass="Contacts"><asp:LiteralID="Literal1"runat="server"Text='<%# Eval("MemberName") %>'></asp:Literal>
<br/><asp:LiteralID="Literal2"runat="server"Text='<%# Eval("Email") %>'></asp:Literal>
<br/><asp:LiteralID="Literal3"runat="server"Text='<%# Eval("Summary") %>'></asp:Literal></p>
</ItemTemplate>
</asp:Repeater>

with this code on the code behind page

If Page.IsPostBack Then
Using myDataContext AsNew SiteDataContext()
Dim allReviews = From r In myDataContext.Reviews _
Where r.CityId = DropDownList2.SelectedValue _
SelectNewWith {r.Member.MemberName, r.Member.email, r.Summary}
Repeater1.DataSource = allReviews
Repeater1.DataBind()
EndUsing
EndIf

I get the results that I want, the User Name, their email address, and the review. However, the review continues off of the page, out of the main content area defined on the other pages. Why is it doing this? How do I fix it? And is using a literal the best way to display the information?
 
Old February 27th, 2009, 11:53 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Unless you are accessing the Literal in the code behind for something, you dont need to place the text from your database into a control. The following code would suffice:

aspnet Code:
...
<ItemTemplate>
<%# Eval("MemberName") %> <br />
<%# Eval("Email") %> <br />
<%# Eval("Summary") %>
</ItemTemplate>
...

As far as your content breaking the page bounds I am not sure what is going on with that. I have had instances in the past where a literal did this but was able to correct it with something like:

<div style="width:750px"><asp:Literal ... /></div>

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old February 27th, 2009, 03:12 PM
Authorized User
 
Join Date: Jan 2009
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. That works great. The review only runs out of the content area if there is no space between the words. So if I were to enter aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa it runs over regardless of the properties that I set int the div tag. Otherwise it wraps just fine. Thanks again.
 
Old February 27th, 2009, 03:14 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Glad it worked out for you! =]

-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================




Similar Threads
Thread Thread Starter Forum Replies Last Post
window.opener working .... not working alyeng2000 Javascript How-To 5 January 5th, 2007 08:05 AM
Web.Config..Working or Not Working peace95 ASP.NET 1.0 and 1.1 Basics 1 September 18th, 2006 06:53 AM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM
Get Working Copy... not working Enkiel Classic ASP Basics 0 April 21st, 2004 01:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.