Friday, October 26, 2007

.NET Daily Tips

I stumbled across this website and thought I'd share it with everyone. Visit this site frequently for some little known tips you might not have ever known about regarding .NET

http://dotnettipoftheday.org/tips/

Tuesday, September 11, 2007

Resetting Visual Studio 2005 to Original Settings

NOTE: Issuing the following command will clear any custom and environmental settings.

You may come across a situation where your Visual Studio 2005 Environment is non-functional in one manner or another. This recently happened to me when I was trying to reset the environment to the General Environment settings using the Import and Export Settings Wizard. During the process I encountered an error which left the IDE in a state in which it would try to Initialize the Toolbox and after a few seconds close Visual Studio. Thus I was unable to access any of the ToolBox controls.

After a quick search I came across a command that you run from the Visual Studio Command Prompt to reset the IDE to its original install settings. If after exhausting all possible repair options you need to run this command please backup any custom and/or environmental settings you do not want to lose prior to its use.

Then perform the following:

1. Click Start and then select All Programs.
2. Select Microsoft Visual Studio 2005.
3. Select Visual Studio Tools and then Visual Studio 2005 Command Prompt.
4. Enter devenv /resetuserdata.

You will not receive any confirmation of reset completion, so you can use task manager to monitor the devenv process for its completion.

Friday, August 10, 2007

Symbols, Accents, Punctuation, and Foreign Characters

Have you ever needed to type special symbols or characters such as the Registered Trademark Symbol, Copyright Symbol, or perhaps an Umlaut or the Math Division Symbol in a document or web page? Trying to remember the codes that represent them or having to reference some web page that list them can be really impractical. There is a much easier way to access these symbols.

Many people are unaware of a small program that comes with Windows and Linux Distributions that provides them for you along with their associated codes. This same type of program I'm sure comes with OSX as well as other Operating Systems; however, I am unable to verify this due to my lack of access to the numerous number of Operating Systems available.

In Windows 2000/XP/Vista

1. Click the Start Button.
2. Select All Programs.
3. Select Accessories.
4. Select System Tools.
5. Select Character Map.

In Ubuntu Linux (Other Linux Distros may have similar steps)

1. Click Applications from the Task Bar.
2. Select Accessories.
3. Select Character Map.

Whenever you need to make use of a special symbol or foreign character just to mention a couple types; this is a very simple way that is readily available to you. No need to search for the codes for such symbols via the Internet or consult some other reference material.

Wednesday, July 25, 2007

Accessing Master Page Controls via Content Pages

There are a couple of ways to access the controls of Master Pages in ASP.NET 2.0 from within your content pages. You can do this in a non-strongly typed manner or a strongly type manner. I will provide a sample below that demonstrates each approach.

Non-Strongly Typed

Lets say your Master Page contains a Label that displays the current Date. Within your content page use the FindControl method to locate the control in the Master Page whose property you want to access. Note you must perform an explicit cast.

Label lbl = (Label)Master.FindControl("DateLabel");
lbl.Text = DateTime.Today.ToLongDateString();

Strongly Typed

In the code-behind of your Master Page create a property that accesses the control you want to manipulate.

public string GetCurrentDate
{
get { return DateLabel.Text; }
set { DateLabel.Text = value; }
}


In the HTML source of your content page add the following beneath the Page <%@ Page %><%@ Page %><%@ Page %>directive. Add MasterType before TypeName.


< typename="Site">

Replace "Site" with the class name of your Master Page. You could also re-write the above using VirtualPath in place of TypeName. NOTE: Blogger will not display the percent signs that are between the <> as well it also Strips out the MasterType Directive.

To do this just replace the MasterPage.master with the name of your Master Page.

Finally in the code-behind of your content page you can set the DateLabel to the current date on your Master Page as below:

this.Master.GetCurrentDate = DateTime.Today.ToLongDateString();

Thursday, July 12, 2007

Free Web Services and SQL Connection Strings

I wanted to share the following resources I came across hopefully if you ever need them they'll be helpful to you.

Web Services

http://www.webservicex.net/WCF/default.aspx
http://www.xmethods.net/
http://www.webservicelist.com/

SQL Connection Strings

