Follow by Email

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); 


}

example to call a javascript function from code-behind or server-code using ScriptManager in asp.net,c#

hi in this post i will show how to call a javascript function from codebehind in asp.net :

Example:

aspx.cs page(codebehind):

 protected void testMethod()
    {
 ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage1", "ShowalertMessage();", true);
    }

aspx page:
 <script type="text/javascript" language="javascript">
  function ShowalertMessage() {
            alert('hello world!');
        }
    </script>

Tuesday, September 10, 2013

add items to dropdown in asp.net from code behind | Add items manually to dropdownlist in asp.net | using listitem in asp.net

hi in this post i will show how to add items into dropdownlist from codebehind using listitem in asp.net

aspx:

  <asp:DropDownList ID="ddltest" runat="server" OnSelectedIndexChanged="ddltest_OnSelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>


aspx.cs:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            ListItem l1 = new ListItem("--Select--", "0");
            ListItem l2 = new ListItem("Mobiles", "1");
            ListItem l3 = new ListItem("Laptops", "2");
            ListItem l4 = new ListItem("Headphones", "3");

            ddltest.Items.Add(l1);
            ddltest.Items.Add(l2);
            ddltest.Items.Add(l3);
            ddltest.Items.Add(l4);
        }
    }

Sunday, September 1, 2013

Using Generics in C#.net | Simple Example of generics .net

Using generics we can pass the data type as a parameter, thus it maximizes the code re-usability. The data type is passed while creating the object of that generic class. The datatype can be any thing like int,double,string, class etc.

Generics lets us create a type safe list at the compile time.

Example : Here we will use a Generic Method which will add for int type and concat for string type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class GenericList<T> // here T is a unknown data type
    {
        public T Add(T inputx,T inputy) {

            dynamic a = inputx;
            dynamic b = inputy;
            return a + b;
           
        }
    }
    public class Class1
    {
        static void Main(string[] args)
        {
            GenericList<int> list1 = new GenericList<int>();

            int result = list1.Add(10, 20);
            Console.WriteLine(result);
            //Console.ReadLine();

            GenericList<string> list2 = new GenericList<string>();

            string result2 = list2.Add("Chandan", " Singh");
            Console.WriteLine(result2);
            Console.ReadLine();
        }
    }
}

Output: