Pages

Wednesday, September 11, 2013

Async function Call on button click or page load

public delegate string LoadDelegateR(string userName, string reportingDate, Dictionary<string, string> functions, string jobNumber, out string message);

protected void Page_Load(object sender, EventArgs e)
{
 // call asynchronously                       
 LoadDelegateR d = new LoadDelegateR(ImportData);
 d.BeginInvoke(user, txtPeriod.Text.Trim(), functions, txtJobNumber.Text.Trim(), out message, new System.AsyncCallback(SendNotification), null);
}

public override string ImportData(string username, string reportingDate, Dictionary<string, string> functions, string jobNumber, out string message)
{
}

public void SendNotification(IAsyncResult iRes)
{
 try
 {
  AsyncResult ar = (AsyncResult)iRes;
  LoadDelegateR bp = (LoadDelegateR)ar.AsyncDelegate;
  string result = bp.EndInvoke(out message, iRes);
 }
 catch (Exception ex)
 {
  message = "Error in loading the data.Please contact admin.";
 }
}

Sort the IEnumerable list using Linq

var sortedEngData = engData.OrderByDescending(c => c.CausalCountPeriod).ThenByDescending(c => c.CausalCountToDate); 

Get distinct rows from datatable using Linq

var engData = (from dr in dtEng.AsEnumerable()
       where Convert.ToString(dr["job_number"]) == jobNumber
       && Convert.ToString(dr["period"]) == per.Periods
       && Convert.ToString(dr["office_short_name"]) == off.OfficeShortName
       select new
       {
        GroupCode = Convert.ToString(dr["group_code"]),
        GroupShortName = Convert.ToString(dr["group_short_name"]),
        CausalCountPeriod = (dr["casual_count_period"] == DBNull.Value || Convert.ToString(dr["casual_count_period"]) == "") ? Convert.ToInt32("0") : Convert.ToInt32(dr["casual_count_period"]),
        CausalCountToDate = (dr["casual_count_to_date"] == DBNull.Value || Convert.ToString(dr["casual_count_to_date"]) == "") ? Convert.ToInt32("0") : Convert.ToInt32(dr["casual_count_to_date"]),
        ReportingPeriod = Convert.ToString(dr["reporting_period"])

       }).Distinct();

Filter data and store it in datatable

DataTable dtEng = new DataTable();
DataRow[] drEngg = dt.Select("work_type = 'Engineering'");
if (drEngg.Length > 0)
{
 dtEng = drEngg.CopyToDataTable();
}

Friday, September 6, 2013

Remove totally blank rows from datatable

Below code will remove totally blank rows from datatable that were fetched from excel during upload

DataTable dtStupByDate = dtStupByDate.Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).CopyToDataTable();

Useful Jquery syntax

// Checks the count of the checkboxes checked
var n = $("input[type=checkbox]:checked").length;
//Prevents the button submit
$("#btnUpload").click(function (event) { event.bubbles = false; });
//Checks if radio button is checked
$('#radConst_0').is(':checked')
//Checks if check box is checked
$('#chkConst').is(':checked')
// Makes the control disable
$("#divRwkFunctions").attr("disabled", "disabled");
// clears all the checked boxes
var m = $("input[type=checkbox]").removeAttr('checked');
// Select the radio button
$('#ctl00_MainContent_radConst_1').attr('checked', 'checked');
// Adds the click event on all the checkboxes
$("input[type=checkbox]").click(function () {
                chkFunctionStatus();
            });
//Clear the File upload control//For IE
$("#fUpload").replaceWith($("#ctl00_MainContent_fUpload").clone(true));
//For other browsers
$("#fUpload").val("");

Show loading image for 5 secs after button click

function showLoading() {
 document.getElementById('myHiddenDiv').style.display = "";
 setTimeout('document.images["myAnimatedImage"].src="work.gif"', 500000000);  
}


<span id='myHiddenDiv' style='display: none;' align="Center">
 <img id="myAnimatedImage" src="Images/ajax-loader.gif" align="middle" alt="" />
</span>


<asp:Button ID="btnUpload" runat="server" Text="Submit" CssClass="button" OnClientClick="return showDiv();" />

Jquery datepicker with month and year only


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
 Home Page
</title><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script type="text/javascript" src="
http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="
http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css">
<script type="text/javascript">
        $(document).ready(
    function () {
        $('#txtPeriod').datepicker(
        {
            changeMonth: true,
            changeYear: true,
            dateFormat: 'MM yy',
            onClose: function () {
                var iMonth = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                var iYear = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
            },
            beforeShow: function () {
                if ((selDate = $(this).val()).length > 0) {
                    iYear = selDate.substring(selDate.length - 4, selDate.length);
                    iMonth = jQuery.inArray(selDate.substring(0, selDate.length - 5), $(this).datepicker('option', 'monthNames'));
                    $(this).datepicker('option', 'defaultDate', new Date(iYear, iMonth, 1));
                    $(this).datepicker('setDate', new Date(iYear, iMonth, 1));
                }
            }
        });
    });
</script>
<style>
        .ui-datepicker-calendar
        {
            display: none;
        }
        .red
        {
            color: Red;
            font-size: 10px;
        }
       
        td
        {
            text-align: left;
        }
        #divValidate
        {
            text-align: center;
        }
    </style>
<div id="divPeriod" style="display: none;">
 Period :
 <asp:TextBox ID="txtPeriod" runat="server" Width="100px" onkeydown="return false;"></asp:TextBox>
</div>
</body>
</html>