Saturday, December 22, 2012

How to Close Window Using Javascript


Window.Close() method is used for close window using JavaScript in HTML

Example :

<input type="button" value="Close" class="button" onclick="window.opener=null; window.close(); return false;">

HTML Example For Open Window and Close Window on Button Click Event:
Example :

<html>
<head>
    <script>

        function openWin() {
            myWindow = window.open("", "", "width=200,height=100");
            myWindow.document.write("<p>This is 'myWindow'</p>");
        }

        function closeWin() {
            myWindow.close();
        }
    </script>
</head>
<body>
    <input type="button" value="Open 'myWindow'" onclick="openWin()">
    <input type="button" value="Close 'myWindow'" onclick="closeWin()">
</body>
</html>


Window.Close() method is used for close window using JavaScript in ASP.NET
 
Example :
<asp:Button ID="Button2" runat="server" Text="Close" OnClientClick="javascript: window.close();"/>

  

#ASP.NET, #JAVASCRIPT,#CLOSE WINDOW
 

Saturday, April 21, 2012

SQL Custom Paging


Using SQL Server Stored Procedure for implementing Custom Paging:

All of us would have implemented Paging in our applications.


Paging is particularly useful if you have lots of records to be displayed on a page and you can't get them displayed in one stretch. Say we have 1000 records to be displayed in a page. In this scenario, we cannot show up all the records in a single stretch in the page. Hence we need to implement Paging functionality whereby users can see a set of records and then click on a Button/Link to view the next set of records.

Here is the sample store procedure that is used for the SQL Server store procedure for paging.
Example:
CREATE PROCEDURE USP_PRODUCT_SELECT
(
@CurrentIndex int,
@TotalPageSize int
)
 AS

SELECT * FROM
(

SELECT ROW_NUMBER() OVER(ORDER BY ProductID) AS RowId, * FROM Products

) AS  P
WHERE  P.RowId BETWEEN (@CurrentIndex -1) * @TotalPageSize+ 1 AND (@CurrentIndex  * @TotalPageSize )
GO

#SQL Server, #Custom Paging

Recursive Query In Sql Server

A common table expression (CTE) provides the significant advantage of being able to reference itself, thereby creating a recursive CTE. A recursive CTE is one in which an initial CTE is repeatedly executed to return subsets of data until the complete result set is obtained.

A query is referred to as a recursive query when it references a recursive CTE. Returning hierarchical data is a common use of recursive queries, for example: Displaying employees in an organizational chart, or data in a bill of materials scenario in which a parent product has one or more components and those components may, in turn, have subcomponents or may be components of other parents.
A recursive CTE can greatly simplify the code required to run a recursive query within a SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. In earlier versions of SQL Server, a recursive query usually requires using temporary tables, cursors, and logic to control the flow of the recursive steps.

The structure of the recursive Query


The structure of the recursive CTE in Transact-SQL is similar to recursive routines in other programming languages. Although a recursive routine in other languages returns a scalar value, a recursive CTE can return multiple rows.
A recursive CTE consists of three elements:
  1. Invocation of the routine.
    The first invocation of the recursive CTE consists of one or more CTE_query_definitions joined by UNION ALL, UNION, EXCEPT, or INTERSECT operators. Because these query definitions form the base result set of the CTE structure, they are referred to as anchor members.
    CTE_query_definitions are considered anchor members unless they reference the CTE itself. All anchor-member query definitions must be positioned before the first recursive member definition, and a UNION ALL operator must be used to join the last anchor member with the first recursive member.
  2. Recursive invocation of the routine.
    The recursive invocation includes one or more CTE_query_definitions joined by UNION ALL operators that reference the CTE itself. These query definitions are referred to as recursive members.
  3. Termination check.
    The termination check is implicit; recursion stops when no rows are returned from the previous invocation.
Recursive query in sql server Example:

WITH  CTE(categoryId,CategoryName,ParentCategoryID,TopParentCategoryID) AS
(
SELECT    
a.CategoryId,
a.CategoryName,
a.ParentCategoryID, 
a.CategoryId AS TopParentCategoryID
FROM Categories  a WHERE  ParentCategoryID is null
UNION ALL
SELECT c.CategoryId, c.CategoryName, c.ParentCategoryID, cte.TopParentCategoryID AS TopParentCategoryID FROM Categories c,cte
WHERE cte.CategoryId = c.ParentCategoryID
)
SELECT CTE.categoryId,cte.CategoryName,cte.ParentCategoryID,cte.TopParentCategoryID FROM CTE


Reverse Word In String in C#


Reverse Word In String in C#

Words in a string can be reversed. We have several methods or ways to reverse word in a string.
Using C# you can reverse string:


 Example:
           char [] charAray = "This is Test String".ToCharArray();
            
            StringBuilder sb = new StringBuilder();
            System.Console.WriteLine((new string(charAray)));
            System.Console.WriteLine("Reverse");
            for(int i=charAray.Length-1;i>=0 ;i--)
            {
                if (charAray[i] == ' ')
                {
                   
                    char[] charAray1 = sb.ToString().ToCharArray();
                    Array.Reverse(charAray1);
                    System.Console.Write(new string(charAray1) + " ");
                    sb.Clear();
                }
                else
                {
                    sb.Append(charAray[i]);
                }
            }
            char[] temp = sb.ToString().ToCharArray();
            Array.Reverse(temp);
            System.Console.Write(new string(temp));

Friday, March 30, 2012

Using jQuery to Consume ASP.NET JSON Web Services

