Wednesday

base page asp.net error handling

basepage class:
 
public delegate void UnSafeProcedure();
public class BasePage : System.Web.UI.Page
{
public CAssertions assert= new CAssertions(); // my own assertor, produce AssertionException
public void RunSafe(UnSafeProcedure s)
{
try
{
s();
}
catch (Exception e1)
{
if (e1.GetType().FullName == "AssertionException")
{

ShowMessage(e1.Message);

}
else
{
ShowMessage("Unhandled Exception");
Debug.Write(e1.ToString());
}
}

}
// will be overwrited on actual page:
public virtual void ShowMessage(string s) { Debug.Write(s); }
}



This is how actual page would look like:
  
public partial class NewEft : BasePage {
protected void SubmitButtonClick(object sender, EventArgs e) {
RunSafe(Submit);
}

private void Submit() {

AUtyl butyl = new AUtyl();
assert.IsTrue(butyl.CheckRoutingNumberByRouting(txtRoutingNumber.Text), "Routing Number is invalid");
}

public override void ShowMessage (string s ) { lblMessage.Text = s; }
}




more...

get source from github (windows)

1.download and install git from
googlecode

Bash installation is good enough
2.click on "Git Bash" on desktop, you can change folder
with "cd" command like cd c:\temp\


3.run git clone command to get sources:
git clone [link from github]

for example:
git clone http://github.com/twilio/stashboard.git
more...

Tuesday

c# parse string into enum


public enum enmode
{
fee , scheduledpayment , adjustment , new1 , del , cancel , freeze
}


Propery convering string from Request["mode"] into enum:

public enmode Mode
{
get { return (enmode) Enum.Parse(typeof (enmode), Request["m"].ToLower()); }
}

more...

open popup in center of screen ( popup window javascript )

      
function openPopUpInCenter() {
var w1 = 400;
var h1 = 450;
var left = (screen.width / 2) - (w1 / 2);
var top = (screen.height / 2) - (h1 / 2);
window.open('AddPopup.aspx', 'add', 'top=' + top + ',left=' + left + ',width=' + w1 + ',height=' + h1 + ',location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no');
}

more...

Thursday

iis7 enable asp and asp.net


You need to install ASP/ASP.NET through the Windows setup interface available from the Control Panel \ Programs applet:

Tuesday

close popup window c#


string clientScript = @"<script language='javascript'>
window.opener.location.reload();
window.close();
</script>";

this.Page.ClientScript.RegisterStartupScript(clientScript.GetType(),"_reload",clientScript);


more...

opener reload javascript

to close popup and reload main window:

<script language='javascript'>
window.opener.location.reload();
window.close();
</script>

register javascript asp.net c#

     
string clientScript = @"<script language='javascript'>
alert("hello");
</script>";


this.Page.ClientScript.RegisterStartupScript(clientScript.GetType(),"hello",clientScript);

remove space above table in Blogger.com

when you publishing table on Blogger.com you will have a bunch of space above table.
Because Blogger.com adds a
<br />

tag for each new line
To avoid this, add this before HTML for your table:

<style type="text/css">.nobrtable br { display: none }</style>
<div class="nobrtable">

credit card type based on number

first numbers identifying card issuer:





























IssuerIdentifierCard Number Length
Diner's Club/Carte Blanche300xxx-305xxx,
36xxxx, 38xxxx
14
American Express34xxxx, 37xxxx15
VISA4xxxxx13, 16
MasterCard51xxxx-55xxxx16
Discover6011xx16















first digitIssuer Category
0ISO/TC 68 and other industry assignments
1Airlines
2Airlines and other industry assignments
3Travel and entertainment
4Banking and financial
5Banking and financial
6Merchandizing and banking
7Petroleum
8Telecommunications and other industry assignments
9National assignment


jquery iframe contents

JQuery allows to open regular popup in nice dialog window by using iframe,
here is code how to do that:
        
<html>
<head>
<link rel="stylesheet" href="./styles/smoothness/jquery-ui-1.7.2.custom.css" type="text/css" media="screen" />
<script type="text/javascript" src="./scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="./scripts/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">
$(function() {
$(' .diag').click(function(e) {
e.preventDefault();
var $this = $(this);
var horizontalPadding = 30;
var verticalPadding = 30;
$('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
autoOpen: true,
width: 800,
height: 500,
modal: true,
resizable: true,
autoResize: true,
overlay: {
opacity: 0.5,
background: "black"
}
}).width(800 - horizontalPadding).height(500 - verticalPadding);
});
});
</script>
</head>
<body>
<ul>
<li><a href="http://www.google.com" class ='diag' title="Google Dialog">Google</a></li>
</ul>
</body>
</html>

more...

Sunday

show hide javascript

Here is sample how to show hide, control in asp.net/javascript
let say we have ddBerechnung control and we have to hide controls when this dropdown has value 'identisch'

server side will have this :
         
protected override void OnPreRender(EventArgs e)
{
ddBerechnung.Attributes.Add("onChange", "ShowHidePercentField(this)");
}


