Simple LINQ to SQL Nested Select - c#

I'm working on the following
var result =
from ap in db.Appraisals
join at in db.AppraisalTypes
on ap.AppraisalType_ID equals at.AppraisalType_ID
join w in db.Workers
on ap.Worker_ID equals w.Worker_ID
where ap.Worker_ID == WorkerID
&& ap.DateDue != null
&& ap.DateCompleted == null
select new
{
ap.Worker_ID,
ap.Appraisal_ID,
at.Description,
ap.DateDue,
ap.DateCompleted,
CompletedBy = from ap2 in db.Appraisals
join w2 in db.Workers
on ap2.CompletedBy equals w2.Worker_ID
select new
{
name = w2.Surname + ", " + w2.FirstName
}
};
As you can probably see I am using two worker objects, one being the worker that is having their annual/interim employment appraisal and the other object is their line manager.
This select is used to bind a summary list view control but on the CompletedBy column I am trying to assign will always have the most up to date entry as being null then historic data will be populated with the line managers name when an appraisal has been completed. I have done a quick search on this question and there are many posts w/answers but I'm not very experienced as a developer and looking for a simple answer. My other concern is that I am trying to assign a column with something that I know will always have a null value in the list.
Although the compiler recognises this as being syntactically correct, on the bind my label text property has the following...
System.Collections.Generic.List`1[<>f__AnonymousType9`1[System.String]]
I am just looking to any possible caveats I am building here and to fill in the huge gaps to my very limited knowledge of LINQ.
<asp:ListView ID="lvwProbations" runat="server"
OnSelectedIndexChanged="lvwProbations_SelectedIndexChanged"
OnPagePropertiesChanged="lvwProbations_PagePropertiesChanged"
OnPagePropertiesChanging="lvwProbations_PagePropertiesChanging"
DataKeyNames="Worker_ID,Appraisal_ID">
<ItemTemplate>
<tr class="tableItemStyle"
onmouseover="this.style.backgroundColor='Silver'"
onmouseout="this.style.backgroundColor='#EAFFFF'">
<td>
<asp:Label ID="lblWorkerID" runat="server"
Text='<%# Bind("Worker_ID") %>' Visible="false" />
</td>
<td>
<asp:Label ID="lblProbation" runat="server" Width="200"
Text='<%# Bind("Description") %>' />
</td>
<td align="center">
<asp:Label ID="lblDateDue" runat="server" Width="100"
Text='<%# Bind("DateDue", "{0:d}") %>' />
</td>
<td align="center">
<asp:Label ID="lblDateCompleted" runat="server" Width="100"
Text='<%# Bind("DateCompleted") %>' />
</td>
<td align="center">
<asp:Label ID="lblCompletedBy" runat="server" Width="100"
Text='<%# Bind("CompletedBy") %>' />
</td>
<td>
<asp:ImageButton ID="btnSelect" runat="server"
SkinID="selecttimesheet" CommandName="Select" />
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="tableAlternatingItemStyle"
onmouseover="this.style.backgroundColor='Silver'"
onmouseout="this.style.backgroundColor='#CCFFFF'">
<td>
<asp:Label ID="lblWorkerID" runat="server"
Text='<%# Bind("Worker_ID") %>' Visible="false" />
</td>
<td>
<asp:Label ID="lblProbation" runat="server" Width="200"
Text='<%# Bind("Description") %>' />
</td>
<td align="center">
<asp:Label ID="lblDateDue" runat="server" Width="100"
Text='<%# Bind("DateDue", "{0:d}") %>' />
</td>
<td align="center">
<asp:Label ID="lblDateCompleted" runat="server" Width="100"
Text='<%# Bind("DateCompleted") %>' />
</td>
<td align="center">
<asp:Label ID="lblCompletedBy" runat="server" Width="100"
Text='<%# Bind("CompletedBy") %>' />
</td>
<td>
<asp:ImageButton ID="btnSelect" runat="server"
SkinID="selecttimesheet" CommandName="select" />
</td>
</tr>
</AlternatingItemTemplate>
<EmptyDataTemplate>
<table id="Table1" runat="server" style="">
<tr id="Tr2" runat="server" class="tableHoursHeaderStyle">
<th id="Th1" runat="server" style="width:0px;"></th>
<th id="Th2" runat="server">Appraisal</th>
<th id="Th3" runat="server">Date Due</th>
<th id="Th13" runat="server">Date Completed</th>
<th id="Th4" runat="server">Completed By</th>
<th id="Th6" runat="server" style="width:0px;"></th>
</tr>
<tr>
<td colspan="4">
<p>No Probations available</p>
</td>
</tr>
</table>
</EmptyDataTemplate>
<LayoutTemplate>
<table id="Table2" runat="server">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server"
border="0" class="TimeSheet_Table" style="">
<tr id="Tr2" runat="server"
class="tableHoursHeaderStyle">
<th id="Th1" runat="server" style="width:0px;"></th>
<th id="Th2" runat="server">Appraisal</th>
<th id="Th3" runat="server">Date Due</th>
<th id="Th13" runat="server">Date Completed</th>
<th id="Th5" runat="server">Completed By</th>
<th id="Th7" runat="server" style="width:0px;"></th>
</tr>
<tr ID="itemPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
Edited to show markup/bindings on request

