Menu ▼

Home / blogs /

<< < 1 2 3 4 5 6 7 8 9 10 > >>

Below JavaScript function getdatetime() will return the date string in mm/dd/yyyy hhmmss ampm format. You need to pass date as parameter that need to be returned in the format mm/dd/yyyy hhmmss ampm. If you want current date in the format then you can simple call the function as getdatetime(new Date());

Example

JavaScript Function

function getdatetime(dt) {
    var res = "";
    res += formatdigits(dt.getMonth() + 1);
    res += "/";
    res += formatdigits(dt.getDate());
    res += "/";
    res += formatdigits(dt.getFullYear());
    res += " ";
    res += formatdigits(dt.getHours() > 12 ? dt.getHours() - 12 : dt.getHours());
    res += ":";
    res += formatdigits(dt.getMinutes());
    res += ":";
    res += formatdigits(dt.getSeconds());
    res += " " + dt.getHours() > 11 ? " PM" : " AM";
    return res;
}
function formatdigits(val) {
    val = val.toString();
    return val.length == 1 ? "0" + val : val;
}

Calling function:

var dtformated = getdatetime(new Date());
$("#divdatetime").html(dtformated);

Source code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        function getdatetime(dt) {
            var res = "";
            res += formatdigits(dt.getMonth() + 1);
            res += "/";
            res += formatdigits(dt.getDate());
            res += "/";
            res += formatdigits(dt.getFullYear());
            res += " ";
            res += formatdigits(dt.getHours() > 12 ? dt.getHours() - 12 : dt.getHours());
            res += ":";
            res += formatdigits(dt.getMinutes());
            res += ":";
            res += formatdigits(dt.getSeconds());
            res += " " + dt.getHours() > 11 ? " PM" : " AM";
            return res;
        }
        function formatdigits(val) {
            val = val.toString();
            return val.length == 1 ? "0" + val : val;
        }
        $(document).ready(function () {
            var dtformated = getdatetime(new Date());
            $("#divdatetime").html(dtformated);
        });
    </script>
</head>
<body>
    <div id="divdatetime">
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 25 Nov 2016

Below JavaScript function getdate() will return the date string in mm/dd/yyyy format. You need to pass date as parameter that need to be returned in the format mm/dd/yyyy. If you want current date in the format then you can simple call the function as getdate(new Date());

Example:

Javascript Function:

function getdate(dt) {
    var res = "";
    if ((dt.getMonth() + 1).toString().length < 2)
        res += "0" + (dt.getMonth() + 1).toString();
    else
        res += (dt.getMonth() + 1).toString();
    res += "/";
    if (dt.getDate().toString().length < 2)
        res += "0" + dt.getDate().toString();
    else
        res += dt.getDate().toString();
    res += "/" + dt.getFullYear().toString();
    return res;
}

Calling function:

var dtformated = getdate(new Date());
$("#divdatetime").html(dtformated);

Source code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        function getdate(dt) {
            var res = "";
            if ((dt.getMonth() + 1).toString().length < 2)
                res += "0" + (dt.getMonth() + 1).toString();
            else
                res += (dt.getMonth() + 1).toString();
            res += "/";
            if (dt.getDate().toString().length < 2)
                res += "0" + dt.getDate().toString();
            else
                res += dt.getDate().toString();
            res += "/" + dt.getFullYear().toString();
            return res;
        }
        $(document).ready(function () {
            var dtformated = getdate(new Date());
            $("#divdatetime").html(dtformated);
        });
    </script>
</head>
<body>
    <div id="divdatetime">
    </div>
</body>
</html>

Read More...
Submited By : mrkraju On 17 Nov 2016

Using jQuery animate you can animate the controls. In this example we will see how to animate the control width using jQuery animate and toggle.

Example:
Click on menu icon to animate


jQuery code to slide/animate menu bar

    <script language="javascript">
        $(document).ready(function () {
            $(".menuicon").on("click", function () {
                $(".menu").animate({ width: 'toggle' }, "slow");
            });
        });
    </script>

Source code:

