Reference a generated dataset - c#

How would I reference and pull data out of a generated dataset?
I have 2 projects in the same solution.
(1) MyUIProject
(2) MyDataSetProject
->MyGeneratedDataSet.xsd
-->-->MyNamesTable (in the dataset)
All I want to do is reference the MyNamesTable and loop through the names in the table and put them in a list box. I'm having trouble getting the records out of the generated dataset.
I'm trying to do something like:
foreach (var name in MyDataSetProject.GeneratedDataSet.MyNamesTable)
{
MyDropDownList.Items.Add(new ListItem(name));
}
Thanks for any thoughts.

First thing to do is make sure your references are correct between your projects. Right click on your MyUIProject and click Add Reference. Go to the Projects tab and add your MyDataSetProject entry. If it gives you an error about it already have been added, then it's already added.
Second, you need to access your dll project classes from your website. Let's say in your website you have a page called Default.aspx, and in your dll project you have a class called DataSetAccessor, which looks like the following:
public class DataSetAcessor
{
public DataSet GetDataSet(<arguments>)
{
//populate the dataset and return it
}
}
You can then use this class in your Default page:
//at top
using MyDataSetProject; //this may vary
//down in some method
DataSetAccessor dsa = new DataSetAccessor();
DataSet data = dsa.GetDataSet();
foreach(DataRow row in data.Tables[0].Rows)
{
//using the values in row to populate your drop down list
}
Hopefully this help.

Related

acumatica c# / Add SOPackageDetailEx

I have this error when I try to add a line of package
Error : Another process has added the "SOPackagedetail" record. Your changes will be lost.
error
My c# code is this :
protected virtual void creationColis()
{
SOShipment ship=Base.CurrentDocument.Select();
SOPackageDetailEx colis = new SOPackageDetailEx();
colis.BoxID="COLIS";
colis.PackageType="M";
colis.ShipmentNbr=ship.ShipmentNbr;
SOShipmentEntry graph = PXGraph.CreateInstance<SOShipmentEntry>();
graph.Packages.Insert(colis); //insertion de l'enregistrement
graph.Packages.Update(colis);
graph.Actions.PressSave();
graph.Clear();
}
Do you know what I must to change please ?
Thanks so much
Xavier
Your question needs more context. For starters, where does your code reside? Given that you reference Base.CurrentDocument.Select, I'm going to assume you are extending SOShipmentEntry to add your code.
In this case, you would just use the Base.Packages view rather than initializing your own instance of SOShipmentEntry where your example goes into trying to use graph.Packages. Regardless, there are 2 parts here that need to be addressed.
Packages is not the primary view of SOShipmentEntry. When you create an instance of a graph, you must tell the graph what record is needed in the primary view. In your example where you create a new instance of a graph, you might do something like this:
graph.Document.Current = graph.Document.Search<SOShipment.shipmentNbr>(myShipmentNbr);
If you are working on a graph extension of SOShipmentEntry, then you probably don't need to create a new instance of the graph. Just make sure graph.Document.Current isn't null before you add your package record - see bullet 2.
Once you have a shipment selected, you can then insert your package information. However, the way you have done it here effectively is trying to add a random package to a null shipment (by the structure of the views) but forcing the record to attach to the right shipment by sheer brute force. The views don't like to work that way.
A better way to add your package once you have a current shipment (Document) is like this:
// Find the current shipment (from the primary view Document)
SOShipment ship = Base.Document.Current();
if(ship?.ShipmentNbr != null) {
// Insert a record into the Packages view of the current shipment and return the record into colis
SOPackageDetailEx colis = Base.Packages.Insert(colis);
// Set the custom values
colis.BoxID="COLIS";
colis.PackageType="M";
// Update the Packages cache with the modified fields
Base.Packages.Update(colis);
// If more fields need to be updated after those changes were applied, instead do this...
colis = Base.Packages.Update(colis);
colis.FieldA = ValueA;
colis.FieldB = ValueB;
Base.Packages.Update(colis);
// If a save is needed, now is the time
Base.Save.Press();
}
Notice that I didn't assign ShipmentNbr. That is because the DAC has that field defined to pull the ShipmentNbr from SOShipment through these 2 attributes.
[PXParent(typeof(FK.Shipment))]
[PXDBDefault(typeof(SOShipment.shipmentNbr))]
This means that when the record is created, Acumatica should lookup the parent SOShipment record via the Key and do a DBDefault on the field to assign it to the SOShipment.ShipmentNbr value (from the parent). Important side note: PXDefault and PXDBDefault are NOT interchangeable. We use PXDefault a lot, but off the top of my head I can't think of a case of PXDBDefault outside of defaulting from a database value like this specific usage.

How to use methods generated by DataSet Designer in C#?

I created a DataSet using the "Add" button on the solution explorer on Visual Studio to use the DataSet Desginer, then add my tables (The connection was already done) and created some queries (everything done from the designer) to get specific data, but how can I use those methods from my code?
I tried a lot of ways but I found nothing:
DBDataSet dBDataSet = new DBDataSet();
dBDataSet. //My methods aren't here
dBDataSet.c_Town. //My methods aren't here neither
dBDataSet.c_Town.DataSet. //...
dBDataSet.c_Town.Rows. //...
dBDataSet.c_Town.Columns. //...
So how can I use those methods? I can't find them anywhere
Nevermind...
I was calling the entire class but what I had to call was the TableAdapter of the respective table, then I can use those methods:
c_TownTableAdapter townTableAdapter = new c_TownTableAdapter();
townTableAdapter.GetNameBy("Town");
My bad, but anyways thanks