Your CompletedBy field is actually typed as a collection of a class that has a single string field. Because you are using a select new { name = ... } you are actually creating a class that looks like this which is not what you want at all:
class Anonymous9
{
public name { get; set; }
}
And, in addition, you are creating a IEnumerable<Anonymous9> which is collection of those objects.
Instead, you just want a single string. To get it to be just a single string, get rid of the new {} and wrap it with FirstOrDefault():
CompletedBy = (from ap2 in db.Appraisals
join w2 in db.Workers
on ap2.CompletedBy equals w2.Worker_ID
select w2.Surname + ", " + w2.FirstName
).FirstOrDefault()

Related

C# = The ListView raised event which wasn't handled.

There is a ListView which shows the data that were retrieved from the database.
I want to edit a single record on a ListView by clicking the edit button beside it but if I press the edit button, I get the error:
The ListView 'lvItemView' raised event ItemEditing which wasn't handled.
Here is the ListView:
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder">
<LayoutTemplate>
<table style="color: Black;" width="100%" border="0" cellpadding="5">
<tr>
<th style="text-align: center;">Customer ID</th>
<th style="text-align: center;">Contact Name</th>
<th style="text-align: center;">Company</th>
<th style="text-align: center;">Created By</th>
<th style="text-align: center;">Created Date</th>
<th style="text-align: center;">Modified By</th>
<th style="text-align: center;">Modified Date</th>
</tr>
<asp:PlaceHolder ID="itemHolder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left">
<asp:Label ID="lblCustomerID" runat="Server" Text='<%#Eval("_CustomerID") %>' />
</td>
<td align="left">
<asp:Label ID="lblContactName" runat="Server" Text='<%#Eval("_ContactName") %>' />
</td>
<td align="left">
<asp:Label ID="lblCompany" runat="Server" Text='<%#Eval("_CompanyID") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedBy" runat="Server" Text='<%#Eval("_CreatedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedDate" runat="Server" Text='<%#Eval("_CreatedDate") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedBy" runat="Server" Text='<%#Eval("_ModifiedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedDate" runat="Server" Text='<%#Eval("_ModifiedDate") %>' />
</td>
<td align="left">
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
<td align="left">
<asp:LinkButton ID="Delete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And the code behind
protected void lvItemView_ItemEditing(object sender, ListViewEditEventArgs e)
{
System.Web.UI.WebControls.Label index_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCustomerID");
int customerID = int.Parse(index_id.Text);
Item item = new Item();
item._CustomerID = customerID;
System.Web.UI.WebControls.Label cmp_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCompany");
int companyID = int.Parse(cmp_id.Text);
item._CompanyID = companyID;
System.Web.UI.WebControls.Label samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblContactName");
item._ContactName = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedBy");
item._CreatedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedDate");
item._CreatedDate = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedBy");
item._ModifiedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedDate");
item._ModifiedDate = samp.Text;
CustomerID = item._CustomerID.ToString();
ContactName = item._ContactName.ToString();
CompanyID = item._CompanyID.ToString();
CreatedBy = item._CreatedBy.ToString();
CreatedDate = item._CreatedDate.ToString();
ModifiedBy = item._ModifiedBy.ToString();
ModifiedDate = item._ModifiedDate.ToString();
modAdd.Show();
}
I am new to asp.net and c# so I have no idea what to do with this kind of error.
set the event lvItemView_ItemEditing in OnItemEditing which will be triggered when click on edit button
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder" OnItemEditing="lvItemView_ItemEditing">
and also set data source if you are binding on pageload
lvItemView.DataSource = SomeData;
lvItemView.DataBind();