<html>
<head>
    <style>
        .menu {
            float: left;
            display: none;
            padding: 0px 5px;
        }

            .menu ul {
                list-style-type: none;
                margin: 0;
                padding: 0;
            }

                .menu ul li {
                    margin: 0;
                    padding: 0;
                }

                    .menu ul li a {
                        text-decoration: none;
                        color: #292323;
                        background: #ccc;
                        display: block;
                        padding: 5px 30px 5px 5px;
                        border-bottom: solid 1px #fff;
                        height: 20px;
                        white-space: nowrap;
                    }

        .menuicon {
            border: solid 1px #000;
            border-radius: 50%;
            padding: 5px 8px;
            float: left;
            cursor: pointer;
        }

        .menuiconbar {
            width: 25px;
            height: 5px;
            background-color: black;
            margin: 3px 0;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script language="javascript">
        $(document).ready(function () {
            $(".menuicon").on("click", function () {
                $(".menu").animate({ width: 'toggle' }, "slow");
            });
        });
    </script>
</head>
<body>
    <div>
        <div class="menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Blogs</a></li>
                <li><a href="#">Tutorials</a></li>
                <li><a href="#">Forums</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>
        <div class="menuicon">
            <div class="menuiconbar"></div>
            <div class="menuiconbar"></div>
            <div class="menuiconbar"></div>
        </div>
    </div>
    <br clear="all" />
</body>
</html>

Read More...
Submited By : mrkraju On 06 Oct 2016

This example shows you how to blink div background using jQuery toggleClass and javascript setInterval.

jQuery toggleClass() example!



Source code:
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style type="text/css">
        .divmsg {
            background: green;
            color: #fff;
            display: inline-block;
            margin: 20px;
            padding: 20px;
            border-radius:5px;
        }

        .divred {
            background: red !important;
        }

        #btntoggle, #btnstoptoggle {
            margin: 10px 20px;
            padding:10px;
        }
    </style>
</head>
<body>
    <div class="divmsg">
        jQuery toggleClass() example!
    </div><br />
    <input type="button" id="btntoggle" value="Blink background using toogle" /><br />
    <input type="button" id="btnstoptoggle" value="Stop blinking" />
    <script src="http://code.jquery.com/jquery-1.7.2.min.js" language="javascript"></script>
    <script language="javascript">
        $(document).ready(function () {
            var blink = null;
            $("#btntoggle").on("click", function () {
                if (blink == null)
                    blink = setInterval(blinkMessage, 500);
            });
            $("#btnstoptoggle").on("click", function () {
                if (blink != null) {
                    clearInterval(blink);
                    blink = null;
                    $(".divmsg").removeClass("divred");
                }
            });
        });

        function blinkMessage() {
            $(".divmsg").toggleClass("divred");
        }
    </script>
</body>
</html>

Read More...
Submited By : mrkraju On 25 Sep 2016

To take backup of dll in GAC follow the below setps.

  • Open Windows Run
  • Type C:\Windows\assembly\GAC_MSIL and click enter
  • All the GAC MSIL dll's will be opened
  • You can take backup of what ever dll you want

Read More...
Submited By : mrkraju On 14 Jul 2016

This example shows you how to bind data to DropDownList in GridView based on the Grid Row Value. In this example we will bind the States to DropDownList based on Country in Row.

In the GridView RowDataBound we will find the DropDownList and Country then fetch the state list for the Country and bind the state list to DropDownList.

RowDataBound event

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int CountryID = int.Parse(((HiddenField)e.Row.FindControl("hidCountryID")).Value);
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlState");
            ddl.DataSource = GetStates(CountryID);
            ddl.DataTextField = "State";
            ddl.DataValueField = "StateID";
            ddl.DataBind();
        }
    }

Example
Dropdown binding in GridView

CS Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;
using System.Collections;

namespace WebApp
{
    public partial class Sample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGrid();
            }
        }

        void FillGrid()
        {
            gv.DataSource = GetCountries();
            gv.DataBind();
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int CountryID = int.Parse(((HiddenField)e.Row.FindControl("hidCountryID")).Value);
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlState");
                ddl.DataSource = GetStates(CountryID);
                ddl.DataTextField = "State";
                ddl.DataValueField = "StateID";
                ddl.DataBind();
            }
        }

        DataTable GetCountries()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CountryID", typeof(int));
            dt.Columns.Add("Country", typeof(string));
            dt.Rows.Add(1, "Australia");
            dt.Rows.Add(2, "New Zealand");
            dt.Rows.Add(3, "India");
            dt.Rows.Add(4, "USA");
            return dt;
        }

        DataTable dtStates = null;

        DataTable GetStates(int CountryID)
        {
            if (dtStates == null)
            {
                dtStates = new DataTable();
                dtStates.Columns.Add("CountryID", typeof(int));
                dtStates.Columns.Add("StateID", typeof(string));
                dtStates.Columns.Add("State", typeof(string));
                dtStates.Rows.Add(1, 1, "Albury");
                dtStates.Rows.Add(1, 2, "Dubbo");
                dtStates.Rows.Add(1, 3, "Sydney");
                dtStates.Rows.Add(2, 1, "Auckland");
                dtStates.Rows.Add(2, 2, "Hamilton");
                dtStates.Rows.Add(2, 3, "Wellington");
                dtStates.Rows.Add(3, 1, "Delhi");
                dtStates.Rows.Add(3, 2, "Hyderabad");
                dtStates.Rows.Add(3, 3, "Mumbai");
                dtStates.Rows.Add(4, 1, "Chicago");
                dtStates.Rows.Add(4, 2, "New York");
                dtStates.Rows.Add(4, 3, "Washington DC");
            }
            dtStates.DefaultView.RowFilter ="CountryID=" + CountryID;
            return dtStates.DefaultView.ToTable();
        }


    }

}

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs" Inherits="WebApp.Sample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
            CellPadding="5" CellSpacing="5" ForeColor="#333333" GridLines="Both" 
            onrowdatabound="gv_RowDataBound"  >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:HiddenField ID="hidCountryID" runat="server" Value='<%#Eval("CountryID") %>' />
                        <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlState" runat="server" style="width:100%">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Read More...
