Category: .net

Dec 30 2015

Using Yeoman to scaffold asp.net mvc applications

  1. Install nodejs
  2. From the node command prompt install yeoman ... npm  install -g yo
  3. You may want to install any dependencies that will be used by yeoman like...
    npm install -g gulp
    npm install -g bower
    npm install -g grunt
    npm install -g grunt-cli
  4. Install a yeoman generator...for example
    npm install -g generator-aspnet
    or (for a plain vanilla website)
    npm install -g generator-webapp
  5. Create and navigate to the target directory where you want to create your aspnet web app.
  6. run a yeoman generator as follows...
    yo aspnet
    or
    yo (then select your generator from the list)
  7. When you run the aspnet generator you will be prompted to name your web app...name it.
  8. The navigator will create a folder matching your app name...navigate into that folder (cd <appname>)
  9. To create a dotnet runtime environment for your app type...
    dnu restore
    If you get a dnu command not found error ... from another admin cmd type ...
    dnvm upgrade...
    If that command doesn't work see this page...
    https://github.com/aspnet/home/
  10. To launch the web server type dnx web
  11. To get help on a generator type yo <generatorname> --help...like this...
    yo aspnet  --help
  12. This will show you all of the  sub-generators for the generator
    1. To get help on a subgenerator type yo <generator>:<subgen> --help ... like this...
      yo aspnet:TypeScript --help
      or
      yo aspnet:MvcController --help
  13. So..to generator a new controller do yo aspnet:MvcController <controllername>...like this...
    yo aspnet:MvcController HelpController
  14. To create a matching view for an action you can use aspnet:MvcView <viewname>...BUT...
    first you have to navigate to the views folder (cd views) create a target folder that matches the controller name ... like "Help" and then navigate to that folder.

For more information on this, including how it all works beautifully with visual studio code see this...
https://code.visualstudio.com/Docs/runtimes/ASPnet5

Jun 03 2014

Which DataGridView event do I use when I want to change the type of a cell at runtime?

        <p>I have a winform application that has a user control that is using a DataGridView control on it. </p>

When rendering the DataGridView I want some of the rows Quantity columns cells to be read only DataGridViewTextBox cells and other rows to a a DataGridViewComboBoxCell.

What is the appropriate DataGridView event to use this. CellFormatting or CellPaint or some other event?

Jun 03 2014

Which DataGridView event do I use when I want to change the type of a cell at runtime?

I have a winform application that has a user control that is using a DataGridView control on it.

When rendering the DataGridView I want some of the rows Quantity columns cells to be read only DataGridViewTextBox cells and other rows to a a DataGridViewComboBoxCell.

What is the appropriate DataGridView event to use this. CellFormatting or CellPaint or some other event?

Feb 20 2014

What is the best way to create "abstract" class members when declaring the class as abstract is not an option?

