Monday, September 30, 2013

using where condition like sql in .net | Linq query to fetch rows based on where condition


HI lets see how to fetch records in LINQ based on the where condition specified.

Example:

var query = from r in dtSampleDataTable.AsEnumerable()
                        where r.Field<string>("Salary") == "1000"
                        select r;

            if (query.Count() > 0)
            {
                DataTable dt1 = query.CopyToDataTable(); //fetching filtered rows to a new datatable.
                object sumSalary = dt1.Compute("Sum(Salary)", "");
            }

string s = sumSalary.ToString();

LINQ startswith,EndsWith example | Asp.net LINQ Query to get or fetch rows from a datatable for a coloumn starting with any letter,word,Number

Hi in this post i will show using LINQ query how to fetch rows from a dataTable for a column starting or ending with any letter,word,Number etc.

Example:

1. here i will fetch rows from a datatable for Names Ending with 'ar'

var query = dtSampleDatatable.AsEnumerable()
                 .Where(row => row.Field<string>("Name").EndsWith("ar"));

            DataTable dtSampleDatatableNew = new DataTable();
            dtSampleDatatableNew = query.CopyToDataTable(); // Adding filtered rows to a new datatable.

2. To fetch Names starting with 'ar'

var query = dtSampleDatatable.AsEnumerable()
                 .Where(row => row.Field<string>("Name").StartsWith("ar"));

            DataTable dtSampleDatatableNew = new DataTable();
            dtSampleDatatableNew = query.CopyToDataTable(); // Adding filtered rows to a new datatable.

Friday, September 27, 2013

textbox float value validation | Using Jquery in asp.net to validate and allow only float,integer,decimal values in the textbox


hi in this post i will show how to validate and allow only float,integer,decimal values in asp.net textbox.

Example:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script language="javascript" type="text/javascript" >
 $(document).ready(function () {
     $("#<%= TextBox1.ClientID %>").keypress(function (event) {

                if (event.which < 46 || event.which > 59) {
                    event.preventDefault();
                } // prevent if not number/dot

                if (event.which == 46 && $(this).val().indexOf('.') != -1) {
                    event.preventDefault();
                } // prevent if already dot
                
            });
        });

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

Tuesday, September 17, 2013

check whether checkbox is checked or not in Jquery Asp.net | validate checkbox using Jquery

hi in this post i will show how to validate a checkbox in Jquery.

Example :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    SELECT Laptops:
    <br />
        <asp:CheckBox ID="CheckBox1" Text="HP" runat="server" /><br />
        <asp:CheckBox ID="CheckBox2" Text="DELL" runat="server" /><br />
        <asp:CheckBox ID="CheckBox3" Text="ACER" runat="server" /><br /><br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    </div>
    </form>
</body>
</html>

1. to check atleast one checkbox should be checked from the list of checkboxes

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('input[id$=btnSubmit]').click(function (e) {
            var checked = $(':checkbox:checked').length; //checks whether atleast one check box is checked from the list of checkboxs.
            if (checked == 0) {
                alert('Select atleast one checkbox');
                e.preventDefault();
            }
            else {
                alert('all validations true');
            }
        });
    });
</script>


2. to validate a specific checkbox 

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('input[id$=btnSubmit]').click(function (e) {
            var checked = $('#CheckBox1').is(':checked'); // for a specific checkbox
            if (checked == 0) {
                alert('First Checkbox Should be checked');
                e.preventDefault();
            }
            else {
                alert('all validations true');
            }
        });
    });
</script>

Friday, September 13, 2013

calculating days,months,years difference between two dates in asp.net,C# | calculating date difference using timespan to calculate days,months,years

hi in this post i will show how to calculate days,months and years between two date in asp.net,c#(code-behind):

Below is the code for this:

public void calcDayMonthYear()
{

        DateTime dayStart;
        DateTime dateEnd;

        dayStart = Convert.ToDateTime(frmdate.Text);
        dateEnd = Convert.ToDateTime(todate.Text);
        TimeSpan ts = dateEnd - dayStart;

        double Years = Convert.ToDouble(ts.TotalDays) / 365;
        double Months = Years * 12;
        double Days = Convert.ToDouble(ts.TotalDays); 


}