Importing a Dataset into RDLC file Wizard stopped working

Weeks ago I created a few (RDLC) reports. To create a Dataset I defined a dummy class and I imported with the procedure, it worked well.
public class DataSetCartaIntestata
{
public string Desc
public string Immage;
public string Name;
}
I did that to get the result of a query with Anonymous type:
public IEnumerable list;
list= b.Results.Where(x=>x.Name="Jack").Select(x=>new{x.Name,x.Image,x.Desc}).ToList();
Now it seems that if I'm going to do that again the procedure won't let me add a Dataset.
When I select new Dataset it creates the Datasource but not the Dataset.
Is there anything I got wrong?
i Found the answare is here the import pocedure import only properties not simple fields of a class even if they are public. so i had to define them.
anyway if I define by hand the Dataset in the RDLC i can access directly fields.
It's easier to make Mistake changing xml plus VS2012 is a bit touchy on the RDLC and crash a lot

Use DataGridView for local settings

I need simple way to persist and edit a set of string pairs in my application. I believe that DataGridView with two culmns can help me with viewing and editing, and I was hoping to use application settings instead of database to store data (maybe a DataSet as setting type). I just can't put all that together. Is there a way?
Here's how I did it. Class MyMap holds value pairs. They must be properties, because DatGridView doesn't work with fields. MyMapCollection holds the collection of MyMaps, as BindingList (allows adding rows in DataGridView). This class is needed to make Visual Studio settings editor happy, couldn't make it work with plain BindingList. So:
public class MyMap {
public String FirstField { get; set; }
public String SecondField { get; set; }
}
public class MyMapCollection : BindingList<MyMap>
{
public MyMapCollection Clone()
{
MyMapCollection result = new MyMapCollection();
foreach (MyMap map in this)
result.Add(new MyMap() {
FirstField = map.FirstField, SecondField = map.SecondField });
return result;
}
}
Function Clone creates a deep copy of the object, so that data is not changed directly on the object in Settings.Default, but when the users says so. In settings editor you would add an item of type MyMapCollection, called say TheValues, and use very simple it in the code:
myDataGridView.DataSource = Settings.Default.TheValues.Clone();
If data should be changed back to settings (when users clicks OK) then change settings accordingly:
Settings.Default.TheValues = (MyMapCollection)myDataGridView.DataSource;
Using a DataTable or DataSet instead of MyMapCollection is also possible, but this solution allows me to use TheValues in the rest of the code, which is even sweeter than DataSet could have been.
If the values that you are trying to edit are plain key value pairs, you can create a class which holds these values as properties and serialize this class object into an XML file. You can deserialize the class and assign the values to the DataGridView.
You could also create a custom configuration and store it separately from App.config / Web.config file. This will be similar to what NHibernate or spring.Net configuration files are stored with a reference to them in the configsections key.
Here is a link how to creat your own custom configuration.
MSDN link

Selecting one row when working with typed datasets

I have a typed dataset in my project. I would like to populate a datatable with only one row instead of all rows. The selected row must be based on the primary key column. I know I could modify the designer code to achive this functionality however if I change the code in the designer I risk that this code will be deleted when I update my datased via designer in the future.
So I wanted to alter the SelectCommand not in the designer but just before firing up MyTypedTableAdapter.Fill method. The strange thing is that the designer does not create a SelectCommand! It creates all other commands but not this one. If it would create SelectCommand I could alter it in this way:
this.operatorzyTableAdapter.Adapter.SelectCommand.CommandText += " WHERE MyColumn = 1";
It is far from perfection but atleast I would not have to modify the designer's work. unfortunately as I said earlier the SelectCommand is not created. Instead designer creates something like this:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
private void InitCommandCollection() {
this._commandCollection = new global::System.Data.SqlClient.SqlCommand[1];
this._commandCollection[0] = new global::System.Data.SqlClient.SqlCommand();
this._commandCollection[0].Connection = this.Connection;
this._commandCollection[0].CommandText = "SELECT Ope_OpeID, Ope_Kod, Ope_Haslo, Ope_Imie, Ope_Nazwisko FROM dbo.Operatorzy";
this._commandCollection[0].CommandType = global::System.Data.CommandType.Text;
}
It doesn't make sense in my opinion. Why to create UpdateCommand, InsertCommand and DeleteCommand but do not create SelectCommand? I could bear with this but this._commandCollection is private so I cannot acces it outside of the class code.
I don't know how to get into this collection without changing the designer's code. The idea which I have is to expose the collection via partial class definition. However I want to introduce many typed datasets and I really don't want to create partial class definition for each of them.
Please note that I am using .NET 3.5.
I've found this article about accessing private properties but it concerns .NET 4.0
Thanks for your time.
In the Dataset designer, first configure a Table/Adapter pair with the basic (all rows) query. Make sure it works and updates/deletes etc.
Then right-click, "Add Query" and change the SQL text to include a "WHERE MyId = #Id". Make sure you keep the same columns. Select Next and call the generated method "FillById()".
Ad of course, use FillById(id) instead of Fill() at the right places.

Categories