How do I get the caller Form's name in the Insert() of a table in AX 2012? - x++

How do I get the caller Form's name in the Insert() of a table in AX 2012?

Check the dataSource's formRun.
FormDataSource fds;
FormName fn;
if (this.isFormDataSource())
{
fds = this.dataSource() as FormDataSource;
if (fds)
{
fn = fds.formRun().name();
}
}

Related

WP7 Insert all linq results in an ObservableCollection

I parse an xml results from a webservice using linq :
XElement items = XElement.Parse(e.Result);
MyListBox.ItemsSource = from item in items.Descendants("node")
select new MyViewModel
{
...
};
This automatically populate my ListBox. But the problem is, I usually access my ObservableCollection like this :
App.MyViewModel.MyItems;
having in my xaml :
ItemsSource="{Binding MyItems,}"
How can I modify directly my ObservableCollection ? I read Cast LINQ result to ObservableCollection
and tried this :
var v = from item in items.Descendants("node")
select new MyViewModel
{
...
};
OApp.MyViewModel.MyItems = new ObservableCollection<MyViewModel>(v);
But I can't since this in WP7 (Silverlight 3), and there is no constructor like this
Thanks !
I'd just invent a static method like this:-
public static ObservableCollection<T> CreateObservableCollect<T>(IEnumerable<T> range)
{
var result = new ObservableCollection<T>();
foreach (T item in range)
{
result.Add(item);
}
return result;
}
Now your last line of code becomes:-
OApp.MyViewModel.MyItems = new CreateObservableCollection<MyViewModel>(v);
The constructor you're trying to use is in Silverlight, just not available on the phone. (as per MSDN)
Unfortunately, you'll have to populate your ObservableCollection yourself.
Do you need ObservableCollection? Do you need add or delete objects from collection or just update?
If only update, you can change MyViewModel.MyItems to:
public MyTypeOfCollection MyItems
{
get { return _myItems; }
set
{
_myItems = value;
OnNotifyPropertyChanged("MyItems");//invoke INotifyPropertyChanged.PropertyChanged
}
}
If you need adding or deleting of items, you can extend your collection to:
public static class Extend
{
// Extend ObservableCollection<T> Class
public static void AddRange(this System.Collections.ObjectModel.ObservableCollection o, T[] items)
{
foreach (var item in items)
{
o.Add(item);
}
}
}

EF4 return new unique identifier from stored procedure

I am using EF4 and MVC 2.
I am inserting a new record to the database, and I need it to return the new ID value.
My stored procedure ends like this:
SELECT SCOPE_IDENTITY() AS NewApplicationID;
Here is my action method:
public ActionResult CreateApplication(ApplicationViewModel applicationViewModel)
{
if (ModelState.IsValid)
{
try
{
Mapper.CreateMap<ApplicationViewModel, Application>();
var application = (Application)Mapper.Map(applicationViewModel, typeof(ApplicationViewModel), typeof(Application));
var success = applicationRepository.InsertApplication(application);
}
catch (Exception ex)
{
}
}
return View("CreateApplication", applicationViewModel);
}
Here is my InsertApplication method in my repository class:
public int InsertApplication(Application application)
{
db.Applications.AddObject(application);
return db.SaveChanges();
}
I need to return the value of the new ID.
I hope someone can help.
Thanks.
I managed to get this one sorted out. When I added my stored procedure I mapped NewApplicationID to the ApplicationID property. So after the record is inserted it sets the ApplicationID property to the value returned by SCOPE_IDENTITY().

SubSonic Return ExecuteSingle for Stored Procedure

I wish to return a single ScPollOption item using a Stored Procedure via the code below:
public ScPollOption FetchPollOptionByID(int optionID)
{
StoredProcedure sp = SPs.ScPollOptionGetOptionByID(optionID);
return sp;
}
When working with a query I would use:
ExecuteSingle<ScPollOption>()
but SubSonic only allows for sp.ExecuteTypedList<> and sp.ExecuteScalar<>.
How can I return a single ScPollOption item?
Thanks
Dan
I know it's not terribly attractive, but this would work if you're able to use the LINQ extensions:
sp.ExecuteTypedList<ScPollOption>().FirstOrDefault();
You could also execute an IDataReader and inflate the ScPollOption object manually:
ScPollOption item;
using (IDataReader reader = sp.ExecuteReader())
{
if (reader.Read())
{
item = new ScPollOption();
item.SomeProperty = reader.GetValue(0);
// Set additional properties
}
}
return item;

