Intellisense can't see Datatable Name in Dataset generated by hand - c#

I'm trying to learn this tutorial http://www.devx.com/dotnet/Article/28678/1954 in C#, I have created the datatables but when I want to type
DsActivitiesTasks.Tasks.AddTasksRow("Email")
Intellisense doesn't see Tasks but only TasksRow and TasksDataTable and none have add method.
Did I forget to do something ?

DsActivitiesTasks is the name of the class generated by the designer.
Tasks is an instance property of that class, so you can only access it from an instance of the dataset.
Try creating a new instance of DsActivitiesTasks, like this:
new DsActivitiesTasks().Tasks.AddTasksRow("Email");
Note that this code will throw away the new dataset; you'll need to store it somewhere in a field or property.
For example:
public static readonly DsActivitiesTasks Database = new DsActivitiesTasks();
//In some method:
Database.Tasks.AddTasksRow("Email");
Note that datasets are not thread-safe, so you must not work with it on multiple threads.

Instantiate DsActivitiesTasks.

Related

FileHelpers - Creating a field object

I am trying to dynamically add field properties to a record class that I am also building dynamically using FileHelpers.Dynamic.DelimitedClassBuilder. I have no issues creating the class object and I currently add a field using the AddField(String) method.
As my apps grows I now have a need to declare specific field properties in various situations. So in the same sense I wanted to use FileHelpers.Dynamic.DelimitedFieldBuilder to create a field object and then pass that to my DelimitedClassBuilder object using the method AddField(DelimitedFieldBuilder).
However I am unable to instantiate a new object using FileHelpers.Dynamic.DelimitedFieldBuilder. When I issue the following code I get an error stating that DelimitedFieldBuilder does not contain a constructor that takes two arguments.
FileHelpers.Dynamic.DelimitedFieldBuilder fb = new FileHelpers.Dynamic.DelimitedFieldBuilder("ClassName", "Type");
Looking at the documentation it appears that this class does only have properties associated with it, so I am kind of stuck on how to actually implement this. It seems like it should be fairly easy but I cant seem to figure it out. Thanks for any help.
Not familiar with that functionality of file helpers; however, in the vast majority of functions/methods across .NET there is usually a way to assign properties after the class is instantiated.
Try something like this:
FileHelpers.Dynamic.DelimitedFieldBuilder fb = new FileHelpers.Dynamic.DelimitedFieldBuilder();
fb.Whatever = "ClassName";
fb.otherwhatever = "Type";
Just a stab. I have no idea if it will work or not.
The constructors of DelimitedFieldBuilder are internal so you'll run into difficulty with your approach. However AddField(String) returns a DelimitedFieldBuilder, so you might be able to use that.
It might be easier to make your own class MyFieldBuilder which calls the standard AddField(String).

Accessing Form class from other class

I have been struggling with this bit for over a day now. It appears that many people have asked about similar thing, but they usually want to do something way more advance than what I need. I got an idea what it should look like (I think), but struggle with actually properly implementing it - so here it goes:
I have a form application that does some Odbc, Sql, Csv import/exports.
My main class is the ImportForm class, which contains all the buttons, controls it also creates SQL and ODBC connections in each constructor etc.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ImportForm importForm = new ImportForm();
Application.Run(importForm);
}
.
public partial class ImportForm : Form
public static SqlConnection sqlConnection = new SqlConnection(ConstantValues.SqlConnectionString);
public static OdbcConnection odbcConnection = new OdbcConnection(ConstantValues.OdbcConnectionString);
(...)
I now have few other classes that do different things. For instance once of imports CSV files into SQL Server. It does it through SqlBulk based on the SQL connection defined in the ImportForm class:
internal class CsvImportIntoSql
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConnection);
This, however, returns a syntax error 'The name does not exist in the current context'
I tried:
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(importForm.sqlConnection);
But it doesnt work, the class ImportForm is available in the context, but its defined instance importForm is not and I cannot understand why and what I should do to make it work - I have been struggling with that for a while and have to employ different workarounds which often do not make for good code.
Any help would be appreciated.
Almost right: you need the class name ImportForm, not an instance of it ( importForm) to access a static property:
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(ImportForm.sqlConnection);
If the CsvImportIntoSql instance is created in the ImportForm, then your best option would be to inject the sqlConnection, or if it is likely to change after the CsvImportIntoSql instance is created then inject a refrence to the CsvImportIntoSql instance itself.
If I understand, you are trying to use the same sqlConnection variable that is defined in another class. but it appears that you have declared this variable as static, which should help while referring to it from another class:
Try to replace
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConnection);
with:
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(ImportForm.sqlConnection);
This should work. however,using the same connection in multiple instance is not always a good practice. instead, you can initialize a new connection for each job.

