I have the below query on my aspx page that works:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="InventoryDataContext"
EntityTypeName="" TableName="V_InventoryForDisplays"
Where="ConfiguredCarId == #ConfiguredCarId">
<WhereParameters>
<asp:Parameter DefaultValue="2827" Name="ConfiguredCarId" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
I need to change the Where clause to use a Contains statement. I have an auto-propertyĐ–
public IEnumerable<int> ConfiguredCarIds { get; set; }
within the same class that I would like to use
so what is the proper syntax to do this:
Where="ConfiguredCarIds.Contains (ConfiguredCarId)"
Thanks!
Try Where="ConfiguredCarId.Contains(#ConfiguredCarId)"
Related
Using ASP.NET/C#/nHibernate architecture - I have a GridView with an ObjectDataSource and am having problems getting the delete method to work.
When I simply use the Remove(T entity) function declared in my service layer (which then calls down to my nHibernate repository), the GridView_RowDeleting() method gets called, followed by ObjectDataSource_Deleting(), and then the service layer function is invoked and the business object is properly deleted.
However, when I try to use the RemoveByID(int ID) function, after the GridView_RowDeleting() and ObjectDataSource_Deleting() methods are called, the service layer is never being invoked.
I think this has something to do with the ObjectDataSource.DeleteParameters, but I can't seem to get it right.
Below is the code snippet for the GridView and ObjectDataSource. As you can see, I have the business object's primary key pkChapter7ID listed in the GridView's DataKeyNames:
<asp:GridView ID="gvChapter7" runat="server" DataSourceID="odsChapter7" DataKeyNames="pkChapter7ID">
<Columns>
<asp:BoundField DataField="pkChapter7ID" HeaderText="pkChapter7ID" SortExpression="pkChapter7ID"/>
...
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsChapter7" runat="server"
DeleteMethod="RemoveByID" OldValuesParameterFormatString="original_{0}"
SelectMethod="FindAllForProjectID" TypeName="CLS.Services.Chapter7Service">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:SessionParameter Name="ID" SessionField="ProjectID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
And the relevant portion of my generic service layer (calls down the nHibernate repository, which is working fine once I get there):
[DataObjectMethod(DataObjectMethodType.Delete, false)]
public virtual void Remove(T entity)
{
_repository.Remove(entity);
}
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public virtual void RemoveByID(int ID)
{
T entity = FindBy(ID);
if (entity != null)
_repository.Remove(entity);
}
I have tried setting DeleteParameters in the _RowDeleting method:
protected void gvChapter7_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
odsChapter7.DeleteParameters["ID"].DefaultValue = e.Keys["pkChapter7ID"].ToString();
}
As well as numerous ways of setting up the ObjectDataSource control parameter in the aspx page, such as:
<DeleteParameters>
<asp:ControlParameter ControlID="gvChapter7" Name="ID" PropertyName="SelectedDataKey" Type="Int32" />
</DeleteParameters>
Can anyone see what I am missing?
<asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="DataClassesDataContext"
TableName="PrivateMessages" Where="Sender == #Sender">
<WhereParameters>
<asp:QueryStringParameter Name="Sender" QueryStringField="idCompany" Type="String" />
</WhereParameters>
</asp:LinqDataSource>:LinqDataSource>
this code select from table PrivateMessages where Sender==QueryString('idCompany')
this code work fine.
i want select from privateMessage where Sender=="admin"????????
where sender equal a const string.
You can use Selecting event and do like..
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.WhereParameters["Sender"] = set here...
}
Or you can replace the WhereParameters section with something like
<WhereParameters>
<asp:Parameter Name="Sender" Type="String" DefaultValue="admin" />
</WhereParameters>
Depends on you in choosing what flavor of this 2 solutions best suits this scenario.
I am making a WebForm in asp.net and I want to display gridview with data from asp:SqlDataSource
My problem becomes when I try to skip (pass null) values for some of the Parameters.
Here is some snippets of my code
In aspx file sqldatasource looks like
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="art_bat_art" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="art_naz" DefaultValue="HLA" ConvertEmptyStringToNull="true" Type="String" />
<asp:Parameter Name="art_sifra" DbType="String" Direction="Input"/>
<asp:Parameter Name="vrsta_id" DefaultValue="3" ConvertEmptyStringToNull="true" Type="Int32" />
<asp:Parameter Name="podvrsta_id" DefaultValue="13" ConvertEmptyStringToNull="true"
Type="Int32" />
<asp:Parameter Name="podgrupa2" DefaultValue="1365" ConvertEmptyStringToNull="true" Type="Int32" />
<asp:Parameter Name="podosobine" DefaultValue="1:9241" ConvertEmptyStringToNull="true"
Type="String" />
<asp:Parameter Name="podosobineOR" DefaultValue="true" ConvertEmptyStringToNull="true"
Type="Boolean" />
</SelectParameters>
My grid looks like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
And troubles comes in code behind
One of parameters for my stored procedure is optional, If i pass it as null in SQL the stored procedure is
going to execute anyway.
I am trying to skip and pass that parameter as follows
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = DBNull.Value; //Cannot implicitly convert type
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = System.Data.SqlTypes.SqlString.Null; //Cannot implicitly convert type
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = System.Data.SqlTypes.SqlString.Null.ToString(); //IT compiles but i get NULL as text in SQL
//IF I Just skip seting of this parametar, My SqlDataSource won't call stored proce
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = string.Empty; //Again no execution of stored proce
//Even if I call SqlDataSource1.DataBind(); //nothing cames to sql
GridView1.DataBind();
}
}
You'll have to edit your SQL (or stored proc) to be able to handle an optional parameter.
ie: if you have:
SELECT blah FROM yourTable WHERE art_sifra = #art_sifra
change it to:
SELECT blah FROM yourTable WHERE (art_sifra = #art_sifra OR #art_sifra = '')
and then just change ConvertEmptyStringToNull="false" and set your select parameter to "":
SqlDataSource1.SelectParameters["art_sifra"].DefaultValue = "";
Also need to set CancelSelectOnNullParameter="false" on your datasource.
DBNull.Value; //Cannot implicitly convert type
Have you tried casting it? E.g.
DBNull.Value.Tostring()
I'm using an ObjectDataSource to sort/page/filter as below:
<asp:ObjectDataSource
ID="odsCompaniesIndex" runat="server" EnablePaging="true"
SelectMethod="GetCompaniesSubset"
StartRowIndexParameterName="startRowIndex"
MaximumRowsParameterName="maximumRows"
SelectCountMethod="GetCompaniesCount"
SortParameterName="sortExpression"
TypeName="Company">
<SelectParameters>
<asp:ControlParameter ControlID="ddlStatus"
ConvertEmptyStringToNull="true"
DbType="Boolean" PropertyName="SelectedValue" Name="status" />
</SelectParameters>
</asp:ObjectDataSource>
The gridview consuming the ObjectDataSource:
<asp:GridView ID="gvCompanyIndex" AutoGenerateColumns="true" runat="server" DataSourceID="odsCompaniesIndex"
AllowPaging="true" DataKeyNames="company_id" AllowSorting="true">
</asp:GridView>
I want to pass in a number of parameters like the one above into SelectParameters. The method call 'GetCompaniesSubset' executes but on the return I get the following error:
ObjectDataSource 'odsCompaniesIndex'
could not find a non-generic method
'GetCompaniesCount' that has
parameters: status.
My SelectMethod is:
public DataSet GetCompaniesSubset(
int startRowIndex, int maximumRows, string sortExpression, bool status)
{...}
How do you allow the SelectMethod to utilise StartRowIndexParameterName/MaximumRowsParameterName and any extra parameters?
Thanks
The problem is that GetCompaniesCount does not have status parameter, not GetCompaniesSubset.
I have this ASP.NET code:
<asp:DropDownList
ID="ddlBrokers"
runat="server"
AutoPostBack="true"
DataSourceID="srcBrokers"
DataTextField="broker"
DataValueField="brokerId"
/>
<asp:ObjectDataSource
id="srcBrokers"
TypeName="DatabaseComponent.DBUtil"
SelectMethod="GetBrokers"
runat="server">
</asp:ObjectDataSource>
My DAL code:
public DataTable GetBrokers(bool? hasImport=null)
{
SqlCommand cmd = new SqlCommand("usp_GetBrokers");
if (hasImport.HasValue)
cmd.Parameters.AddWithValue("#hasImport", hasImport);
return FillDataTable(cmd, "brokers");
}
When the form loads I get this error:
ObjectDataSource 'srcBrokers' could not find a non-generic method 'GetBrokers' that has no parameters.
Is it my optional parameter that is causing the problem? How can I workaround this? Is it possible to have optional parameters with declarative ASP.NET code?
add method:
public DataTable GetBrokers() {
return GetBrokers(null);
}
and check is it works?
You can use SelectParameters like this:
<asp:ObjectDataSource id="srcBrokers" TypeName="DatabaseComponent.DBUtil" SelectMethod="GetBrokers" runat="server">
<SelectParameters>
<asp:Parameter Name="hasImport" Type="Empty" />
<SelectParameters>
</asp:ObjectDataSource>