Binding Object Data Source Update Parameters to Business Entity - objectdatasource

I have a GridView on my ASP.net website which is bound to the Tasks entity.The Task entity is
generated by the entity framework:
class Task
{
int TaskID;
string TaskName;
string TaskDescription;
int TaskWeightage;
}
I'm trying to insert Task in GridView using an object Data source. I've defined the InsertParameters for object data source as:
TaskID,TaskName,TaskDescription,TaskWeightage.
I've declared the Insert method for the object data source as:
public bool AddTask(Task t)
{}
I would like the Object Data source to construct the Task Object from the Insert Parameters and then call the Insert method.How can I acheive this?

Related

Best practice to retrieve Json value stored in sql table through c# code

I have a JSON value stored in SQL Server. I want to retrieve that JSON value and bind it to C# property (which is deserialized to desired entity). I want to know what would be the best practice to do that effectively? Right now, I'm doing like this:
Public class Employee
{
public int Id;
public string Name;
public int Age;
}
Public class EmployeeData
{
public string JsonEmployeeText {get;set;} // Binding the json string from database
public List<Employee> Employees { get { return JsonConvert.DeserializeObject<Employee>(JsonEmployeeText );}} //Converting the retrieved json string from Database to c# entity
}
I guess we should not do any sort of database logic on getters as it can sometimes delay the process of initialization in case we need to use this we need to make sure we enable the lazy loading

Object return type is not supported in WCF test client

In WCF i have a class which is acting as the return type for all my methods.
public class ResponseResult
{
public object data {get;set;}
public string Name {get;set;}
}
and in IService interface i have method declaration like:
ResponseResult GetValues(int id);
if run the test client GetValues is not available.beside the method name i am getting cross sign on hover it says "this method is not supported in WCF Test client because it uses mmService.Common.ResponseResult".
if i remove the property data then it get visible and i want that object property. please help me out.
Object data type will not work with service client.service client need the serialize data type like list , string , int etc.... it will not actually decide the actual return type of object data type.

Scala: How to transform a POJO like object into a SQL insert statement using Scala reflection

I'm facing this (at least for me) interesting task: getting a SQL insert statement from a POJO like object. Let me say I don't need to add a framework between my Scala application and the DB because I just need to insert data into a single DB table.
So, supposing the attributes of my class are named equally to those of the DB table, I'd like to use Scala reflection in order to get from a class like this one
class MyDataObj {
var a:Int = 345
var b:Boolean = false
var c:Double = 1243.98
var d:String = "A random string"
}
a SQL insert statement like this
INSERT INTO table_a (a, b, c, d) values (345, false, 1243.98, 'A random String');
Well, what we need is
1) access to the class attributes
2) access to the attribute types
3) access to the attribute values of the object instance
In order to get something like this
List( ("a","Int",345), ("b","Boolean",false), ("c","Double",1243.98), ... )
that will be easy to transform into what we want.
Up to now, I've just found out how to access to the attributes names
val columns = typeOf[MyDataObj].members.view.filter{_.isTerm}.
filter{!_.isMethod}.map{_.name}.toList
How can I get the rest I need?
Thanks as usual for supporting me.
In your case, you can use the following codes:
val o = new MyDataObj
val attributes = o.getClass.getDeclaredMethods.filter {
_.getReturnType != Void.TYPE
}.map {
method => (method.getName, method.getReturnType, method.invoke(o))
}
Here I use getDeclaredMethods to get the public methods in the MyDataObj. You need to notice that getDeclaredMethods can not get methods in its parent class.
For MyDataObj, getDeclaredMethods will return the following methods:
public double MyDataObj.c()
public boolean MyDataObj.b()
public java.lang.String MyDataObj.d()
public int MyDataObj.a()
public void MyDataObj.c_$eq(double)
public void MyDataObj.d_$eq(java.lang.String)
public void MyDataObj.b_$eq(boolean)
public void MyDataObj.a_$eq(int)
So I add a filter to filter out irrelevant methods.

nHibernate mapping to custom types

