My ListView is displaying duplicates - c#

I am trying to allow a user of a buy and sell website to only be able to edit the products that they have put up for sale. I have got that, but the result is now that it shows duplicates. I am new to Stack Overflow and asp.net so apologies if I am not following the correct procedures.
<asp:SqlDataSource ID="itemsjoin" runat="server" ConnectionString = '<%$ConnectionStrings:itemsconnection%>' SelectCommand="SELECT itemsupdate.ID, itemsupdate.itemname, itemsupdate.price, itemsupdate.image, itemsupdate.description, itemsupdate.location, itemsupdate.productID, itemsupdate.userID, producttype.productname
FROM producttype, users, itemsupdate
WHERE ([userID] = #userID)">
<SelectParameters>
<asp:QueryStringParameter Name="userID" QueryStringField="userID" Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView ID="itemsdisplay" runat="server" DataSourceID = "itemsjoin" DataKeyNames="ID">
<ItemTemplate>
<div class="col-md-4">
<div class="itemtext">
</br>
<asp:Label ID="itemname" runat="server" Text='<%# Eval("itemname") %>'></asp:Label></br>
<asp:Label ID="itemprice" runat="server" Text='<%# Eval("price") %>'></asp:Label>
<asp:HyperLink ID="itemhyperlink" runat="server" NavigateUrl='<%#"itemediting.aspx?itemid="+Eval("ID")%>'>
<div class="imgright">
<asp:Image ID="imagepic" runat="server" ImageURL='<%# "../../files/"+Eval("image") %>' width="200px" /></asp:HyperLink>
</div>
</div>
</div>
</ItemTemplate>
</asp:ListView>

Just add DISTINCT in your query
SELECT DISTINCT itemsupdate.ID, itemsupdate.itemname, itemsupdate.price, itemsupdate.image, itemsupdate.description, itemsupdate.location, itemsupdate.productID, itemsupdate.userID, producttype.productname
FROM producttype, users, itemsupdate
WHERE ([userID] = #userID)

Related

ListViewDataItem does not contain a property with the name 'CommentId' when using DataBinder.Eval

I have a ListView which displays all the comments made by users on a specific news article.
I need the comment's and author's ids for each comment so I can show a delete button next to it (if it is made by the respective user).
The problem is that the line DataBinder.Eval(dataItem, "CommentId") throws the following error
System.Web.UI.WebControls.ListViewDataItem' does not contain a property with the name 'CommentId'.
I also tried changing from CommentId to Comments.CommentId, but with no luck.
What I am doing wrong?
Any help is appreciated.
Thanks!
In News.aspx
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="ListView1_ItemDataBound">
<ItemTemplate>
<span style="">
<table>
<tr>
<td style="width=60px">
<asp:Label Text='<%# Eval("UserName") + ":"%>' runat="server" ID="UserNameLabel" Font-Bold="true" /><br />
</td>
<td>
<asp:Label Text='<%# Eval("body") %>' runat="server" ID="bodyLabel" /><br />
</td>
<td>
<asp:Button ID="DeleteCommentButton" runat="server" Text="Delete" OnClick="DeleteCommentButton_Click" />
</td>
</tr>
</table>
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:DefaultConnection %>' SelectCommand="SELECT Comments.CommentId, Users.UserId, Comments.body, Users.UserName FROM Comments INNER JOIN Users ON Comments.UserId = Users.UserId WHERE NewsId = #NewsId">
<SelectParameters>
<asp:Parameter Name="NewsId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
In News.aspx.cs:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
int commentId = (int)DataBinder.Eval(dataItem, "CommentId");
...
I solved it like this:
ListViewDataItem dataItem = e.Item as ListViewDataItem;
int commentId = (int)DataBinder.Eval(dataItem.DataItem, "CommentId");
Guid authorId = (Guid)DataBinder.Eval(dataItem.DataItem, "UserId");

'username' does not exist in it's current context

The ID is inside the ItemTemplate and it cannot be found in the code file.
<form id="form1" runat="server">
<asp:SqlDataSource ID="searchresults" runat="server"
ConnectionString='<%$ ConnectionStrings:AgileDatabaseConnection %>'
SelectCommand=" SELECT [userID], [userName], [firstName],[lastName],[password], [email] FROM [Users] WHERE ([email] LIKE '%' + #email + '%')">
<selectparameters>
<asp:querystringparameter querystringfield="searchquery" name="email" type="String"></asp:querystringparameter>
</selectparameters>
</asp:SqlDataSource>
<asp:ListView ID="displayitems" runat="server" DataSourceID="searchresults">
<itemtemplate>
<asp:label runat="server" associatedcontrolid="projectOwner" cssclass="col-md-2 control-label">Project Owner:</asp:label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="ProjectOwner" CssClass="form-control" /><br />
<asp:label runat="server" associatedcontrolid="projectOwner" cssclass="col-md-2 control-label">Scrum Master:</asp:label>
<div class="col-md-10">
<asp:SqlDataSource ID="ScrumMaster" runat="server" ConnectionString='<%$ ConnectionStrings:AgileDatabaseConnection %>' SelectCommand="SELECT userName FROM [Users]"></asp:SqlDataSource>
<asp:dropdownlist runat="server" id="usertype" DataSourceID="ScrumMaster" DataTextField="userName"></asp:dropdownlist><br />
</div>
</div>
<asp:label runat="server" cssclass="col-md-2 control-label">Email:</asp:label>
<asp:Label Text='<%#Eval("email") %>' runat="server" ID="emaillabel" /><br />
<asp:label runat="server" cssclass="col-md-2 control-label">UserName:</asp:label>
<asp:Label Text='<%#Eval("userName") %>' runat="server" ID="username" /><br />
<asp:label runat="server" cssclass="col-md-2 control-label">UserID:</asp:label>
<asp:Label runat="server" Text='<%#Eval("userID") %>' ID="UserID" CssClass="form-control" />
<br />
<div class="actions"></div>
<asp:Button Text="Add" runat="server" class="btn pull-right" ID="uploadbutton" OnClick="add_Click"></asp:Button>
</div>
</itemtemplate>
</asp:ListView>
<emptydatatemplate>
<span>No users match <asp:Label Text='' runat="server" ID="email" /> .</span>
</emptydatatemplate>
</form>
C# says it can't find 'userName'. Here's my backend code:
string ownerName = ProjectOwner.Text;
string IDuser = username.Text;
string IDdata = Session["userID"].ToString();
How do I get the userName value through?
The problem is that ListView can return more then one item, so code behind doesn't know about which of the itens controls (username and ProjectOwner) are you talking about.
If you want to do the same thing for EACH item, you can do like this:
protected void displayitems_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
string ownerName = ((TextBox)e.Item.FindControl("ProjectOwner")).Text;
string IDuser = ((Label)e.Item.FindControl("username").Text;
string IDdata = Session["userID"].ToString();
}
}

How do I populate multiple textboxes based on a dropdown list selection?

I have a form with a dropdown list and 3 textbox fields. The dropdown list is supposed to populate the 3 textboxes. So far I got it to populate 2 textboxes but I don't know how I can populate the 3rd one. Here is the HTML portion:
<EditItemTemplate>
<div style="height:50px;overflow:hidden;border:0px;width:100%">
<asp:Panel runat="server" Visible='<%# IsActive((string)Eval("Status")) %>'>
<div class="StatusAlert" style="float:left;"> Permit Picked Up</div>
</asp:Panel>
<div class="StatusAlert" style="background-color:#808080;float:left">Pending Pick Up</div>
</div>
<p>
Date:
<asp:Label ID="dateLabel" runat="server" Text='<%# Bind("PickupDate") %>'></asp:Label>
</p>
<asp:DropDownList ID="NameSelect" runat="server" DataSourceID="PickupNameSource" AppendDataBoundItems="true" DataTextField="Pickup_Name" DataValueField="PickupDrivers" OnSelectedIndexChanged="NameSelect_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="SELECT CONTRACTOR" Selected="True" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="PickupNameSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>"
SelectCommand="[ROW].SELECT_PickupContactsDDL" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<p>
Name:
<asp:TextBox ID="nameBox" runat="server" Text='<%# Bind("Pickup_Name") %>'></asp:TextBox>
</p>
Phone:
<asp:TextBox ID="phoneBox" runat="server" Text='<%# Bind("Pickup_Phone") %>'></asp:TextBox>
<p>
DL NO:
<asp:TextBox ID="DriversBox" runat="server" Text='<%# Bind("DriversNumber") %>'></asp:TextBox>
</p>
<p>
Contractor:
<asp:DropDownList ID="contractorList" runat="server" AutoPostBack="false" DataSourceID="SelectContractorDB" SelectedValue='<%# Bind("ContractorID") %>' DataTextField="Company" DataValueField="ContractorID"></asp:DropDownList>
<asp:SqlDataSource ID="SelectContractorDB" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" SelectCommand="SELECT * FROM [ROW].[All_Contractor_Names]"></asp:SqlDataSource>
</p>
Here is the C# portion:
protected void NameSelect_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList NameSelect = pickupView.FindControl("NameSelect") as DropDownList;
TextBox nameBox = pickupView.FindControl("nameBox") as TextBox;
TextBox phoneBox = pickupView.FindControl("phoneBox") as TextBox;
TextBox DriversBox = pickupView.FindControl("DriversBox") as TextBox;
nameBox.Text = NameSelect.SelectedItem.ToString();
string[] selectedValues = NameSelect.SelectedValue.Split(',');
//phoneBox.Text = selectedValues[0];
//DriversBox.Text = selectedValues[1];
if (selectedValues.Length > 1)
{
phoneBox.Text = selectedValues[0];
DriversBox.Text = selectedValues[1];
}
else
{
phoneBox.Text = selectedValues[0];
DriversBox.Text = " ";
}
}
I had to modify the Stored procedure
CREATE PROCEDURE [ROW].[SELECT_PickupContactsDDL]
#DriversNumber varchar(50) = '%', #Pickup_Name varchar(500) = '%', #Pickup_Phone varchar(50) = '%'
AS
BEGIN
SET NOCOUNT ON;
SELECT
Pickup_Name, ISNULL(Pickup_Phone,0) + ',' + ISNULL(DriversNumber,0) AS PickupDrivers, pickupDate
FROM
ROW.PickupContacts
GROUP BY
Pickup_Name, pickupDate, Pickup_Phone, DriversNumber
ORDER BY
pickupDate DESC
END
In your dropdown select stored procedure, concatenate phoneBox and DriversBox as one value.
For Example,
123 - phoneBox 345 - DriversBox
123,345 - Final drop down list value
On Dropdown selection event, use Split method to show it in 3text boxes.
Stored Procedure:
ALTER PROCEDURE [ROW].[SELECT_PickupContactsDDL]
AS
BEGIN
SET NOCOUNT ON;
SELECT Pickup_Name, Pickup_Phone + ',' + DriversNumber AS [PickupDrivers], pickupDate FROM ROW.PickupContacts GROUP BY Pickup_Name, pickupDate, Pickup_Phone, DriversNumber ORDER BY pickupDate DESC
END
DESIGN:
<EditItemTemplate>
<div style="height:50px;overflow:hidden;border:0px;width:100%">
<asp:Panel runat="server" Visible='<%# IsActive((string)Eval("Status")) %>'>
<div class="StatusAlert" style="float:left;"> Permit Picked Up</div>
</asp:Panel>
<div class="StatusAlert" style="background-color:#808080;float:left">Pending Pick Up</div>
</div>
<p>
Date:
<asp:Label ID="dateLabel" runat="server" Text='<%# Bind("PickupDate") %>'></asp:Label>
</p>
<asp:DropDownList ID="NameSelect" runat="server" DataSourceID="PickupNameSource" AppendDataBoundItems="true" DataTextField="Pickup_Name" DataValueField="***PickupDrivers***" OnSelectedIndexChanged="NameSelect_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="SELECT CONTRACTOR" Selected="True" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="PickupNameSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>"
SelectCommand="[ROW].SELECT_PickupContactsDDL" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<p>
Name:
<asp:TextBox ID="nameBox" runat="server" Text='<%# Bind("Pickup_Name") %>'></asp:TextBox>
</p>
Phone:
<asp:TextBox ID="phoneBox" runat="server" Text='<%# Bind("Pickup_Phone") %>'></asp:TextBox>
<p>
DL NO:
<asp:TextBox ID="DriversBox" runat="server" Text='<%# Bind("DriversNumber") %>'></asp:TextBox>
</p>
<p>
Contractor:
<asp:DropDownList ID="contractorList" runat="server" AutoPostBack="false" DataSourceID="SelectContractorDB" SelectedValue='<%# Bind("ContractorID") %>' DataTextField="Company" DataValueField="ContractorID"></asp:DropDownList>
<asp:SqlDataSource ID="SelectContractorDB" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" SelectCommand="SELECT * FROM [ROW].[All_Contractor_Names]"></asp:SqlDataSource>
</p>
Code-Behind
protected void NameSelect_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList NameSelect = pickupView.FindControl("NameSelect") as DropDownList;
TextBox nameBox = pickupView.FindControl("nameBox") as TextBox;
TextBox phoneBox = pickupView.FindControl("phoneBox") as TextBox;
TextBox DriversBox = pickupView.FindControl("DriversBox") as TextBox;
nameBox.Text = NameSelect.SelectedItem.ToString();
string[] selectedValues = NameSelect.SelectedValue.Split(',');
phoneBox.Text = selectedValues[0];
DriversBox.Text = selectedValues[1];
}

