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>
Related
In default.aspx.cs , i defined a public variable. = my_id
And i can use this variable in my aspx page.
But i can't use in GridView DataSourceQuery.
My gridview's datasource code : ( in aspx )
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>" SelectCommand="SELECT [mus_ad], [marka], [model], [durum] FROM [servis_kayitlari] WHERE [kullanici_id] LIKE '<%= my_id ;%>'">
</asp:SqlDataSource>
After this datasource query , gridview comes null. What is the problem in this query ?
You should use parameterized query(see Using Parameters with the SqlDataSource Control for reference) and assign parameter value in SqlDataSource.Selecting event:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:aspnetdbConnectionString %>"
SelectCommand="SELECT [mus_ad], [marka], [model], [durum] FROM [servis_kayitlari] WHERE [kullanici_id] LIKE #my_id"
OnSelecting="SqlDataSourceSelectingEventHandler">
<SelectParameters>
<asp:Parameter Name="my_id" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
And event handler:
public void SqlDataSourceSelectingEventHandler(object sender, SqlDataSourceSelectingEventArgs e)
{
SqlDataSource1.SelectParameters["my_id"].DefaultValue = this.my_id;
}
As suggested in other answer, you should rather choose to use parametrized query but per your posted query, the issue is in binding variable with LIKE. You need to remove the ; in <%= my_id ;%>. Your query should look like
SELECT [mus_ad], [marka], [model], [durum] FROM [servis_kayitlari] WHERE [kullanici_id] LIKE '<%= my_id %>'"
I have a session variable which holds the current logged in user's details. I'd like to run a query based on the variable and display it in a Grid view.
I want to replace the default value of the grid view with the session variable but that didn't work.
Can anyone point me in the right direction?
This is what I tried:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:..... %>"
SelectCommand="SELECT [Username], [Password], [CustomerName], [CustomerAddress], [Contact] FROM [Customers] WHERE ([Username] = #Username)">
<SelectParameters>
<asp:Parameter DefaultValue="session[new]" Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
You can supply the Session variable value just before the Query is executed by SqlDataSource using the Selecting event of the datasource. Set this event in Markup using OnSelecting property.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:..... %>"
OnSelecting="SqlDataSource1_Selecting"
SelectCommand="SELECT [Username], [Password], [CustomerName], [CustomerAddress], [Contact] FROM [Customers] WHERE ([Username] = #Username)">
<SelectParameters>
<asp:Parameter Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
In the Selecting Event Handler, you do as below:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if(Session["UserName"]!=null)
{
// do this only when Session Variable stores as a string the Username
e.Command.Parameters["#Username"].Value = Session["UserName"];
}
else
{
// assign the default value if session variable is Null
e.Command.Parameters["#Username"].Value ="DefaultValue";
}
}
In this event handler, you can access the command that’s about to be executed through the
SqlDataSourceSelectingEventArgs.Command property, and modify its parameter values programmatically.
What I ended up doing was kind of similar to FlopScientist's answer. I found that the DataSource actually has an option for selecting a session variable which I did in the design view and it has the code below:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:..... %>"
SelectCommand="SELECT [Password], [CustomerName], [CustomerAddress], [Contact], [Username] FROM [Customers] WHERE ([Username] = #Username)">
<SelectParameters>
<asp:SessionParameter Name="Username" SessionField="mySessionVariable" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Session object in ASP.NET is one of the server side state maintenance variable.
More you can find here.
Session variable simply stores any value that you assign it to. And in simple words, you can access this variable on any other page, even if you created it on some other page.
so suppose on page1.aspx you did:
Session["uniqueName"] = GetGridViewData(); //you can store anything.
where signature of GetGridViewData() is suppose like:
public DataTable GetGridViewData();
then on page2.aspx (or any other page), you can do this:
protected void Page_Load(object sender, EventArgs e)
{
if(Session["uniqueName"]!=null)
{
//typecast it to your datastructure or whatever you assigned into it
DataTable gridDataSource = (DataTable)Session["uniqueName"];
//any custom logic you want to perform on gridDataSource
gridView1.DataSource = gridDataSource;
gridView.DataBind();
}
}
Just make sure, you access the variable only after you have assigned/created earlier in the flow.
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)"
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 thought calling ObjectDataSource.Select() gives the same results as calling ObjectDataSource.DataBind(), but in some cases that doesn’t seem to be true:
<asp:ObjectDataSource ID="ODS1" TypeName="PersonDB" SelectMethod="GetPeople"
runat="server"></asp:ObjectDataSource>
<br>
<asp:ListBox ID="ListBox1" DataSourceID="ODS1" DataTextField="PersonID"
AutoPostBack="true" runat="server"></asp:ListBox>
<br>
<asp:ObjectDataSource ID="ODS2" InsertMethod="InsertEmployee"
TypeName="PersonDB" SelectMethod="GetPerson" runat="server">
<SelectParameters>
<asp:ControlParameter ConvertEmptyStringToNull="True" Name="PersonID"
PropertyName="SelectedValue" ControlID="ListBox1" />
</SelectParameters>
</asp:ObjectDataSource>
<br>
<asp:DetailsView ID="DetailsView1" AutoGenerateInsertButton="true" DataSourceID="ODS2"
runat="server"> </asp:DetailsView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ODS1.Select(); //same as calling ODS1.DataBind();
}
if (IsPostBack)
{
ODS2.Select(); // returns no results
}
}
In the above code calling ODS1.Select() produces the same results as calling ODS1.DataBind(). But if on postback user selects an item in ListBox, ODS2.Select() still doesn’t return any results, but if we replace ODS2.Select(); with ODS2.DataBind(); then a row is returned. So why doesn’t ODS2.Select(); return any results, but ODS2.DataBind(); does?
thank you
EDIT:
Assuming user select an item in a Listbox --> It seems that when we call ODS2.Select(), ODS2 for some reason can't bind to ListBox1.SelectedValue and extract a value from this property
ODS2 has a Select parameter, which in your sample page load is bound to a ListBox control that hasn't been databound. What's in the ListBox? What is being passed in the PersonID parameter that is passed to ODS2?
The most obvious way to start answering your question would be to set a breakpoint in the GetPerson method of PersonDB and see what is being passed as parameters. Then follow the code to see what gets retrieved.