I have a Oracle database and one of the fields is a date range field. It is basically just stored in the database as a VARCHAR(40) in the format YYYY/MM/DD-YYYY/MM/DD. I want to map it in nHibernate to a custom class I have created like this
public class DateTimeRange
{
public DateTimeRange(DateTime fromTime, DateTime toTime)
{
FromTime = fromTime;
ToTime = toTime;
}
public override string ToString()
{
return String.Format("{0} to {1}", FromTime.ToString("HH:mm:ss"), ToTime.ToString("HH:mm:ss"));
}
public DateTime FromTime { get; set; }
public DateTime ToTime { get; set; }
}
How can I map to custom classes like this?
You need to implement your own IUserType.
See this blog post for details. I'll also paste the relevant section below in case the blog disappears.
In NHibernate, a custom mapping type is a class that derives from either the IUserType or ICompositeUserType interfaces. These interfaces contain several methods that must be implemented, but for our purposes here, we’re going to focus on 2 of them. Consider the following.
public class TypeClassUserType : IUserType
{
object IUserType.NullSafeGet(IDataReader rs,
string[] names,
object owner) {
string name = NHibernateUtil.String.NullSafeGet(rs,
names[0]) as string;
TypeClassFactory factory = new TypeClassFactory();
TypeClass typeobj = factory.GetTypeClass(name);
return typeobj;
}
void IUserType.NullSafeSet(IDbCommand cmd,
object value,
int index) {
string name = ((TypeClass)value).Name;
NHibernateUtil.String.NullSafeSet(cmd, name, index);
}
}
Having created this class, I can now explicitly map the association between ActualClass and TypeClass as a simple property on the ActualClass mapping.
<property
name="Type"
column="TypeName"
type="Samples.NHibernate.DataAccess.TypeClassUserType,
Samples.NHibernate.DataAccess" />
As NHibernate is in the process of saving an instance of ActualType, it will load and create a new instance of TypeClassUserType and call the NullSafeSet method. As you can see from the method body, I am simply extracting the name from the mapped property (passed in as the value parameter) and setting the extracted name as the value of the parameter to be set in the database. The net result is that although the Type property of ActualClass is TypeClass in the domain model, only the Name property of the TypeClass object gets stored in the database. The converse is also true. When NHibernate is loading an instance of ActualType from the database and the finds a property of my custom mapping type, it loads my custom type and calls the NullSafeGet method. As you can see, my method gets the name from the returned data, calls my flyweight factory to get the correct instance of TypeClass, and then actually returns that instance. The type resolution process happens transparently to my data access classes (and even to NHibernate itself for that matter).

Rdlc: reporting off a child object that is an IList of some primitive type

I am using rdlc reports to report off some businesss objects.
I use subreports to report off of nested objects (as described here).
When using a regular list of child objects, like IList(Of Books), I create a datasource for books, and then use this as the datasource of the subreport.
I'm little stuck how to use this technique when the nested object is an IList(Of String), or another list of a primitive type.
What's the best way to do the reporting in this scenario?
I encountered a similar problem. I had a report running of a list of CustomObject. The CustomObject list was populated from a query on a list of ParentObjects that contained many-many references to all their CustomObjects. The relationship was removed and a string column was used on the ParentObject (ParentObject.CustomObjectName) instead. Now my report receives a string[] containing all the CustomObject names.
My solution was creating a wrapper object with a single string property and a constructor to use as my data source. I named it just like the CustomObject my report was expecting.
class CustomObject
{
public string Name {get; set;}
public CustomObject(string name)
{
Name = name;
}
}
I load my list using LINQ, I call the wrapper constructor in the select statement
var wrappedObjects = from parent in GetParentObjects()
select new CustomObject(parent.CustomObjectName);
From the report you can add a data source for the CustomObject class like you normally would and access the object as usual "=Fields!Name.Value".
I have an object structure like so
Order -> List:OrderItems(Products) -> List:AdditionalProducts(Products)
I have a main report with a subreport and a subsubreport.
I fixed this in the following way
In the subreport I pass a parameter to the subsub report. this parameter has a value of the product name and is called "thisproduct".
The subreportprocessing function is called by both the subreport and the subsub report.
So I need to assign the data source for the both in here.
I assign the data source for the subreport (order.items)
I add a "if" for if the subsubreport (InternalOrderItemAdditionalProducts) is calling this function. in here i check the parameter, and get the product name. I can then loop through the orderitems, find the one relating to the product being looked at, and then assign the subsubreport datasource to be the additionalproducts related to that product.
private void SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
// assign subreport datasource
e.DataSources.Add(new ReportDataSource("OrderItems", order.Items));
// if the subsubreport is calling this then
if (e.ReportPath != null)
if (e.ReportPath == "InternalOrderItemAdditionalProducts")
{
// find the orderitem relating to this product
foreach (var orderitem in order.Items)
{
if (e.Parameters["thisproduct"].Values[0] == orderitem.ProductType.Name)
{
// assign the subsubreport datasource to be the additional products of that order item
e.DataSources.Add(new ReportDataSource("AdditionalProducts",
orderitem.Product.AllDescendentAdditionalProducts));
}
}
}
}