Archive for the ‘ASP.Net’ Category

CodeBehind vs CodeFile

Posted: June 12, 2014 in ASP.Net

CodeBehind
Needs to be compiled and the compiled binary is placed in the bin folder of the website.

<%@ Page Title="" Language="C#" MasterPageFile="~/YourMasterPage.Master" AutoEventWireup="true" CodeBehind="YourFile.aspx.cs" Inherits="YourPage" %>

CodeFile
You provide the source file with the solution for deployment and .NET compiles the code when needed.

<%@ Page Title="" Language="C#" MasterPageFile="~/YourMasterPage.Master" AutoEventWireup="true" CodeFile="YourFile.aspx.cs" Inherits="YourPage" %>

Convert String to TimeSpan

Posted: March 15, 2012 in ASP.Net

To convert a String to TimeSpan, you can try the following:

TimeSpan myTimeSpan = TimeSpan.Parse("00:00:30");

To split a string by multiple characters into an array, you can try the following:

using System;
using System.Text.RegularExpressions;

class myClass
{
    static void Main()
    {
	string myValue = "line1\r\nline2\r\nline3";

	string[] myLines = Regex.Split(myValue , "\r\n");

	foreach (string myLine in myLines )
	{
	    Console.WriteLine(myLine);
	}
    }
}

Split String Into Array by Delimiter

Posted: December 14, 2011 in ASP.Net

To split a string by a delimiter into an array, you can try the following:

string myContact = "1|2|3|4|5";
string[] myContactArray = myContact.Split('|');
foreach (string myContactValue in myContactArray)
{
  Console.WriteLine(myContactValue);
}    

Iterate Through List Collection

Posted: December 14, 2011 in ASP.Net

To iterate through a List collection, you can try the following:

List<int> myList = new List<int>();
myList .Add(1);
myList .Add(2);
myList .Add(3);

// Loop through List with foreach statement
foreach (int myInt in MyList) 
{
  Console.WriteLine(myInt);
}

// Loop through List with for statement
for (int i = 0; i < MyList.Count; i++) 
{
  Console.WriteLine(MyList[i]);
}

Insert Double Quotes (“) Into String

Posted: December 13, 2011 in ASP.Net

To insert double quotes (“) into a string, you can try the following:

string myString = "\"someString\"";

Many people store PDF invoices in their tables as binary data.  To extract and save this as a PDF document, you can try the following:
Page1.apsx (Get Invoice)

Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase DAL = 
new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(YourConnectionString);
System.Data.Common.DbCommand dbgCommand = DAL.GetStoredProcCommand("YourProcedureName");
DAL.AddInParameter(dbgCommand, "@InvoiceNo", System.Data.SqlDbType.Int, InvoiceNo);
IDataReader rdr = DAL.ExecuteReader(dbgCommand);
try
{
  System.Text.StringBuilder _sb = new System.Text.StringBuilder();
  while (rdr.Read())
  {
    Session["sInvoiceData"] = rdr["YourInvoiceDataColumn"];
    Session["sInvoiceFilename"] = rdr["YourInvoiceFilenameColumn"].ToString();
    _sb.Append("window.open('Page2.aspx','_self',");
    _sb.Append("'toolbar=0,menubar=0,resizable=yes')");
  }
  ScriptManager.RegisterStartupScript(Page, Page.GetType(), "winOpen", _sb.ToString(), 
  true);
}
finally
{
  rdr.Close();
  rdr.Dispose();
}

Page2.apsx (Download Invoice)

try
{
  Response.ContentType = "application/pdf";
  Response.AddHeader("content-disposition", "attachment; filename=" + 
  Session["sInvoiceFilename"]);
  Response.BinaryWrite((byte[])Session["sInvoiceData"]);
  Session["sInvoiceData"] = null;
  Session["sInvoiceFilename"] = null;
}
catch (System.Threading.ThreadAbortException exThread)
{
  // Do nothing
}
catch (Exception ex)
{
  // Catch exception
}