17 November 2013

Confirmation Before Deleting Record Using Grid View

<Column>
<asp:TemplateField> 
<ItemTemplate> 
<asp:Button ID="btnDel" runat="server" Text="Delete CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete?');" /> </ItemTemplate>
</asp:TemplateField>
</Column>

Show Line number in Visual Studio Editor 2005/2008/2010

              You can display line numbers in your visual studio editor. Just follow the following steps to display line numbers. by default line number are hidden.
Step 1: Go to Tools menu at the top of VS.
Step 2: Go to Options
Step 3: Go to Text Editor and then select C# or your language.
Step 4: Select Line number under display options.
Click on OK and it will display he line number in editor.



Displaying line numbers in editor:


How to store Datatable in session, How to retrieve data from session to Datatable & how to search data in Datatable

H


      private void add_rooms1(int p,string room_name,int room_type_id,DateTime dateTime,                    DateTime dateTime_2,  string p_2, string p_3)
      {
            try
            {

                DataTable dt = new DataTable();


                if (Session["room_booking"] == null)
                {

                    dt.Columns.Add("Room_id", typeof(int));
                    dt.Columns.Add("Room_name", typeof(string));
                     dt.Columns.Add("room_type_id", typeof(int));
                    dt.Columns.Add("CheckInDate", typeof(DateTime));
                    dt.Columns.Add("CheckOutDate", typeof(DateTime));
                    dt.Columns.Add("Extra_bed", typeof(int));
                    dt.Columns.Add("Extra_charge", typeof(decimal));

                    dt.Rows.Add(p, room_name, room_type_id,dateTime, dateTime_2,                              nvert.ToInt32(p_2), Convert.ToDecimal(p_3));

                    Session["room_booking"] = dt;
                    grand_total = 0;
                    bind_booking_cart();
                }
                else
                    if (Session["room_booking"] != null)
                    {
                        DataTable dt1 = new DataTable();
                        dt1 = Session["room_booking"] as DataTable;
                      
                          bool AlreadyExists=check_cart(p);
                          if (AlreadyExists == true)
                                {
                                    lblerror.Visible = true;
                                    lblerror.ForeColor = System.Drawing.Color.Red;
                                    lblerror.Text = "Room alredy added";
                                    grand_total = 0;
                                    bind_booking_cart();
                                }
                                else
                                {
                                    dt1.Rows.Add(p,room_name,room_type_id, dateTime, dateTime_2,           Convert.ToInt32(p_2), Convert.ToDecimal(p_3));
                                    Session["room_booking"] = dt1;
                                    grand_total = 0;
                                    bind_booking_cart();
                                }
                      
                    }

            }
            catch (Exception ex)
            {
                lblerror.Visible = true;
                lblerror.ForeColor = System.Drawing.Color.Red;
                lblerror.Text = ex.Message;
            }
        }

protected bool check_cart(int id)
        {
            //try
            //{
                bool AlreadyExists = false;
                DataTable dt1 = new DataTable();
                dt1 = Session["room_booking"] as DataTable;
                foreach (DataRow row1 in dt1.Rows)
                {
                    Int32 array1 = Convert.ToInt32(row1["Room_id"]);
                    if (array1 == id)
                    {
                        AlreadyExists = true;
                        break;
                    }
                    else
                    {
                        AlreadyExists = false;
                       
                    }

                }
                return AlreadyExists;
            //}
            //catch (Exception ex)
            //{
            //    lblerror.Visible = true;
            //    lblerror.ForeColor = System.Drawing.Color.Red;
            //    lblerror.Text = ex.Message;
            //}

        }



How to find days between two dates in c#.net

How to find days between two dates in c#.net
DateTime dt1 = new DateTime(2011, 01, 01);
DateTime dt2 = new DateTime(2011, 01, 20);
int ireturn = (int)dt2.Subtract(dt1).TotalDays;

(or)
var elapsedDays = (endDate - initialDate).TotalDays;

(or)
DateTime start = DateTime.Now;
DateTime end = DateTime.Now.AddDays(5);
TimeSpan span = end.Subtract(start);
(or)

DateTime dt1 = Convert.ToDateTime(Textbox1.text);
DateTime dt2 = Convert.ToDateTime(Textbox1.text);
TimeSpan ts = dt2.subtract(dt1);

(or)

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Label ID="Label9" runat="server">
    </asp:Label>
    <asp:Label ID="Label10" runat="server">
    </asp:Label><br />
    <br />
    <asp:Label ID="Label1" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label2" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label3" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label4" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label5" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label6" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label7" runat="server">
    </asp:Label><br />
    <asp:Label ID="Label8" runat="server">
    </asp:Label><br />
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime startTime = Convert.ToDateTime("12 Aug 2012 12:30 PM");
        DateTime endTime = Convert.ToDateTime("11 Sep 2013 15:30 PM");
        Label9.Text = "StartTime :" + startTime;
        Label10.Text = "EndTime :" + endTime;
        //We subtract the time.
        TimeSpan span = endTime.Subtract(startTime);

        //This will just return the difference selected option
        Label1.Text = "Time Difference (seconds): " + span.Seconds;
         Label2.Text = "Time Difference (minutes): " + span.Minutes;
         Label3.Text = "Time Difference (hours): " + span.Hours ;
         Label4.Text = "Time Difference (days): " + span.Days ;

        //This will return the total difference based on selected options
         Label5.Text = "Total milliseconds: " + Math.Round(span.TotalSeconds, 2).ToString() ;
         Label6.Text = "Total Minutes: " + Math.Round(span.TotalMinutes, 2).ToString() ;
         Label7.Text = "Total hours: " + Math.Round(span.TotalHours, 2).ToString();
         Label8.Text = "Total days: " + Math.Round(span.TotalDays, 2).ToString();
    }
}