Need help to sort my SqlDataSource from a string i have in my .cs file

On my site I have 2 tables where the first table sends a customerNr that should control what information is loaded into my second table.
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
string tempKundeNr;
tempKundeNr = e.CommandName;
}
tempKundeNr gets the correct information but here is my problem: sorting my sqldatasource after the string.
What I have below is currently not functioning, but it hopefully shows what I am aiming for.
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:XXXXX %>"
ProviderName="<%$ ConnectionStrings:XXXXX.ProviderName %>" SelectCommand="SELECT [XXXXX], [XXXXX], [XXXXX] FROM [XXXXX] WHERE ([OrderNr] = ?)">
<SelectParameters>
<asp:QueryStringParameter Name="OrderNr" QueryStringField="tempKundeNr" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Edit*
The table that sends the information i need to sort by:
<asp:DataList ID="OrdersList2" runat="server" CellPadding="4" DataKeyField="OrderNr"
DataSourceID="SqlDataSource2" ForeColor="#333333"
onitemcommand="DataList2_ItemCommand">
<ItemTemplate>
<table>
<tr>
<td>
<b>Ordre Nummer: </b>
<asp:Label ID="OrderNrLabel" runat="server" Text='<%# Eval("OrderNr") %>'/>
<br /><br />
<b>Kunde:</b>
<asp:Label ID="KundeLabel" runat="server" Text='<%# Eval("XXX") %>' />
<br />
<b>Ønsket afhentningstid:</b>
<asp:Label ID="HentwLabel" runat="server" Text='<%# Eval("XXX") %>' />
<br />
<b>Total Pris:</b>
<asp:Label ID="PrisLabel" runat="server" Text='<%# Eval("XXX") %>' />
<br /><br />
<asp:Button ID="btnwinfo" runat="server" CssClass="button" Text="Se Mere" CommandName='<%# Eval("OrderNr") %>' CommandArgument='<%# Eval("OrderNr") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Well, you need to put the parameter in where clause (see below):
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:XXXXX %>"
ProviderName="<%$ ConnectionStrings:XXXXX.ProviderName %>" SelectCommand="SELECT [XXXXX], [XXXXX], [XXXXX] FROM [XXXXX] WHERE [OrderNr] = #orderNr">
</asp:SqlDataSource>
Here I put #orderNr parameter.
Then you can pass this parameter in your button click handler:
SqlDataSource3.SelectParameters.Add("orderNr", DbType.SomeType, someValue);
and this parameter will be passed to you select query.
You even can specify you select query in code-behind (if necessary):
SqlDataSource3.SelectCommand = "SELECT [XXXXX], [XXXXX], [XXXXX] FROM [XXXXX] WHERE [OrderNr] = #orderNr";
Hope it will help.

How to use select parameters in html tags in ASP.NET

<asp:SqlDataSource ID="SqlKitapDataSoruce" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseProjeConnectionString %>"
SelectCommand="SELECT * FROM [Kitap] ORDER BY [satisSayisi] DESC" >
</asp:SqlDataSource>
<% for(int i = 0; i < 5; i++) %>
<% { %>
<div class="indexKitap">
<asp:ImageButton ID="<% %>" runat="server" ImageUrl="<% %>" />
<asp:Label ID="LblKitapAd" runat="server" Text="<% %>"></asp:Label>
</div>
I have this code and I want to use the columns called foto_path as ImageUrl and kitap_ad as Text of Label, but I don't know how I can use parameters of datasource in html tags. I tried to use like <%#foto_path%> but it gives error. How can I use a parameter of a datasource in a html tag?
You must use a data-bound control, like Repeater. Something like:
<asp:Repeater runat="server" DataSourceID="SqlKitapDataSoruce">
<ItemTemplate>
<div class="indexKitap">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("foto_path") %>' />
<asp:Label ID="LblKitapAd" runat="server" Text='<%# Eval("kitap_ad") %>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>

Categories