Using jQuery to Consume ASP.NET JSON Web Service:
 
Following are the steps for consume JSON webservice using JQuery.
  1. Add New Web Page JSONSaveDataWebservice.aspx
  2.  Add Web Service : wsEmployeeInsert.asmx
  3. Add  Java Script in aspx page.

JSONSaveDataWebservice.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JSONSaveDataWebservice.aspx.cs"
    Inherits="JSONSaveDataWebservice" %>

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>JSON SaveData Get Data Using Webservice Jquery title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">script>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript">script>
    <script type="text/javascript" language="javascript">

        function CallService() {
            var name = $("#txtName").val();
            var age = $("#txtAge").val();
            $.ajax({
                type: "POST",
                url: "JSONSaveDataWebserviceaspx.aspx/InsertEmployee",
                data: "{'name':'" + name + "','age':'" + age + "'}",
                contentType: "application/json",
                async: false,
                success: function (data) {
                    alert("Sucess" + data.d);
                },
                error: function (data) { alert("Error" + data.d); }
            });
        }

        $(document).ready(function () {

            // Load Employees
            GetAllEmployees();

            var DropDownList1 = $("#DropDownList1");

            DropDownList1.change(function (e) {

                var employeeId = DropDownList1.val();
                if (employeeId != -1) {

                    // Get Employee Details
                    GetEmployeeDetails(employeeId);

                } else {

                    $("#outputTable").hide();
                }

            });
        });

        function GetAllEmployees() {
            var DropDownList1 = $("#DropDownList1");
            $.ajax({
                type: "POST",
                url: "wsEmployeeInsert.asmx/GetAllEmployees",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {

                    var Employees = response.d;
                    $.each(Employees, function (index, employee) {
                        DropDownList1.append(' + employee.Name + '');
                    });
                },
                failure: function (msg) {
                    alert(msg);
                }
            });
        }
        function GetEmployeeDetails(employeeId) {
            $.ajax({
                type: "POST",
                url: "wsEmployeeInsert.asmx/GetEmployeeDetails",
                data: "{'employeeId':'" + employeeId + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var Employee = response.d;
                    $("#spnEmployeeId").html(Employee.Id);
                    $("#spnEmployeeName").html(Employee.Name);
                    $("#spnAddress").html(Employee.Address);
                    $("#spnSalary").html(Employee.Salary);
                    $("#outputTable").show();
                },
                failure: function (msg) {
                    alert(msg);
                }
            });
        }
    script>
head>
<body>
    <form id="form1" runat="server">
    <div>
        Name:
        <asp:TextBox ID="txtName" runat="server">asp:TextBox><br />
        Age:
        <asp:TextBox ID="txtAge" runat="server">asp:TextBox><br />
        <input id="_btnSubmit" type="button" value="Submit" onclick="CallService();" />
        <span id="message">span>
    div>
    <div>
        Select Employee:
        <asp:DropDownList ID="DropDownList1" runat="server" Width="150">
            <asp:ListItem Text="Select" Value="-1" />
        asp:DropDownList>
        <br />
        <br />
        <table style="border: solid 1px black; width: 300px; display: none; background-color: #f3f3f3"
            cellpadding="4" cellspacing="0" id="outputTable">
            <tr>
                <td>
                    Employee ID:
                td>
                <td>
                    <span id="spnEmployeeId" />
                td>
            tr>
            <tr>
                <td>
                    Employee Name:
                td>
                <td>
                    <span id="spnEmployeeName" />
                td>
            tr>
            <tr>
                <td>
                    Address:
                td>
                <td>
                    <span id="spnAddress" />
                td>
            tr>
            <tr>
                <td>
                    Salary:
                td>
                <td>
                    <span id="spnSalary" />
                td>
            tr>
        table>
    div>
    form>
body>
html>


JSONSaveDataWebservice.aspx.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services; 
public partial class JSONSaveDataWebservice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string InsertEmployee(string name, int age)
    {
        string s_name = name;
        int s_age = age;
        return "Save Sucessfully";
    }
}

wsEmployeeInsert.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

///


/// Summary description for wsEmployeeInsert
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class wsEmployeeInsert : System.Web.Services.WebService,IDisposable {
    List<Employee> list = new List<Employee>();
    public wsEmployeeInsert () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
        list.Add(new Employee { Id = 1, Name = "Rajesh Tendulkar",Address="India", Salary = 10000 });
        list.Add(new Employee { Id = 2, Name = "Rahul Ganguly",Address="Asia", Salary = 20000 });
        list.Add(new Employee { Id = 3, Name = "Saurav Dravid",Address="China", Salary = 30000 });
        list.Add(new Employee { Id = 4, Name = "Suresh Sharma",Address="US", Salary = 40000 });
        list.Add(new Employee { Id = 5, Name = "Sachin Tiwari",Address="UK", Salary = 50000 });
    }

    [WebMethod]
    public List<Employee> GetAllEmployees()
    {
        return list;
    }

    [WebMethod]
    public Employee GetEmployeeDetails(string employeeId)
    {
        int empId = Int32.Parse(employeeId);
        Employee emp = list.Single(e => e.Id == empId);
        return emp;
    }   
}
partial  class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Addresses { get; set; }
    public decimal Salary { get; set; }
}


Notes:
  • Jquery
  • ASP.net
  • JSON
  • Web service

ShareThis

Welcome

Welcome to Rajesh Prajapati, asp.net blog.
Here you can find some useful code and information about asp.net., c#, VB.net, SQL Server, Web Service, Web Designing etc