There is a class that I want to create as abstract but I can't because that class's base class is ActiveReports and as soon as I make it abstract the sub-reports are no longer designable. (Interestingly the base class itself IS designable but not it's children). I know that I cannot declare class members as abstract without first declaring the class as abstract. In this case what is the NEXT BEST way to do this. At this point I made the members virtual and added comments to each one. Where it made sense I also declared a member as protected rather than public. But is there some other best way that would REQUIRE that these members be overridden at compile-time rather than run-time? If you were in this situation how would you do it? EDIT Let me explain further. I can create a class (lets call it MyReportsBase) that inherits from ActiveReports. (I do that using add new and choose the ActiveReports type) Then ActiveReports provides a property of the report object in MyReportsBase (in design time properties) called MasterClass. Once I set that property to true then I can then create new reports and inherit from MyReportsBase instead of directly from ActiveReports. (The way you do that is by create add new ActiveReports but then edit the code in the code-behind to inherit from MyReportsBase instead of ActiveReports). That is all well and good and it all works perfectly well. However, my requirement is that MyUpwardBase would HAVE to be inherited (declared abstract with a few abstract members). And I CAN go in the code-behind and set the abstract modifier on the base class. And even then I can design MyReportsBase. However, at that point, all of the reports that I inherited from MyReportsBase are no longer designable. Attempting to open the derived reports in the designer throws an error about how the designer cannot open the base class because it is declared as abstract. All of this is to just clarify the question. Right now the Interface suggestion is making the most sense to me. EDIT 2
An Interface doesn't really do it for me either for this reason. The base classes "abtract-but-aren't-abstract" members HAVE To be present in my base classes because I have virtual and public members that depend on them. And in my case, because I cannot declare the class as abstract, I have to have an implementation for those members. As soon as I provide an implementation for those members then all of my inheriting classes ALSO have the implementation and the Interface doesn't complain at compile time. I thought by removing the virtual modifier from those members that the compiler would require them but it did not behave that way. I am beginning to think that my solution (document the members well) is the best solution.
Feb 20 2014

What is the best way to create "abstract" class members when declaring the class as abstract is not an option?

        <p>There is a class that I want to create as abstract but I can't because that class's base class is ActiveReports and as soon as I make it abstract the sub-reports are no longer designable.  (Interestingly the base class itself IS designable but not it's children).</p>
I know that I cannot declare class members as abstract without first declaring the class as abstract. In this case what is the NEXT BEST way to do this. At this point I made the members virtual and added comments to each one. Where it made sense I also declared a member as protected rather than public. But is there some other best way that would REQUIRE that these members be overridden at compile-time rather than run-time? If you were in this situation how would you do it?
Jul 23 2012

Do you have to call EndInvoke (or define a callback ) for asynchronous method calls even if you don’t have anything to do when it returns

I found the following code snippet on CodeProject on calling methods asynchronously at ... http://www.codeproject.com/Articles/14931/Asynchronous-Method-Invocation
private void CallFooWithOutAndRefParameters()
{
    // create the paramets to pass to the function
    string strParam1 = "Param1";
    int intValue = 100;
    ArrayList list = new ArrayList();
    list.Add("Item1");

    // create the delegate
    DelegateWithOutAndRefParameters delFoo =
      new DelegateWithOutAndRefParameters(FooWithOutAndRefParameters);

    // call the beginInvoke function!
    IAsyncResult tag =
        delFoo.BeginInvoke(strParam1,
            out intValue,
            ref list,
            null, null);

    // normally control is returned right away,
    // so you can do other work here...

    // calling end invoke notice that intValue and list are passed
    // as arguments because they might be updated within the function.
    string strResult =
        delFoo.EndInvoke(out intValue, ref list, tag);

    // write down the parameters:
    Trace.WriteLine("param1: " + strParam1);
    Trace.WriteLine("param2: " + intValue);
    Trace.WriteLine("ArrayList count: " + list.Count);
    Trace.WriteLine("return value: " + strResult);
}
There are a couple of things I don't understand about this code. Per the comment control is returned immediately to the calling code when it hits the BeginInvoke line. Does that mean that the code that follows (EndInvoke followed by some trace logging) only runs after the FooWithOutAndRefParameters call completes...automagically (even though that code resides in the same method). It looks a little confusing to me. (I have always used callbacks for this kind of thing.) Using this method do I HAVE to call EndInvoke. Can I just invoke the method asyncronously and forget it happened? Any downsides to this? If I don't call EndInvoke (as is shown in this method) should I then always have a callback? Even if the callback does nothing. If the answers YOU SHOULD...then do you call EndInvoke OR define a callback? (The advantage to defining a callback being that you are notified of the result) BTW I know I could check for errors or log resuls in the EndInvoke or callback (and I might in fact do that). What I was wondering is ARE THERE RISKS from not calling EndInvoke or defining a callback (memory leaks for example)? What is the best practice. Seth
Feb 20 2012

What is the best design practice when you have a class where properties are dependent on a web service call?

If I have a class with some read-only properties that are populated by a web service call, what is considered the best way to design this?

Is it considered proper for the property getters to make the web service call. It seems that the downside of this is that the getter is doing more than one thing and obscures the expense of the call. I realize that any of the property getters only needs to make the web service call once (by checking for nulls or flag before making the call). But that a single property getter could potentially be setting the private fields for other properties seems to smell to me.

On the other hand if I have have a public method (ie InitWebServiceVals) that calls the web service and updates the private fields I am creating a temporal dependency between the method and the property getters. So the API obscures the fact that you shouldn't read a property until the "InitWebServiceVals" is called.

Or is there some other method or pattern that addresses this? For example, making the webservice call in the constructor? Or is this generally indicative of a design issue?

I have run into this issue a number of times and I always ended up preferring the second method to the first.

Any thoughts?

Seth

Jan 19 2012

How do I get .NET WinForms components to use a SET timezone instead of clients time zone?

        <p>I was not looking forward to describing this problem until I found another forum web site with the EXACT SAME question I need answered (but with no answer.)  So with credit to <a href="http://www.hightechtalks.com/dotnet-framework-winforms/how-set-timezone-runtime-single-213152.html" rel="nofollow">this guy</a> here is my question...  </p>
We have a large Windows .Net application (winform executable) that gets installed on the client's desktop. This application calls web services on a server in a different timezone. Virtually all date oriented components detect the timezone difference and automatically adjust the datetime values (returned in dataSets generated by SQL queries), which is generally desirable in most applications but causing problems with accounting related applications that are "date" not "datetime" oriented. We are only interested in the "date" portion. However, a date of 1/1/2003 GMT-5 is automatically converted to 12/31/2002 11:00 GMT-6 on the client. Rather than going through all of the code and extracting the UniversalTime to get back to 1/1/2003 for visual purposes we would like to simply "fake" the timezone for the client-side executable by making it think it's in the same timezone as the server. Question: Can we set the TimeZone programmatically for the currently running instance only, rather than the global setting?
I really don't have much to add because it is our EXACT issue. In our case we have ActiveReports that are fetching remote SQL data into a datasets and then binding the report to the dataset. So, for example, birthdays are wrong because we are storing the Date for them and X hours are subtracting for the date in the western time zones? So the birthdates are off by minus 1. Any thoughts? Thanks! Seth
Jan 19 2012

How do I get .NET WinForms components to use a SET timezone instead of clients time zone?

I was not looking forward to describing this problem until I found another forum web site with the EXACT SAME question I need answered (but with no answer.) So with credit to this guy here is my question...

We have a large Windows .Net application (winform executable) that gets installed on the client's desktop. This application calls web services on a server in a different timezone. Virtually all date oriented components detect the timezone difference and automatically adjust the datetime values (returned in dataSets generated by SQL queries), which is generally desirable in most applications but causing problems with accounting related applications that are "date" not "datetime" oriented. We are only interested in the "date" portion. However, a date of 1/1/2003 GMT-5 is automatically converted to 12/31/2002 11:00 GMT-6 on the client. Rather than going through all of the code and extracting the UniversalTime to get back to 1/1/2003 for visual purposes we would like to simply "fake" the timezone for the client-side executable by making it think it's in the same timezone as the server.

Question: Can we set the TimeZone programmatically for the currently running instance only, rather than the global setting?

I really don't have much to add because it is our EXACT issue. In our case we have ActiveReports that are fetching remote SQL data into a datasets and then binding the report to the dataset. So, for example, birthdays are wrong because we are storing the Date for them and X hours are subtracting for the date in the western time zones? So the birthdates are off by minus 1.

Any thoughts?

Thanks!

Seth

Jan 05 2012

How do I create a global exception handler for winforms app that is making asyncronous WebClient calls

I have a winforms app that is making asyncronous WebClient calls with a callback procedures as follows...

using (var wc = new WebClient())
{
    wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
    wc.DownloadProgressChanged+=new DownloadProgressChangedEventHandler(DownloadProgressCallback);
    //other stuff
    wc.DownloadFileAsync(uri, fullLocalPath);
}

I want to create a global exception handler so I have defined an application event for it...

[STAThread]
static void Main()
{
    Application.ThreadException += new ThreadExceptionEventHandler(ApplicationThreadException);
    //other stuff
    Application.Exit();

}

private static void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
{
    // Do logging or whatever here

    Application.Exit();
}

I believe that I am correct that application errors that are raised by the Callback event will not be caught by the Application.ThreadException? So what is the best way of insuring that the callback exceptions are handled?

I have seen on other SO posts that you can also create a handler for AppDomain.CurrentDomain.UnhandledException. Is that the best way to handle the callback exceptions?

I am just looking for best practices when using asyncronous callbacks in a winforms app.

EDIT

Nicholas...thanks for your answer. So can I put the whole callback into a try catch and then do a throw in the catch. Or do I have to explicitly check the AsyncCompletedEventArgs.error property for a non-null value.

If it IS non-null...can you tell me how to cause the callback error to bubble up to my global exception handler?

I think I would do it like this...

private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{

    if (e.Error != null)
    {
        throw e.Error;
    }

    if (DownloadHasCompleted == null) return;
    File.Copy(this.RemoteIniFilePath, this.CurrentIniFilePath, true);
    DownloadHasCompleted(this, e);
}

Seth