This question already has an answer here:
Query returns no records to show on my webpage
(1 answer)
Closed 9 years ago.
I have a query that I have run in Access that doesn't seem to run when I execute it on my website site. Here's the code for the webpage that I want the database to populate content for:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/TravelJoansDB.accdb"
SelectCommand="SELECT * FROM [Table2] INNER JOIN BlogEntryItems ON Table2.ID=BlogEntryItems.BlogID AND Table2.ID=#ID">
<SelectParameters>
<asp:QueryStringParameter Name="ID" QueryStringField="Table2.ID" Type="Decimal" />
</SelectParameters>
</asp:AccessDataSource>
<asp:DataList ID="DataList1" DataSourceID="AccessDataSource1"
runat="server" ItemStyle-CssClass="picTableStyle"
DataKeyField="Table.2ID">
<ItemStyle></ItemStyle>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "PlaceImages/" + Eval("Image") %>' /><br />
<asp:Label ID="Label1" CssClass="picCaptionStyle" runat="server" Text='<%# Eval("Caption") %>' />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" class="picBodyText" runat="server" Text='<%# Eval("PicStory") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
When I take out the join, it works fine. I add the join and it doesn't give me any error or anything, it just seems as though there are no results for it to show on my website. Any ideas?
Note that I haven't worked on AccessDataSource. However, looking at the docs - here is what you should look into
Change the query to use ? instead of #ID
SelectCommand="SELECT * FROM [Table2] INNER JOIN BlogEntryItems ON Table2.ID=BlogEntryItems.BlogID AND Table2.ID= ?"
Because the AccessDataSource control extends the SqlDataSource class
and uses the System.Data.OleDb provider, you specify parameter
placeholders using the "?" placeholder character.
ref: http://msdn.microsoft.com/en-us/library/8e5545e1%28v=vs.100%29.aspx
If the above doesn't work, omit the data type (Decimal) of the parameter OR specify a relevant type such as Integer instead.
Related
I am using a listview to display datas from 3 tables in mysql.
My tables are,
theatredet(theaterid,theatername,locationid)
location(locationid,locationname)
screendet(screenname,theaterid,seatsavailable)
I want to display datas from screendet on the basis of theaterid from the table theatredet,i can only able to disply single data from screendet,there are multiple datas on that table with respect of theaterid.
My query is,
string query = "SELECT `theatredetails`.*,`Location`.`LocationName`,
`screendetails`.`ScreenName`,`screendetails`.`SeatsAvailable`
FROM `theatredetails`INNER JOIN `screendetails`
ON `screendetails`.`TheatreDetailsId` = `theatredetails`.`TheatreDetailsId`
INNER JOIN `location` ON `location`.`LocationId`=`theatredetails`.`LocationId`;";
My aspx
<asp:TextBox ID="TextBox6" runat="server" Text='<%#Bind("ScreenName") %>'></asp:TextBox>
<asp:TextBox ID="TextBox7" runat="server" Text='<%#Bind("SeatsAvailable") %>'></asp:TextBox>
Assuming you have already tested your query on MySql and it is returning intended number of rows.
You can populate your listview using the following code (I am using SqlDataSource to bind the ListView, you can use any other method)
<asp:ListView ID="lstviewScreens" runat="server" DataSourceID="ScreensSqlDataSource" >
<LayoutTemplate>
<table id="itemPlaceholderContainer" runat="server" >
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="background-color: #E0FFFF;">
<td>
<strong>Theater Name:</strong><asp:Label ID="lblTheaterName" runat="server" Text='<%# Eval("theatername") %>' />
<br />
<strong>Screen Name:</strong><asp:Label ID="lblScreenName" runat="server" Text='<%# Eval("ScreenName") %>' />
<br />
<strong>Seats Available</strong><asp:Label ID="lblSeatsAvailable" runat="server" Text='<%# Eval("SeatsAvailable") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ScreensSqlDataSource" runat="server"
ConnectionString="Your connection String"
SelectCommand="SELECT theatredetails.*,Location.LocationName,
screendetails.ScreenName,screendetails.SeatsAvailable
FROM theatredetails INNER JOIN screendetails
ON screendetails.TheatreDetailsId = theatredetails.TheatreDetailsId
INNER JOIN location ON location.LocationId=theatredetails.LocationId">
</asp:SqlDataSource>
This will output your data in tabular format, however you can customize LayoutTemplate to change the generated html, even you can use GroupTemplate to group the data.
I need my own posts and comment on the page in my project, something similar to Facebook. I succesfully created posts by using sql table, sqldatasource and repeater. But I have a problem with comments.
I created sql table Comments, there is a column with Post ID. This is my code:
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:MotWebConnectionString %>"
SelectCommand="SELECT * FROM [Posts]
JOIN [Users] ON [Users].[UserName] = [Posts].[UserName]
JOIN [Comments] ON [Comments].[PostId] = [Posts].[PostId]
WHERE ([GroupId] = #GroupId) ">
<SelectParameters>
<asp:QueryStringParameter Name="GroupId" QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource3" >
<ItemTemplate>
<table style="width: 100%;">
<tr>
<td width="10%">
<asp:Image ID="Image2" runat="server" Width="64px" Height="64px" ImageUrl='<%# Eval("ProfilePicture")%>' />
</td>
<td align="left" width="90%" >
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("UserName", "~/_/Profile.aspx?Username={0}")%>'><p><asp:Literal ID="Literal2" runat="server" Text='<%# Eval("Username")%>'></asp:Literal></p></asp:HyperLink>
</td>
</tr>
</table>
<asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Text")%>'></asp:Literal>
<br />
<asp:Literal ID="Literal4" runat="server" Text='<%# Eval("CommentText")%>'></asp:Literal>
<asp:FormView ID="FormView2" runat="server" DataSourceID="SqlDataSource4" DefaultMode="Insert" >
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="Insert" />
</InsertItemTemplate>
</asp:FormView>
</ItemTemplate>
<SeparatorTemplate>
<br />
<br />
<br />
</SeparatorTemplate>
</asp:Repeater>
But it shows only posts, where is at least 1 comment and if there are more comments than 1, the posts is twice or more times on the page.
And another problem is, that I don't know how to insert right Post ID into the Comments table. It is probably by using Parameter...
My database schema (I did't know how to create it in SQL Management Studio, so it is in VS):
http://img685.imageshack.us/img685/1416/vstiekd.jpg
Please could anyone help me with this:
How to view right comments under the post and view also posts without any comment
How to insert the comment with right Post ID
I will be very thankfull for any kind of help
I'm a beginner asp.net developer
Trying to create my first simple news portal pages.
Here is what I have:
Database
Admin_News.aspx to add news into database
Default.aspx to display all news with the title linkable
NewsDetails.aspx to display the details by ID when they click on the title in Default.aspx
How I did That:
in Default.aspx I used this code:
<div>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table border="1">
<tr>
<td>
<b>title</b>
</td>
<td>
<b>news</b>
</td>
<td>
<b>imges</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink runat="server" ID="hl" NavigateUrl='<%#"~/NewsDetails.aspx?id=" + Eval("id")%>' Text='<%# Eval("title") %>'></asp:HyperLink>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "news")%>
</td>
<td>
<img alt="" src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MoneyHolderConnectionString %>"
SelectCommand="SELECT [id], [title], [news], [imageurl], [detail] FROM [News]"></asp:SqlDataSource>
</div>
NewsDetails.aspx code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MoneyHolderConnectionString %>"
SelectCommand="SELECT [title], [news], [imageurl], [detail] FROM [News] WHERE ([id] = #id)">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int64" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<h1 align="right"><br />
<strong><p style="color:#000">
<%# DataBinder.Eval(Container.DataItem, "title")%>
</strong></h1><p/><br />
<br />
<p align="center"><img src='<%# DataBinder.Eval(Container.DataItem, "imageurl")%>'/>
<p/>
<br />
<br />
<p id="detail" align="right" style="font-size:25px"><%# DataBinder.Eval(Container.DataItem, "detail")%><p/>
<br />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
so what I'm doing is displaying the article according to the ID in .aspx?id= in the url.
everything was working fine.. until I used the url routing.
I changed/added some codes to change the structure of the url I don't want it to appear like ~/NewsDetails.aspx?id=1
I want to be like ~/News/1 instead with the same result.
So I have added this code to Global.asax:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("RouteForNews", "News/{id}", "~/NewsDetails.aspx");
}
and I added this code to the NewsDetails.aspx.cs code behind:
string id = Page.RouteData.Values["id"].ToString();
and I changed the NavigateUrl in NewsDetails.aspx to:
NavigateUrl='<%#"~/News/" + Eval("id")%>'
now when I open Default.aspx the news appear with the titles linkable to ~/News/"id number", when i click on a title, NewsDetails.aspx opens with the link of /News/idnumber but no data inside..its empty i only can see the design of the master page.
I would appreciate any help from you what should I do to display the news, the id value in the url it goes to string id variable but I don't know how to pass it to the sql query (i'm not sure if this is the problem)
You should not use the asp:QueryStringParameter in the asp:SqlDataSource in the NewsDetails page, as your parameter is no longer in the QueryString. A QueryString is the part of URL that comes after the question mark.
Use this link to know how to pass parameters to select statement:
How to pass variable to SelectCommand of SqlDataSource?
connectionstring="<%$ ConnectionStrings:MyConnection %>"
selectcommand="SELECT Name, Age FROM ContactInfo where
State=#StateCode">
I have two Formviews on the same page. I can use FindControl on the first without issue, but it always fails on the second one.
I am using the ItemTemplate for both, both default to ReadOnly, both are bound to SQLDataSources (different ones) but I cannot for the life of me work out why FindControl works for one and not the other.
I have removed a lot of the plain text from the code below.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Apps] WHERE ([AppID] = #AppID)" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="AppID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1>
<asp:Label ID="AppNameLabel" runat="server" Text='<%# Bind("AppName") %>' />
<asp:Label ID="VersionLabel" runat="server" Text='<%# Bind("Version") %>' /> for
<asp:Label ID="OSLabel" runat="server" Text='<%# Bind("OS") %>' />
</h1>
<p>Text here</p>
<p><asp:TextBox ID="LicenseTextBox" runat="server"
Text='<%# Eval("License")%>'
TextMode="MultiLine" Width="800px" Rows="25" ReadOnly="True"></asp:TextBox></p>
<asp:HiddenField ID="AppLocation" runat="server" ViewStateMode="Inherit" Value='<%# Bind("AppLocation") %>'/>
</ItemTemplate>
</asp:FormView>
<p><asp:Literal ID="SizeLiteral" runat="server"></asp:Literal></p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Installations] WHERE (([AppID] = #AppID) AND ([Username] = #Username))" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
<asp:FormParameter FormField="Username" Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="DLFormView" runat="server" DataSourceID="SqlDataSource4"
DataKeyNames="AppID,Username">
<ItemTemplate>
<p> <asp:Label ID="DLAppNameLabel" runat="server" />
<br />
<br />
<asp:Label ID="NumberOfInstallations" runat="server" Text='<%# Bind("Installations") %>'></asp:Label>
<br />
<asp:HiddenField ID="TotalInstallations" runat="server" />
Number of New Installations:
<asp:DropDownList ID="NumberOfNewInstallations" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="TotalNumberOfInstallations" runat="server" Text="Label"></asp:Label>
</p>
</ItemTemplate>
</asp:FormView>
And the FindControl coding is as follows...
TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");
HiddenField AppLocCode = (HiddenField)FormView1.FindControl("AppLocation");
Label AppNameCode = (Label)FormView1.FindControl("AppNameLabel");
These always work...
Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");
This always returns null. I've read a million bosts about the controls not being rendered until databinding has completed, but surely if the two FOrmViews are set up the same way, the result should be the same.
Any help would be much apreciated :)
Matt :)
I'd like to plead stupidity and but also provide some background to how I've used what I've learnt from my stupidity.
My SqlDataSource for DLFormView was returning records, of course I then used slightly different values by accident and so no records were returned. I only figured this out by making the ItemTemplate section of the form view only contain unbound (basically plain text) data. As nothing displayed, this pointed towards the page using a different tempalte. When I put a test line in the EmptyDataTemplate, it displayed, confirming there was nothing returned by the SqlDataSource. Perhaps a School boy error not putting something in this template during development.
As it happens I need to use the EmptyDataTemplate anyway, I just hadn't got to this point. As only one template is rendered at a time, I have been able to use the same ID names in both templates. This means there is no need to test for which template is being used. (although empty template is used when DataItemsCount = 0, otherwise you can test for CurrentMode for the remaining templates.)
So a big fail on my part, but I hope people can learn from my mistake.
Matt :)
I am helping my daughter build an asp.net website that has an Access database. We are using a DataList with a couple fields inside the ItemTemplate. The one that is giving me trouble is the Hyperlink. The code is below. When the page loads the hyperlink renders like this:
<a id="ContentPlaceHolder1_DataList1_lnk_6" href="#http://www.washingtonfaire.com/#" target="_blank">Washington Midsummer Renaissance Faire</a>
But when I click on the link, it tries to navigate to "http://localhost:1852/BOOMPiratesB/Raids.aspx#http://www.washingtonfaire.com/#"
What are we doing wrong?
Here is our code.
<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
<asp:HyperLink id="lnk" runat="server" NavigateUrl='<%# Eval("Link") %>' Target="_blank" Text='<%# DataBinder.Eval(Container.DataItem, "VenueName")%>'>
</asp:HyperLink>
<br />
<asp:Label ID="DateTextLabel" runat="server" Text='<%# Eval("DateText") %>' />
<br />
<asp:Label ID="CityStateLabel" runat="server" Text='<%# Eval("CityState") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/my.mdb"
SelectCommand="SELECT [VenueName], [Link], [DateText], [Season], [DateStart], [CityState] FROM [qpgRaid]">
</asp:AccessDataSource>
I cannot see where the extra # signs are coming from. They don't appear to be in the field in the table.
It's very puzzling. And insight would be most appreciated.
Have a look at this question
I guess you are using an access hyperlink column to store the link. If you convert it to a text column it should start working fine, or you may have to strip the '#'s manually.