and aspx will have this javascript:
         
function ShowHidePercentField(cb){
var selIdx = cb.selectedIndex;
var newSel = cb.options[selIdx].text;
var textBox=document.getElementById('<%=txtBerechnung.ClientID%>');
var Runden= document.getElementById('<%=ddRunden.ClientID%>');
var RundenSpan= document.getElementById('RundenSpan');


if (trim(newSel)=='identisch'){ ///
textBox.style.display="none";
Runden.style.display="none";
RundenSpan.style.display="none";

}else{
textBox.style.display="";
Runden.style.display="";
RundenSpan.style.display="";

}

}

///.... at the end of control
<script language='javascript'>
var elem=document.getElementById('<%=ddBerechnung.ClientID%>')
ShowHidePercentField(elem);
</script>

more...

tsql ceiling round not working for percent calculation because

it's required to put additional zero after 100.0, check this out:

select CEILING(50/100.0) , CEILING(50/100)
----
1,0





more...

Saturday

Controls.add c#/asp.net adding controls table/rows/labels

ASPX page must have placeholder

<asp:PlaceHolder ID="DynamicPlaceHolder" runat="server"></asp:PlaceHolder>


codebehind

protected override void OnPreRender(EventArgs e)
{
HtmlTable mainHtmlTable = new HtmlTable();
HtmlTableRow helpTableRow = AddLabelRow(mainHtmlTableRow.Cells.Count, "Hello World",true);
mainHtmlTable.Rows.Add(helpTableRow);
DynamicPlaceHolder.Controls.Add(mainHtmlTable);
}

public HtmlTableRow AddLabelRow(int cellCount, string text,bool applySkin)
{
HtmlTableRow helpTableRow = new HtmlTableRow();
HtmlTableCell helpTableCell = new HtmlTableCell();
//helpTableCell.Attributes.Add("style", "border: solid 2px; border-color: Blue;");
helpTableCell.ColSpan = cellCount;

Label helpLabel = new Label();
if (applySkin) helpLabel.SkinID = "labelBoldSkin";
helpLabel.Text = "<br>"+text;

helpTableCell.Controls.Add(helpLabel);
helpTableRow.Cells.Add(helpTableCell);
return helpTableRow;
}

more...

Attributes.Add style


change font weight:
         
helpLabel.Attributes.Add("style", "font-weight:bold");


change color
         
helpTableRow.Attributes.Add("style", "color:red;");


more...

Thursday

start of the week C#

     
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}

return dt.AddDays(-1 * diff).Date;
}
}

Usage:
     
dateFrom.Value = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);

what day is the start of the week

Sunday :)
more...

datatable copy c#

here is procedure for copying data b/w different fields in two tables
         
public DataTable CopyData(DataTable indata, Hashtable fields)
{
DataTable ret = new DataTable();
foreach (string k in fields.Keys)
{
ret.Columns.Add((string)fields[k], indata.Columns[k].DataType);
}
foreach (DataRow row in indata.Rows)
{
DataRow dr = ret.NewRow();
foreach (string k in fields.Keys)
{
dr[(string)fields[k]] = String.Format("{0}", row[k]);
}
ret.Rows.Add(dr);
}
return ret;
}

create mapping b/w fields in original table and result table and call procedure
         
Hashtable fieldsMap= new Hashtable()
{
{"[Measures].[Week]","Week"},
{"[Measures].[Month]","Month"},
{"[Measures].[Quarter]","Quarter"},
{"[Measures].[Year]","Year"},
};

// call copy data procedure
DataTable result = CopyData(OriginaldataTable, fieldsMap);



more...

datatable.columns.add

         
DataTable table = new DataTable("MyTable");
Type str = System.Type.GetType("System.String");
table.Columns.Add("Checbox", System.Type.GetType("System.Boolean"));
table.Columns.Add("String1", str);
table.Columns.Add("String2",str);
table.Columns.Add("Decimal", 0.0.GetType());

create datatable on the fly

// here is procedure for creating table on the fly
   
private DataTable CreateTable()
{
DataTable table = new DataTable("Report");
Type str = System.Type.GetType("System.String");
table.Columns.Add("chk1", System.Type.GetType("System.Boolean"));
table.Columns.Add("From", str);
table.Columns.Add("To",str);
table.Columns.Add("Hours", 0.0.GetType());
table.Columns.Add("Desc", str);
return table;
}

// below is procedure for filling table with data
private void ProcessReport()
{
xreport = XDocument.Load(ReportFile.Text);
var header = (from head in xreport.Elements("Journal").Elements("Header")
select head).First();

Rate.Text= header.Attribute("Rate").Value;

var entries = from entri in xreport.Elements("Journal").Elements("Entry")
select entri;
DataTable dataTable = CreateTable();
foreach (var element in entries)
{
DataRow dr = dataTable.NewRow();
dr["From"] = element.Attribute("StartTimeStamp").Value;
dr["To"] = element.Attribute("EndTimeStamp").Value;
dataTable.Rows.Add(dr);
}
Grid.DataSource = dataTable;
}

more...

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...