How to do this issue with the repeater tag using asp.net?

I'm using a repeater for a table in ASP.NET using C#
And I am trying to set the repeater to use a default of 26 rows.
No matter if I have more or less info.
If i have less, I want the rest of the row to be empty, if i have more then 26 rows of info it should go to a second, third, etc. page.
What should i do?
Here is my code
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="sqltoolinv1" >
<HeaderTemplate>
<table style="width: 86.7%; border-collapse: collapse; border: 1px solid black;" align="center">
<tr align="center" class="text" style="background-color: gray">
<th style="width: 5.14%;">No.</th>
<th style="width: 20.22%;">Tool Nomenclature or Description</th>
<th style="width: 11.4%;">Purchase Date</th>
<th style="width: 9.56%;">Original Cost</th>
<th style="width: 9.93%;">Condition of Tool</th>
<th style="width: 8.456%;">Drawer or Shelf</th>
<th style="width: 21.32%;">Remarks</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr align="left">
<td>
<asp:Label runat="server" ID="lbnum"
Text='<%# Eval("ID") %>' /></td>
<td>
<asp:Label runat="server" ID="lbtname"
Text='<%# Eval("Description") %>' /></td>
<td>
<asp:Label runat="server" ID="lbpurdate"
Text='<%# Eval("[Purchase_Date]") %>' /></td>
<td>
<asp:Label runat="server" ID="lboricost"
Text='<%# Eval("Cost") %>' DataFormatString="{0:C}" /></td>
<td>
<asp:Label runat="server" ID="lbtoolcond"
Text='<%# Eval("Condition") %>' /></td>
<td>
<asp:Label runat="server" ID="lbshelf"
Text='<%# Eval("Location") %>' /></td>
<td>
<asp:Label runat="server" ID="lbremarks"
Text='<%# Eval("Remarks") %>' /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="sqltoolinv1" runat="server"
ConnectionString="<%$ connectionstrings:webserverconnectionstring %>"
SelectCommand="SELECT [ID],[Description],[Purchase Date] AS [Purchase_Date],[Cost],[Condition],[Location],[Remarks] FROM [EmpPortalToolInv] WHERE [Payroll] = #Payroll ORDER BY [ID]">
<SelectParameters>
<asp:SessionParameter Name="Payroll" SessionField="Payroll" DbType="String" />
</SelectParameters>
</asp:SqlDataSource>
I'm not sure that's possible. But you can limit data in list what you send.
var firstPageItems = myList.Take(26);
var secondPageItems = myList.Skip(26).Take(26);
Repeater1.DataSource = firstPageItems;
Repeater1.DataBind();
How to get first N elements of a list in C#
OR
you can pages item by js
for example :
https://www.datatables.net/

possible give two datasource to one repeater

