ASP.NET [Object data source] - asp.net

I send id of my class object from one page to second this way :
NavigateUrl='<%# "ItemDetail.aspx?itemId=" + (string)Eval("Id") %>'
Then I get the object with ObjectDataSource and function this way :
<asp:ObjectDataSource ID="ObjectDataSourceItem" runat="server" SelectMethod="GetItem"
TypeName="Catalog">
<SelectParameters>
<asp:QueryStringParameter Name="itemId" QueryStringField="itemId" Type="string" DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
So how to use this item in my new page code :
this.ObjectDataSourceItem.?????

You need to subscribe to the ObjectDataSource's Selected event, access the ObjectDataSourceStatusEventArgs ReturnValue property (e.ReturnValue), and cast it to the appropriate type. Something like:
protected void ObjectDataSourceItem_Selected(object source, ObjectDataSourceStatusEventArgs e)
{
var myDataSet = (DataSet)e.ReturnValue;
}

Related

Passing object to objectdatasource insert parameter

This is my object data source code.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
InsertMethod="InsertCustomerItem" SelectMethod="GetCustomerItems"
TypeName="HPPWebsite.HPPClasses.TestClassHelper">
<InsertParameters>
<asp:Parameter Name="CustCode" Type="String" />
<asp:Parameter Name="ItemIn" Type="Object" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="CustCode" SessionField="CustomerCode"
Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
This is my code behind:
<protected void Button1_Click(object sender, EventArgs e)
{
TestClass item = new TestClass();
item.CustCode = Session["CustomerCode"].ToString();
item.ItemCode =" ";
item.BasePrice =0.0;
item.AddMarkUp =true;
ObjectDataSource1.InsertParameters["CustCode"].DefaultValue = item.CustCode;
ObjectDataSource1.InsertParameters.Add("itemin",TypeCode.Object,"item");
ObjectDataSource1.Insert();
}
It gives me an error that type string cannot be converted to my testclass object. How can this be resolved please? I can do it if i just pass the object fieldds as seperate parameters but im trying to pass the whole object and do the trimmings and calculations in the stored proc

Parameter error when trying to build a selectcommand

I'm passing a value thru a querystring, I want that value to be part of my selectcommand to build my GridView. But I'm getting the following error. Any ideas how to fix?
Error: Conversion failed when converting the nvarchar value '<%=QueryString2%>' to data type int.
C#:
protected void Page_Load(object sender, EventArgs e)
{
QueryString=Convert.ToInt32(Request.QueryString["tourid"]);
}
private int _querystring;
public int QueryString
{
get
{
return _querystring;
}
set
{
_querystring = value;
}
}
ASPX:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>"
SelectCommand="SELECT * FROM [vwSchedule] Where TourId=#tid">
<SelectParameters>
<asp:Parameter DefaultValue="<%=QueryString%>" Name="tid" />
</SelectParameters>
</asp:SqlDataSource>
You can't use <%= ... %> to assign the value of a server-control property. As a result, your DefaultValue is being set to the literal string "<%=QueryString%>", which is not a valid integer.
Try using a QueryStringParameter instead:
<SelectParameters>
<asp:QueryStringParameter
Name="tid"
DbType="Int32"
QueryStringField="tourid"
/>
</SelectParameters>

Error in parameter that i use in update command in sqlDataSource

I use below code :
.aspx
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ConnectionStrings:Con %>" runat="server"
SelectCommand="select * from DorePayvar" OnSelecting="selecting"
UpdateCommand="update DorePayvar set Name=#name" OnUpdating="SqlDataSource1_Updating">
<UpdateParameters>
<asp:Parameter Type="String" Name="name" DefaultValue="Sajjad" />
</UpdateParameters>
</asp:SqlDataSource>
.cs
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["name"].Value = "bvhjbjh";
}
but when i press update button occur below error
An SqlParameter with ParameterName 'name' is not contained by this
SqlParameterCollection.
You are missing the # symbol.
The following should work :
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#name"].Value = "bvhjbjh";
}
For more information on using parameters with sqldatasource check this out.
The problem could be that there is not a parameter in your parameter list with the name "name".
Try:
e.Command.Parameters.Add(new SqlParameter("name", "bvhjbjh"));

Pass a this.property to ObjectDataSource

A user control contains a ListView, ObjectDataSource and a CustomerID property, is there a way to pass CustomerID to ObjectDataSource.
it seems ControlParameter does not fix the problem.
Regards
ControlParameter must be used for to get a parameter from a Control. To get a parameter from a property you have to get it from code behind:
<asp:ObjectDataSource OnSelecting="OdsOnSelecting" .... >
<SelectParameters>
<asp:Parameter Name="CustomerID" />
</SelectParameters>
</asp:objectDataSource>
protected void OdsOnSelecting(object sender, ObjectDataSourceMethodEventArgs e) {
e.InputParameters["CustomerID"] = CustemerID;
}
Respond to this event:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.objectcreating.aspx
Try creating an attribute/property for the usercontrol in question, THEN do what is mentioned in the two post before this one...

asp.net datasource instantiate class

I have a static method that takes a parameter and returns a class. the class has a ReadOnlyCollection Property that i'd like to display in a asp:repeater. Is there a way to do this using LinqDataSource or ObjectDataSource? I got pretty close with ObjectDataSource, but since the method is returning a single class object, I couldn't get me repeater to bind to the Property.. Here's what I did:
ClassName: ClassName
StaticMethod: StaticMethod(ParamName)
ReadOnlyCollection: ClassName.Collection
<asp:objectdatasource
runat="server"
id="myData"
selectmethod="StaticMethod"
typename="ClassName"
>
<selectparameters>
<asp:parameter name="ParamName" defaultvalue="Value" />
</selectparameters>
</asp:objectdatasource>
<asp:repeater runat="server" datasourceid="myData">
<itemtemplate>
<%# Container.DataItem %>
</itemtemplate>
</asp:repeater>
So, This only returns the readonly collection object, not each item as i'd like.
Is this possible without have actual code to instantiate the object?
If you're only concerned with displaying data in your collection you could add another static method that returns your objects ReadOnlyCollection and bind to that. Alternatively you can ditch the ObjectDataSource and do it in code, something like:
var myObj = ClassName.StaticMethod(someParam);
MyRepeater.DataSource = myObj.Collection;
MyRepeater.DataBind();
If you need to display both data in your class and collection, then you can add another Repeater to the ItemTemplate:
<asp:objectdatasource runat="server" id="myData"
selectmethod="StaticMethod" typename="ClassName">
<selectparameters>
<asp:parameter name="ParamName" defaultvalue="Value" />
</selectparameters>
</asp:objectdatasource>
<asp:Repeater runat="server" datasourceid="myData" OnItemDataBound="Rep_ItemDataBound">
<ItemTemplate>
<%# Eval("SomeProperty") %>
<ul>
<asp:Repeater id="RepCollection" runat="server">
<ItemTemplate>
<li><%# Eval("SomeCollectionProperty") %></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</ItemTemplate>
</asp:Repeater>
The ItemDataBound method would look something like:
protected void Rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
var dataItem = (ClassName)e.Item.DataItem;
var innerRepeater = (Repeater)e.Item.FindControl("RepCollection");
innerRepeater.DataSource = dataItem.Collection;
innerRepeater.DataBind();
}
}

Resources