How do I convert a datatable into a POCO object in Asp.Net MVC?

How do I convert a datatable into a POCO object in Asp.Net MVC?
Pass each DataRow into the class constructor (or use getters/setters) and translate each column into the corresponding property. Be careful with nullable columns to extract them properly.
public class POCO
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime? Modified { get; set; }
...
public POCO() { }
public POCO( DataRow row )
{
this.ID = (int)row["id"];
this.Name = (string)row["name"];
if (!(row["modified"] is DBNull))
{
this.Modified = (DateTime)row["modified"];
}
...
}
}
A data table typically holds many rows - do you want to convert each row into an object instance?
In that case, you could e.g. add a constructor to your POCO object that will accept a DataRow as parameter, and then extracts the bits and pieces from that DataRow:
public YourPOCO(DataRow row)
{
this.Field1 = row["Field1"].ToString();
...
this.FieldN = Convert.ToInt32(row["FieldN"]);
}
and so on, and then call that constructor on each of the rows in the DataTable.Rows collection:
List<YourPOCO> list = new List<YourPOCO>();
foreach(DataRow row in YourDataTable.Rows)
{
list.Add(new YourPOCO(row));
}
And you could then create a ASP.NET MVC view or partial view based on this "YourPOCO" type and use the "List" template to create a list of "YourPOCO" instances in a list-like display.
Marc
Old question, anyway this can be usefull for somebody:
private static T CreatePocoObject<T>(DataRow dr) where T : class, new()
{
try
{
T oClass = new T();
Type tClass = typeof (T);
MemberInfo[] methods = tClass.GetMethods();
ArrayList aMethods = new ArrayList();
object[] aoParam = new object[1];
//Get simple SET methods
foreach (MethodInfo method in methods)
{
if (method.DeclaringType == tClass && method.Name.StartsWith("set_"))
aMethods.Add(method);
}
//Invoke each set method with mapped value
for (int i = 0; i < aMethods.Count; i++)
{
try
{
MethodInfo mInvoke = (MethodInfo)aMethods[i];
//Remove "set_" from method name
string sColumn = mInvoke.Name.Remove(0, 4);
//If row contains value for method...
if (dr.Table.Columns.Contains(sColumn))
{
//Get the parameter (always one for a set property)
ParameterInfo[] api = mInvoke.GetParameters();
ParameterInfo pi = api[0];
//Convert value to parameter type
aoParam[0] = Convert.ChangeType(dr[sColumn], pi.ParameterType);
//Invoke the method
mInvoke.Invoke(oClass, aoParam);
}
}
catch
{
System.Diagnostics.Debug.Assert(false, "SetValuesToObject failed to set a value to an object");
}
}
return oClass;
}
catch
{
System.Diagnostics.Debug.Assert(false, "SetValuesToObject failed to create an object");
}
return null;
}
Source is http://blog.developers.ie/cgreen/archive/2007/09/14/using-reflection-to-copy-a-datarow-to-a-class.aspx
I saw your other question about using a datatable in the data access layer. If you return POCO at some point its a good idea to let your DAL return POCO already.
You would use an SqlDataReader to fill the POCO. This is more light weight. Sometimes its easier to use DataSet and DataTable for Lists of entries, but if you tranform the rows into stronly typed POCOS anyway I am pretty shure that this is the way to go.

Can not update object in control action with ASP.NET MVC?

I have a view with a form, when user submit the form, the matched action method is like:
public ActionResult Test(ViewModel vm, Member member)
{
//...
if (ModelState.IsValid)
{
try{
//...
member.OID = 1; //error here
//...
}Catch(Exception ex)
{
//...
}
}
}
It works fine before, but now I get error as below when assigning the value to a object property:
Operation is not valid due to the current state of the object
why? how to resolve it?
It is not very clear as your code sample, if my guess is right. Member object is LINQ's class where you have OID as a FK to other object in your schema.
The error show that you cannot assign OID directly. say your O is Occupation Id. then you have to
member.Occupation = (from c in dc.Occupation where c.ID = 1 select c);
Hope this helps.

Resources