This is my repeater control with data coming from [Patient_Master] table. But Visit_Date is coming from [Patient_Visit] table. How can I show Visit_Date to the same repeater? Is it possible to give two datasources to one repeater? [Patient_Master] and [Patient_Visit] have a relation:
<asp:Repeater ID="repeaterPatientList" runat="server" OnItemDataBound="repeaterPatientList_ItemDataBound" >
<ItemTemplate>
<tr onclick="selectRow(this);">
<td class="csstablelisttd" style="display:none" >
<asp:Label ID="lblPID" runat="server" Text='<%#Eval("PID")%>'></asp:Label>
</td>
<td class="csstablelisttd" >
<%#Eval("Patient_Name")%>
</td>
<td class="csstablelisttd">
<asp:Label ID="lblPatientName" runat="server" Text='<%#Eval("Patient_ID")%>'></asp:Label>
</td>
<td class="csstablelisttd" >
<asp:Label ID="lblSex" runat="server" Text='<%#Eval("Sex")%>'></asp:Label>
</td>
<td class="csstablelisttd">
<asp:Label ID="lblPatientsBirthDate" runat="server" Text='<%#Eval("Patients_Birth_Date")%>'></asp:Label>
</td>
<td class="csstablelisttd">
<asp:Label ID="lblLastVisitDate" runat="server" Text='<%#Eval("Visit_Date")%>'></asp:Label>
</td>
<td class="csstablelisttd">
New
</td>
<td id="tdTodaysVisit" bgcolor="#00cc66" runat="server">
<span class="cssgreen">Today's Visit</span>
</td>
<td class="csstablelisttd">
<%--<asp:HyperLink ID="lnkSchedule" CssClass="csshyperlinkSchedulePatient" runat="server" NavigateUrl='<%# Eval("PID", "~/Create_Order.aspx?ID={0}") %>'>Schedule</asp:HyperLink>--%>
Schedule
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
Why don't you use an outer join between patient_master and patient_visit.

Table display issue

I got a table with 2 nested tables inside that display my two repeaters. My repeaters display Home and office addresses respectively. The problem is that whenever I add a new record in one repeater, the other repeater table's display gets messed up. Like if I add a record in rpt1 , then table for rpt1 moves up and table for rpt2 goes down..ie the headers do not display in one single line . They move up and down when records are added or deleted. WHat I want is these headers to be fixed so if I add new records or delete records, the headings of both repeaters display on the same line...How do I fix this ? Hope its not confusing.
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="50%">
<asp:Panel ID="pnlAddAddress" runat="server">
<asp:Repeater ID="rpt1" OnItemCommand="rpt1_ItemCommand" runat="server" OnItemDataBound="rpt1_OnItemDataBound">
<HeaderTemplate>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="5" class="linegrey">
</td>
</tr>
<tr class="lgrey">
<td>
Address1
</td>
<td>
City
</td>
<td>
State
</td>
<td>
IsDefault
</td>
<td>
Actions
</td>
</tr>
<tr>
<td colspan="5" class="dots">
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkAddressB" runat="server" Text='<%# Eval("Address1")%>' CommandName="DisplayAddressB" CommandArgument='<%#Eval("AddID") %>' CausesValidation=false></asp:LinkButton>
</td>
<td>
<%# Eval("City")%>
</td>
<td>
<%# Eval("State")%>
</td>
<td>
<%-- Visible='<%# Eval("IsDefault")%>'--%>
<asp:LinkButton ID="lnkDefaultB" Text="Set as Default" CommandName="SetDefaultB" runat="server" CommandArgument='<%# Eval("AddID") + "," + Eval("IsB") %>'
CausesValidation="false" Visible='<%# Eval("IsDefault")%>'></asp:LinkButton>
<asp:Label ID="labelDefaultB" Text="Yes" runat="server" Visible='<%# Eval("IsDefault")%>'></asp:Label>
</td>
<td>
<asp:ImageButton ID="lnkAdd" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Edit" runat="server" ImageUrl="~/images/Edit.gif" Width="14" Height="14" ToolTip="Edit"></asp:ImageButton>
<asp:ImageButton ID="lnkDel" Text="Delete" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Delete" runat="server" ImageUrl="~/images/Delete.gif" Width="14" Height="14" ToolTip="Delete"></asp:ImageButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
</td>
<td>
<asp:Panel ID="pnlSping" runat="server">
<asp:Repeater ID="rpt12" OnItemCommand="rpt12_ItemCommand" runat="server" OnItemDataBound="rptSpping_OnItemDataBound">
<HeaderTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="5" class="linegrey">
</td>
</tr>
<tr class="lgrey">
<td>
Address1
</td>
<td>
City
</td>
<td>
State
</td>
<td>
IsDefault
</td>
<td>
Actions
</td>
</tr>
<tr>
<td colspan="5" class="dots">
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="lnkAddressS" runat="server" Text='<%# Eval("Address1")%>' CommandArgument='<%#Eval("AddID") %>' CommandName="DisplayAddressS" CausesValidation="false"></asp:LinkButton>
</td>
<td>
<%# Eval("City")%>
</td>
<td>
<%# Eval("State")%>
</td>
<td>
<asp:LinkButton ID="lnkDefaultS" Text="Set as Default" CommandName="SetDefaultS" runat="server" Visible=true CommandArgument='<%# Eval("AddID") + "," + Eval("IsS") %>'
CausesValidation="false"></asp:LinkButton>
<asp:Label ID="labelDefaultS" Text="Yes" runat="server" Visible=true></asp:Label>
</td>
<td>
<asp:ImageButton ID="lnkAdd" Text="Edit" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Edit" runat="server" ImageUrl="~/images/Edit.gif" Width="14" Height="14" ToolTip="Edit"></asp:ImageButton>
<asp:ImageButton ID="lnkDel" Text="Delete" CommandArgument='<%#Eval("AddID") %>'
CausesValidation="false" CommandName="Delete" runat="server" ImageUrl="~/images/Delete.gif" Width="14" Height="14" ToolTip="Delete"></asp:ImageButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
</td>
</tr>
</table>
Use css attribute valign and set its value to top like this <td width="50%" valign="top">.
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="50%" valign="top">
<asp:Panel ID="pnlAddAddress" runat="server">
......
</asp:Panel>
</td>
<td valign="top">
<asp:Panel ID="pnlSping" runat="server">
......
</asp:Panel>
</td>
</tr>
</table>
For headers look to use the <th> element, this will keep them at the top of the table.
Put the <table> tags you do have outside your repeater controls, make sure the repeater only renders a single row and the same for the header.
Unless you have good reason consider just wrapping the two tables in <div> tags as nested tables really don't work ideally for layout.