http://www.connectionstrings.com/
http://www.carlprothman.net/Default.aspx?tabid=81

Thursday, June 21, 2007

More Useful C# Code Snippets

Display Pop-Up Error Messages In An ASP.NET Application

I came across an issue in which someone needed to pop-up a Window to display an error message to users in their ASP.NET application. I did some research and discovered this very handy bit of code that will display a JavaScript Alert MessageBox that you can populate with the error message.

Below is an example I created that purposely causes a DivisonByZero Exception and displays this in a JavaScript Alert MessageBox.


int num1 = 10;
int num2 = 0;
int result;

try
{
result = n1 /n2;
}
catch (DivideByZeroException ex)
{
DisplayErrorMsg(ex);
}


private void DisplayErrorMsg(Exception ex)
{
RegisterStartupScript(Guid.NewGuid().ToString(),
string.Format("<script language='Javascript'>alert('{0}');</script>", ex.Message));
}

Populate The Display Text Of A CollapsiblePanel Control From A File

I had the need to populate the text to be displayed in an ASP.NET AJAX CollapsiblePanel Control via a text file as opposed to just embedding the text between a tag of my Content Panel. I had to display a large amount of text that displayed some Terms and Conditions and there was just to much to put between the tags. Besides it really would make the code ugly having all of that text embedded.

The solution I decided to go with was to create a Web User Control and fill the control with the text to be displayed. Then I went back to the page that I was going to display this text on and dropped the Custom Control inside of my CollapsiblePanel's ContentPanel. I then went into the HTML source of the page and surrounded the ContentPanel with a Div tag and sat the height, overflow, and background color.






<ajaxToolkit:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="server"
TargetControlID="ContentPanel"
ExpandControlID="TitlePanel"
CollapseControlID="TitlePanel"
Collapsed="false"
TextLabelID="Label1"
ExpandedText="(Hide Details...)"
CollapsedText="(Show Details)"
ImageControlID="Image1"
CollapsedImage="images/expand.jpg"
ExpandedImage="images/collapse.jpg"
SuppressPostBack="true">
</ajaxToolkit:CollapsiblePanelExtender>


<asp:Panel ID="TitlePanel" runat="server" Height="30px" Width="545px">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/expand.jpg"/>
Terms and Conditions
<asp:Label ID="Label1" runat="server" Text="Label">(Show Details ...)</asp:Label>
</asp:Panel>
<asp:Panel ID="ContentPanel" runat="server" Height="0px" Width="545px">                                
<div style="height:150px;overflow:scroll;background-color:ButtonFace;" >
<uc2:Terms ID="Terms1" runat="server" /></div>
</asp:Panel>

Friday, June 8, 2007

Useful C# Code Snippets

Mapping a month name to its chronological order value

The following C# code can be used to map a month name to its equivalent numeric chronological order value. You can use this code in a method and pass the month in as a string to convert for example "JUN" to 06. You can modify the array to map the full month name in place of it's three letter abbreviation.

public string MonthToNumberMapper(string month)
{
string Month = month.ToUpper();
string ConvertedMonth = null;
string[,] MonthsToConvert = new string[,]
{
{ "JAN", "01" }, { "FEB", "02" }, { "MAR", "03" }, { "APR", "04" },
{ "MAY", "05" }, { "JUN", "06" }, { "JUL", "07" }, { "AUG", "08" },
{ "SEP", "09" }, { "OCT", "10" }, { "NOV", "11" }, { "DEC", "12" }
};

// Determine the numeric month.
for (int i = 0; i < MonthsToConvert.Length / 2; i++)
{
if (Month == MonthsToConvert[i, 0])
{
ConvertedMonth = MonthsToConvert[i, 1];
break;
}
}

return ConvertedMonth;
}

Converting the text in a ComboBox Control to Uppercase
The following C# code can be placed in the TextChanged event handler of a ComboBox Control to change the case of the text entered to uppercase. This works for text entered into or selected within the ComboBox Control.

private void StateComboBox_TextChanged(object sender, EventArgs e)
{
String stateText = (string)StateComboBox.Text;
stateText = stateText.ToUpper();
StateComboBox.Text = stateText;
installStateComboBox.SelectionStart = installStateComboBox.Text.Length;
}