Submited By : mrkraju On 20 Jun 2016

This example shows you how to write C# extension method and how to convert DataSet to JSON using JavaScriptSerializer.

By default DataSet doesnot have GetJSON method to get JOSN string from DataSet. So in this example I have written an extension method to add GetJSON method to DataSet and it will return JSON String as output.

    public static class DataSetExt
    {
        public static string GetJSON(this DataSet ds)
        {

            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            ArrayList root = new ArrayList();
            List<Dictionary<string, object>> table;
            Dictionary<string, object> data;

            foreach (DataTable dt in ds.Tables)
            {
                table = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dt.Rows)
                {
                    data = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        data.Add(col.ColumnName, dr[col]);
                    }
                    table.Add(data);
                }
                root.Add(table);
            }
            
            return serializer.Serialize(root);
        }
    }

Steps to write GetJSON extension method.

  • Class should be declared as static
  • Method name should be declared as static
  • Method parameter should be starts with "this" keyword and the Object(DataSet) and variable name(ds).
  • Using System.Web.Script.Serialization.JavaScriptSerializer we converted the DataSet to JSON using Dictionary and ArrayList.

Example
C# extension method

CS Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Text;
using System.Collections;

namespace WebApp
{
    public partial class Sample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnGetJSON_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("Employee Name", typeof(string));
            dt.Columns.Add("Salary", typeof(decimal));
            dt.Columns.Add("Department", typeof(string));
            dt.Rows.Add("John", 5000.00, "Marketing");
            dt.Rows.Add("Smith", 4000.00, "IT");
            dt.Rows.Add("Williams", 6000.00, "Sales");
            dt.Rows.Add("Vijay", 6500.00, "IT");
            ds.Tables.Add(dt);
            dt = new DataTable();
            dt.Columns.Add("StudentID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Marks", typeof(int));
            dt.Columns.Add("Grade", typeof(string));
            dt.Rows.Add(1, "Suresh", 950, "A+");
            dt.Rows.Add(1, "Rama", 900, "A+");
            dt.Rows.Add(1, "Kishore", 750, "B");
            ds.Tables.Add(dt);
            divOutput.InnerHtml = ds.GetJSON();


        }

    }


    public static class DataSetExt
    {
        public static string GetJSON(this DataSet ds)
        {

            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            ArrayList root = new ArrayList();
            List<Dictionary<string, object>> table;
            Dictionary<string, object> data;

            foreach (DataTable dt in ds.Tables)
            {
                table = new List<Dictionary<string, object>>();
                foreach (DataRow dr in dt.Rows)
                {
                    data = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        data.Add(col.ColumnName, dr[col]);
                    }
                    table.Add(data);
                }
                root.Add(table);
            }
            
            return serializer.Serialize(root);
        }
    }
}

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Sample.aspx.cs" Inherits="WebApp.Sample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnGetJSON" runat="server" Text="Get JSON" OnClick="btnGetJSON_Click" />
    <br /><br />
    <div id="divOutput" runat="server">
        
    </div>
    </form>
</body>
</html>

Read More...
Submited By : mrkraju On 17 Jun 2016

When you truncate the table data, table Identity will be reset automatically. But when you delete the records in the table Identity will not be reset.

Below query will reset the table Identity in SQL server.

DBCC CHECKIDENT ('[TableName]', RESEED, 0);
GO

Read More...
Submited By : mrkraju On 15 Jun 2016

Below are the differences between Truncate and Delete in MS-SQL.

Delete Truncate
Can apply WHERE condition to filter records that need to be deleted. Cannot apply WHERE condition.
Delete is DML command. Truncate is DDL command.
Works against table row (locks the table row). Works against table (locks the table).
By using transactions you can rollback the deleted records. You cannot roll back the deleted records.
Trigger will fire when Delete the rows. Truncate cannot activate Trigger (Trigger will no fire)
No impact on identity. Resets the Column identity.

Read More...
Submited By : mrkraju On 15 Jun 2016

This example shows you how to restore SQL database from a backup file using SQL Query.

Example
USE MASTER
GO


ALTER DATABASE SampleDB
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Go


RESTORE DATABASE SampleDB
FROM DISK = N'E:\DB\Bakup\SampleDBBackupfile.bak'
Go


ALTER DATABASE SampleDB
SET MULTI_USER with ROLLBACK IMMEDIATE
Go


In the above example
SampleDB is the database name.
N'E:\DB\Bakup\SampleDBBackupfile.bak' physical path of backup file.

Read More...
Submited By : mrkraju On 28 Apr 2016
<< < 1 2 3 4 5 6 7 8 9 10 > >>




Search dotnetlearners.com