Programming Journal C#, Java, SQL

Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Sunday, May 25, 2014

Generate Random Unique Identifier in Java

Here's a quick one on how to generate random numbers.
                UUID mId;  //be sure to import java.util.UUID;
  //generate random unique identifier
  mId = UUID.randomUUID();

Friday, May 23, 2014

Beginning Android Development

I started learning Android Development a couple weeks back. I began by downloading necessary components:

  • Java SDK
  • ADT (with Eclipse and Android Libraries)
The emulator was terribly slow on my hardware so I downloaded the Android device driver for my Android device. This is required for debugging from USB port with your device.

Next, I tried the tutorials at Google: http://developer.android.com/training/index.html
The few tutorials that I tried were frustrating because of compatibility issues between versions. I purchased the Kindle book, "Android Programming: The Big Nerd Ranch Guide". I'm through 4 1/2 chapters so far and it is much better. I'll begin posting again to document any handy tips for Android development.

Override methods in Eclipse

Here is a shortcut to override methods in Eclipse:

  1. Right click -> Source -> Override/Implement Methods 
  2. Check box target method(s) 
  3. Click OK 


Reference: http://www.java-tips.org/other-api-tips/eclipse/how-to-override-a-method-from-a-base-class-using-ec.html

Tuesday, August 25, 2009

Installing Web Profile Generator on ASP.NET 3.5

I finally figure out how to do this. Unfortunately the technical writers were out to lunch when the tried to explain this. Here is my explanation on how to accomplish this in ASP.NET 3.5:

http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx

Install the MSI.
Add (under the other Imports), the <Import Project="$(MSBuildExtensionsPath)\WebProfileBuilder\WebProfileBuilder.targets" />](without []) line to the "MyProject.csproj" file (NOT the user project file, but the one without an extension on Vista) where MyProject is your Web Application Project name.
Add an App_Code folder to your project.
Add the WebProfile.cs file (rt. clicking folder and add existing item)
Rt. click the WebProfile.cs file and click properties.
Change its Build Action from Content to Compile.

Add the profile tags to the web.config file: e.g.

Now you are set. Just make a call to your Profile by setting up this method:

public static WebProfile Profile
{
//get { return new WebProfile(HttpContext.Current.Profile); }
get { return WebProfile.Current; }
}

It should be noted that when you change the Profile, you will need to remove the class from the App_Code and add the newly generated WebProfile.cs class as before.

Sunday, August 23, 2009

How to disable Internet Explorer 8 From Installing With Automatic Updates

To disable Internet Explorer 8 From Installing With Automatic Updates follow these directions:
Download the file from the link below and run.

References:

Saturday, August 15, 2009

Use log4net to Log Errors or Visitors

log4net is a free utility that can be used for logging data about errors or visitors. It is available here: http://logging.apache.org/log4net/index.html

Here is an introductory article: http://www.codeproject.com/KB/trace/Log4Net_with_ASP_NET.aspx

I think a useful way to track users is to log users by their IP Address. Once your log4net is installed correctly, the following code in the Page_Load event can be used to write IP Addresses to a file placed in the App_Code folder (where it is not public):


if (!Page.IsPostBack)
{
string nowip;
nowip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (nowip == null)
{
nowip = Request.ServerVariables["REMOTE_ADDR"];
}
log.Info("Visitor IP: " + nowip);
}


I'll include the BasePage for a reference to the log variable above (which the System.Web.Page inherits from):

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;

public class BasePage : System.Web.UI.Page
{
public static readonly ILog log = LogManager.GetLogger("Logger");
public BasePage()
{
initLog4Net();
//logAll();
}
/// <summary>
/// initializes Log4Net
/// Described at http://www.codeproject.com/KB/trace/Log4Net_with_ASP_NET.aspx
/// configured in web.config
/// level value can be ALL ERROR DEBUG FATAL DEBUG OFF WARN
/// </summary>
private void initLog4Net()
{
log4net.Config.XmlConfigurator.Configure();
}
/// <summary>
/// test log4net
/// </summary>
protected void logAllTest()
{
bool isDebugEnabled = log.IsDebugEnabled;
bool isErrorEnabled = log.IsErrorEnabled;
bool isFatalEnabled = log.IsFatalEnabled;
bool isInfoEnabled = log.IsInfoEnabled;
bool isWarnEnabled = log.IsWarnEnabled;
log.Debug("Debug msg");
log.Error("Error msg");
log.Fatal("Fatal msg");
log.Info("Info msg");
log.Warn("Warn msg");
}
}


References: http://www.aspxcode.net/free-asp-net-sample-source-code-c.aspx?Topics=how%20to%20get%20the%20IP%20Address%20of%20the%20Visitor

Sample SQLServer Databases

2005 and 2008 Sample SQLServer Databases are available here: MSFTDBProdSamples
Northwind and pubs databases are available here: Northwind and pubs

These are useful for testing .NET Data objects by using SQLDatasource as the DataSource.