Aliases in sql server

SQL server aliases are used to temporarily rename a table or a column heading.

SQL Aliases

SQL aliases are used to give a database table, or a column in a table, a temporary name.
Basically aliases are created to make column names more readable.

Alias Syntax for Columns

SELECT column_name AS alias_name
FROM table_name;

Alias Syntax for Tables

SELECT column_name(s)
FROM table_name AS alias_name;

DISTINCT in sql server

In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.

The DISTINCT keyword can be used to return only distinct (different) values.

DISTINCT Syntax

SELECT DISTINCT column_name,column_name
FROM table_name;

DISTINCT Example

SELECT DISTINCT namr FROM tbl_student;

SQL datetime formats with century

SQL datetime formats with century (YYYY or CCYY format)- sql time format
SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)
-- Oct 2 2010 11:01AM
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2010
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd - 2010.10.02
SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) -- dd mon yyyy
SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy
SELECT convert(varchar, getdate(), 108) -- hh:mm:ss
SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
-- Oct 2 2010 11:02:44:013AM
SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd
-- yyyymmdd - ISO date format - international standard - works with any language setting
SELECT convert(varchar, getdate(), 112) -- yyyymmdd
SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm
-- 02 Oct 2010 11:02:07:577
SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm
-- 2010-10-02T10:52:47.513
-- Without century (YY) date / datetime conversion - there are exceptions!
SELECT convert(varchar, getdate(), 0) -- mon dd yyyy hh:mmAM (or PM)
SELECT convert(varchar, getdate(), 1) -- mm/dd/yy
SELECT convert(varchar, getdate(), 2) -- yy.mm.dd
SELECT convert(varchar, getdate(), 3) -- dd/mm/yy
SELECT convert(varchar, getdate(), 4) -- dd.mm.yy
SELECT convert(varchar, getdate(), 5) -- dd-mm-yy
SELECT convert(varchar, getdate(), 6) -- dd mon yy
SELECT convert(varchar, getdate(), 7) -- mon dd, yy
SELECT convert(varchar, getdate(), 8) -- hh:mm:ss
SELECT convert(varchar, getdate(), 9) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
SELECT convert(varchar, getdate(), 10) -- mm-dd-yy
SELECT convert(varchar, getdate(), 11) -- yy/mm/dd
SELECT convert(varchar, getdate(), 12) -- yymmdd
SELECT convert(varchar, getdate(), 13) -- dd mon yyyy hh:mm:ss:mmm
SELECT convert(varchar, getdate(), 14) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 20) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 21) -- yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 22) -- mm/dd/yy hh:mm:ss AM (or PM)
SELECT convert(varchar, getdate(), 23) -- yyyy-mm-dd
SELECT convert(varchar, getdate(), 24) -- hh:mm:ss
SELECT convert(varchar, getdate(), 25) -- yyyy-mm-dd hh:mm:ss.mmm
-- SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), '/', ' ') -- yyyy mm dd
SELECT convert(varchar(7), getdate(), 126) -- yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8) -- mon yyyy
SELECT substring(convert(varchar, getdate(), 120),6, 11) -- mm-dd hh:mm

what is trim() in c#

Trim eliminates leading and trailing whitespace. We need to remove whitespace from the beginning or ending of a string. We use the .NET Framework's Trim method to do this efficiently. This method removes any characters specified.

 

Trim Example input & output


1)      String input:       "   This is an example string. "
      Trim method Output: "This is an example string."
 
2)       String input:       "This is an example string.\r\n\r\n"
      Trim method Output: "This is an example string."
 
3)       String input:       "\t, again and again."
      Trim method Output:   "again and again."
Note:  You can remove commas, spaces, and tabs.

Example

This first example removes space characters from the beginning and end of a C# string. Note that when you call Trim on a string, it copies the string and returns a modified version of that copy. The original is not changed.
Example program for trim in c#
 
using System;
class Program
{
    static void Main()
    {
             // Input string
             string st = "  This is an example string. ";
 
             // Call Trim instance method.
             // This returns a new string copy.
             st = st.Trim();
 
             Console.WriteLine(st);
    }
}
 
Output:-(Spaces were removed.)
 
This is an example string.

How to find media screen resolution in javascript

<script type="text/javascript">
    function viewOrder() 
    {
      var width = (window.innerWidth > 0) ? window.innerWidth : screen.width;
    }
  </script>

How to showing a video/movie on a web page

<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>

How to get current location(latitude and longitude) in javascript

<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude; 
  }
</script>