I have an entity datasource and i would like to filter it (created_on) to show only the max date. How do i go about doing it?
<asp:EntityDataSource ID="Payroll_DetailsDS" runat="server"
ConnectionString="name=sspEntities" DefaultContainerName="sspEntities"
EnableDelete="True" EnableFlattening="False" EnableInsert="True"
EnableUpdate="True" EntitySetName="Payroll_Details"
OrderBy="it.[Payslip_no] desc" EntityTypeFilter="" Select=""
Where="it.Status = "COM"">
</asp:EntityDataSource>
Right now the data source is only filtering by it.status. I would to filter also by it.created_on = Max(created_On) .. thats the idea of what i want, dont know how to get it.
i was able to get something similar in linq and it worked fine. just need it in entity datasource.
var testquery = from d in context.Payroll_Details
where d.Created_on == ((from b in context.Payroll_Details
where b.Employee_Personal_InfoEmp_id == xyz.PD_EmpPersonal_Id
select b.Created_on).Max())
select new
{
d.Employee_Personal_InfoEmp_id,
d.Basic_Pay_YTD
};
var r = testquery.SingleOrDefault();
If I undertand you right, you can use this way:
<asp:EntityDataSource ID="SalesOrderHeader" runat="server"
ConnectionString="name=AdventureWorksEntities"
DefaultContainerName="AdventureWorksEntities" EnableDelete="True"
EnableInsert="True" EnableUpdate="True" EntitySetName="SalesOrderHeader"
EntityTypeFilter="" OrderBy="it.TotalDue DESC" Select=""
Where="it.OnlineOrderFlag = TRUE AND it.TotalDue > #ordercost">
<WhereParameters>
<asp:ControlParameter ControlID="costLimit" DbType="Int32"
DefaultValue="2500" Name="ordercost" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.orderby.aspx
Related
I have a drop down list (very long, over 100 items) inside the insert item template view for a details view. I would like to add a text box and button (search feature) so i can filter this list but i get the following error.
Databinding methods such as Eval(), XPath(), and Bind() can only be
used in the context of a databound control.
I created two entity datasource, one with a where clause and the other one without. When I hit search, the code behind (button click event) switches the datasource to the one with the where clause and parameter but i get the error above. Any advice on how to go about doing this?
Dim aa As DropDownList = DetailsView1.FindControl("DropDownList1")
aa.DataSourceID = ""
aa.DataSource = EmpPersonalInfoLOV1
aa.DataBind()
EDITED
Change aa.DataSource from a string to EmpPersonalInfoLOV1 (the name of the datasource)
EDIT #2
More info requested by users..
Datasource # 1 Code
<asp:EntityDataSource ID="EmpPersonalInfoLOV" runat="server"
ConnectionString="name=sspEntities" DefaultContainerName="sspEntities"
EnableFlattening="False" EntitySetName="Employee_Personal_Info"
EntityTypeFilter=""
Select="it.[Emp_id], it.[Employee_No_FastPay], it.[Surname] + ' '+ it.[Firstname] As FullName"
Where="">
</asp:EntityDataSource>
Datasource # 2 Code
<asp:EntityDataSource ID="EmpPersonalInfoLOV1" runat="server"
ConnectionString="name=sspEntities" DefaultContainerName="sspEntities"
EnableFlattening="False" EntitySetName="Employee_Personal_Info"
Select="it.[Emp_id], it.[Employee_No_FastPay], it.[Surname] + ' '+ it.[Firstname] As FullName"
Where="it.Surname like '%' + #Name + '%'">
<WhereParameters>
<asp:ControlParameter ControlID="TxtBx1" DbType="String"
DefaultValue="""" Name="Name" PropertyName="SelectedValue" />
</WhereParameters>
</asp:EntityDataSource>
It looks like you are not using the correct PropertyName on your EntityDataSource. Since you are getting the value from a textbox, the property you are getting is Text. Try this:
<asp:EntityDataSource ID="EmpPersonalInfoLOV1" runat="server"
ConnectionString="name=sspEntities" DefaultContainerName="sspEntities"
EnableFlattening="False" EntitySetName="Employee_Personal_Info"
Select="it.[Emp_id], it.[Employee_No_FastPay], it.[Surname] + ' '+ it.[Firstname] As FullName"
Where="it.Surname like '%' + #Name + '%'">
<WhereParameters>
<asp:ControlParameter ControlID="TxtBx1" DbType="String" Type="String" DefaultValue="" Name="Name" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>
Also, when you are using the like in your where clause, you really don't need two data objects. When the textbox is empty the query will select all.
HiI have a list box that is bound to a table in a database. it produces a list of companies. The user will then come along and select multiple companies that they want to view information about, and then they hit a selet button which ill display the company information (e.g company name, company site, address e.t.c) in a gridview underneath. however the issue that i am having is that it only displays ONE of the multiple companies selected and its always the top one.
Can someone please shed some light on how i get all the companies to be displayed in the gridview?
i am programming in vb.net
please see source code below
<asp:ListBox ID="ListBox1" runat="server"
DataSourceID="SqlDataSource11" DataTextField="compName"
DataValueField="compDataID" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource11" runat="server"
ConnectionString="<%$ ConnectionStrings:IWSRiskAssessmentConnectionString %>"
SelectCommand="SELECT [compDataID], [compName] FROM [tblCompany] WHERE ([compDataID] <> #compDataID) ORDER BY [compName]">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="compDataID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView
ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="compName" HeaderText="compName"
SortExpression="compName" />
<asp:BoundField DataField="Site Name" HeaderText="Site Name"
SortExpression="Site Name" />
<asp:BoundField DataField="Reference Number" HeaderText="Reference Number"
SortExpression="Reference Number" />
<asp:BoundField DataField="Asset" HeaderText="Asset" ReadOnly="True"
SortExpression="Asset" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" />
<asp:BoundField DataField="Block" HeaderText="Block" SortExpression="Block" />
<asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:IWSRiskAssessmentConnectionString %>"
SelectCommand="SELECT tblCompany.compName, tblSite.siteName AS [Site Name], tblSite.siteUPRN AS [Reference Number], tblIncMain.incAsset + ' ' + CAST(tblIncMain.incNumber AS varchar) AS Asset, tblIncMain.incLocation AS Location, tblIncMain.incBlock AS Block, tblIncMain.incRoom AS Room FROM tblIncMain INNER JOIN tblSite ON tblIncMain.incSite = tblSite.siteID INNER JOIN tblCompany ON tblSite.siteCompany = tblCompany.compDataID WHERE (tblIncMain.incActive = 1) AND (tblSite.siteActive = 1) AND (tblIncMain.incRemoved = 0) AND (tblCompany.compDataID = #compDataID) ORDER BY [Site Name], tblIncMain.incAsset, tblIncMain.incNumber">
<SelectParameters>
<asp:ControlParameter ControlID="ListBox1" Name="compDataID"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
this is the source code :-) please take a look and any help is greatly appreciated
May be DataBind code executes on every request or you are not collecting selected items from the listbox.
Sub page_load()
if Not IsPostBack Then
End If
End sub
and to iterate Listbox items,
For Each item in ListBox1.Items
if item.Selected then
End If
Next
Your data source is only selecting 1 record. The ListBox will return the first value when you use a multiple select ListBox as a ControlParameter.
(tblCompany.compDataID = #compDataID)
What you want to do is have something with an IN statement such as
(tblCompany.compDataID in #compData)
You may need to do something in the code behind.
<asp:ListBox ID="ListBox1" runat="server"
AutoPostBack="True"
DataTextField="compName"
DataSourceID="SqlDataSource11"
DataValueField="compDataID"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
SelectionMode="Multiple">
</asp:ListBox>
Code behind
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s = string.Empty;
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
s += li.Value + ",";
}
if (s != string.Empty)
{
s = s.Substring(0, s.Length - 2); // chop off trailing ,
SqlDataSource2.SelectParameters["compData"].DefaultValue = s;
}
}
NOTE this hasn't been tested but it is one option you can try. Basically you want a IN not a = for your select to get all results.
ok so i added 'in' to my sql statement, however it still only picked up one selection from the list box instead of multiple this is the sql statement:
SELECT tblCompany.compName, tblSite.siteName AS [Site Name], tblSite.siteUPRN AS [Reference Number], tblIncMain.incAsset + " " + CAST(tblIncMain.incNumber AS varchar) AS Asset, tblIncMain.incLocation AS Location, tblIncMain.incBlock AS Block, tblIncMain.incRoom AS Room FROM tblIncMain INNER JOIN tblSite ON tblIncMain.incSite = tblSite.siteID INNER JOIN tblCompany ON tblSite.siteCompany = tblCompany.compDataID WHERE (tblIncMain.incActive = 1) AND (tblSite.siteActive = 1) AND (tblIncMain.incRemoved = 0) AND tblCompany.compDataID in(#compDataID) ORDER BY [Site Name], tblIncMain.incAsset, tblIncMain.incNumber
I have two dropdownlists for a search, the 1st list is for the city and the second is for the area within the selected city. I would like to add a default value in the 2nd dropdownlist that will search ALL of the areas by default unless a specific area is selected from the list that contains the areaID for a specific search.
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="cityName" AutoPostBack="true" DataValueField="cityID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Cities]"></asp:SqlDataSource>
<asp:DropDownList ID="DropArea" runat="server" DataSourceID="SqlArea"
DataTextField="areaName" DataValueField="areaID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlArea" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [area] WHERE ([cityID] = #cityID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="0" Name="cityID"
PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
Bind the second from code behind. Create a data table and insert the Select text at the begining and bind the datatable to the DDL.
DataTable dt =
DataRow dr = dt.NewRow()
dr["areaName"] = "All";
dr["SqlArea"] = "All";
tbldata.Rows.InsertAt(dr,0);
DropArea.DataSource = dt;
DropArea.DataBind();
You can try to add for second dropdownlist OnDataBound="DropArea_DataBound" event.
And use in code:
protected void DropArea_DataBound(object sender, EventArgs e)
{
DropArea.Items.Insert(0, new ListItem() {Value = "-1", Text = "ALL"});
}
And then when you handle click event, check:
var value = DropArea.SelectedItem.Value;
if(string.equals(value, '-1')
{
use your logic here
}
Hope it will help with your problem.
Best regards, Dima.
Chris,
I got the idea below from an answer HERE, but basically you want to modify your query to be in the form of the following:
Your Original:
SELECT * FROM [Cities]
Change To:
SELECT City FROM [Cities]
UNION ALL
SELECT 'ALL'
Old question, but you never found an answer.
How can I execute a stored procedure using sqldatasource and get the return value in vb.net.
Thanks,
Terri
The method you are looking for is DataBind. Call it using mySqlDataSource.DataBind()
<asp:SqlDataSource
ID="sds2"
runat="server"
ConnectionString="..."
SelectCommand="spTest"
SelectCommandType="StoredProcedure"
>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text"
Name="ParamName" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gv" runat="server" DataSourceID="sds2"></asp:GridView>
The stored procedure is executed when you call DataBind. The DataBind method is called automatically if the DataSourceID property of the GridView control refers to a valid data source control.
You need to use a SqlConnection with a SqlCommand, like this:
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("StoredProcedureName", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("SomeParam", someValue);
object result = command.ExecuteScalar();
}
If you already have the SP returning a value then you have to grab the value in the corresponding event for the data source. AKA - Inserted, Selected, etc...
Here's a couple links illustrating the point.
http://fredrik.nsquared2.com/viewpost.aspx?PostID=162
http://www.velocityreviews.com/forums/t86158-re-how-to-retrieve-an-output-parameter-using-sqldatasource-control.html
<asp:SqlDataSource ID="ADSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ADConnection %>"
SelectCommand="GetProfile" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="InputTextBox" Name="Host" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
GetProfile is the stored proc name and Host is parameter name, which is retreived from a texbox called InputTextBox
Is Possible to put some server tag in Session Field to access a constant string???
<asp:EntityDataSource ID="EntityDataSourceProcesos" runat="server" ConnectionString="name=Entities"
DefaultContainerName="Entities" EntitySetName="PROCESO" EntityTypeFilter="PROCESO"
Select="it.[DE_PROC], it.[ID_PROC], it.[ST_PROC]"
Where="it.[ST_PROC] = 1 and it.[TMEMPR].[CO_EMPR] = #EmpresaID">
<WhereParameters>
<asp:SessionParameter Name="EmpresaID" SessionField='<%= stringPublicVariable %>' Type="String" />
</WhereParameters>
</asp:EntityDataSource>
Instead you can add your parameter at code behind :
EntityDataSourceProcesos.WhereParameters.Add(
new SessionParameter("EmpresaID", TypeCode.String, stringPublicVariable));