How to check the child td in Datalist

I have a Datalist. Following is the structure of the ItemTemplate:
<ItemTemplate>
<div id="driversGrid" runat="server" style="width:3500px;" >
<table cellpadding="0" cellspacing="0" border="0" width="3500px">
<tr>
<td id="Td1" runat="server" style="visibility:hidden;">
<asp:Label ID="lblID" runat="server" BackColor="White" Font-Bold="true" Text='<%# Eval("ID") %>' /><br />
</td>
<td id="title" style="width:90px;text-align:center;">
<asp:Label ID="lblTitle" runat="server" BackColor="White" Font-Bold="true" Text='<%# Eval("Name") %>' /><br />
</td>
<td id="am0900" runat="server" style="width:90px;"> </td>
<td id="am0915" runat="server" style="width:90px;"> </td>
<td id="am0930" runat="server" style="width:90px;"> </td>
<td id="am0945" runat="server" style="width:90px;"> </td>
<td id="am1000" runat="server" style="width:90px;"> </td>
<td id="am1015" runat="server" style="width:90px;"> </td>
<td id="am1030" runat="server" style="width:90px;"> </td>
<td id="am1045" runat="server" style="width:90px;"> </td>
<td id="am1100" runat="server" style="width:90px;"> </td>
</tr>
</table>
</div>
</ItemTemplate>
Now, at the itemdatabound event, I fetch multiple time (data) from the database in form 10:25 AM.
I manipulate each data and convert it to am1030 (similar to one of the td ID in itemTemplate). Now I want to check each td in ItemTemplate and compare the lblID (i.e. the staff ID) to the ID in my variable and then compare the tdID to the manipulated data. If they match change the background color of the td.
In short, I want to check that if the lblID is 3 then get the td with ID am1030 and change the background color.
Needs to be done in C#.net.
Use a foreach loop to iterate through the e.Item.Controls collection, like this -
foreach (Control c in e.Item.Controls)
{
//if c is HtmlCell, then check the ID and change the color.
}

Categories