Get all writeable properties of an ADLDS-Class

I'm developing an application which can deal with a MS-ADLDS-Service.
Currently it is possible to create Directory-Entries and assign values to some properties.
Not a realy exciting task until this:
Im my application it's possible (it should be) to configure which properties of a class (for instance: the CN=Person class) should be assigned with values which are evaluated at runtime in my application.
Long story short:
I want to retrieve all (writeable) properties of a class. Without creating and saving a new CN=Person-Object before.
Currently i use my schemaBinding to get the Directory-classSchema-Entry of the Person-Class (CN=Person) from where i read some property-values (like "AllowedAttributesEffective", "mayContain", "AllowedAttributes") - i get the most properties by this way - but some Properties are missing! For instance the "telephoneNumber"-Property (attributeSchema: CN=Telephone-Number)
Does anybody know how to get these properties of a class? ADSI-Edit does this: when i create a new object with adsi-edit i can assign values to all possible properties before committing the new entry.
thanks a lot for any hint!
(.net code is welcome)
I have found the solution for my task!
Some of these properties are "calculated" and not persistent at the directoryentry.
So its meant to call the RefreshCache() Method and pass the needed property names as an string array.
directoryEntry.RefreshCache(new string[] { "allowedAttributesEffective",
"allowedAttributes",
"systemMayContain",
"systemMustContain" });
After that call, the properties have values....
if (directoryEntry.Properties["systemMayContain"]).Value != null)
{
/// Success
}

Possible to use DataSet across Classes?

I am using VS 2010 and C# windows forms.
I need the user to enter how many objects he has and how much each weights.
I then need to process each one. I typically use for each datarow in row collection.
Question is i tried cleaning up some of my Very Nasty code (this is my first real project ever btw) I have one main class at ~5000 lines of code and would like to break up specific sets of modules into there own classes. The problem is when the user enters info i have to set up a dataset on the main form (DSObjects) and link a table grid view and a couple of entry boxes with an add button to it so the user can add the data they need. The issue I am having is when I run the code and break up the class into sub classes the dataset is wiped out on each new class.
Do not create a new instance of the data set in every class, but pass one instance around, like:
public class AnotherClass
{
private DataSet m_dataSet;
public AnotherClass(DataSet ds)
{
m_dataSet = ds;
}
}
When creating a new instance of AnotherClass use:
AnotherClass ac = new AnotherClass(m_dataSet);
where m_dataSet is again a member variable that references the data set - either passed to the calling class in the constructor, or (in case of the main class) being created somewhere in the code.
Only create the data set once, for example in the main class.
Another approach could be to use a singleton class that holds an instance to the data set. The singleton could then be accessed from lots of different objects.
A non-threadsafe sample would be:
public class DataHolder
{
private DataSet m_dataSet;
private static DataHolder m_instance;
private DataHolder()
{
m_dataSet = ... // Fill/Create it here
}
public static DataHolder Instance
{
get
{
if (m_instance = null)
m_instance = new DataHolder();
return m_instance;
}
}
public DataSet Data
{
get { return m_dataSet; }
}
}
Then, access it using DataHolder.Instance.Data;
You could try passing the dataset into the constructor of each class
var something = new Something(dataset)
Keep in mind that a DataSet is actually a small (but complete) database in memory. While most applications just use it to transfer data from a database server to objects in the application, it has all the parts of a full database: multiple tables, which can be joined be relationships, which can have queries run against them.
With that in mind, just as you wouldn't have separate databases for each class, but instead have one used globally for the whole application, it is similarly quite reasonable to have one DataSet shared by all objects in the application.

Populating A New Object With Existing Objects Data

I have a service that is returning a custom object called "UserSettings" In my application I want to add more properties to the "UserSettings" object so I created a new object called "MyUserSettings" that inherits from "UserSettings" now I want to populate my new object with the existing data held in "UserSettings" and the new data I have for my new properties. I do not want to have to map each property one by one to the same property in the new object like this..
_MyUserSettings.Name=_UserSettings.Name;
Is there a way or better approach to what I am doing to populate ALL the properties of the existing object into my new object in one shot??
Yes, you can use Copy Constructor pattern. It would give you an other benefit - you do not need public property setters so object becomes immutable.
public MyUserSettings(UserSettings baseSettings)
{
// TODO: set all properties
}
Unfortunately this is the only way, however, the specific mechanism can change. There are a numerous ways (not listing them all):
Copy constructor, that takes an item and does this manual copying of fields across.
Use reflection to have a more generic mechanism of achieving the same.
Use something like AutoMapper.
They all boil down to pretty much doing the same thing.
If the UserSettings is actually a MyUserSettings then you can simply cast it:
var mySettings = (MyUserSettings)settings;
However, this will fail if UserSettings is really UserSettings.

Categories