Tuesday, 26 November 2013

How to Write a stored procedure for Optional Search

How to write a Stored Procedure for Optional Search?

--Create Table

create table stu_details(id int primary key identity,name nvarchar(30),class int)

 --**************************************************************

--insert record

insert into stu_details(name,class) values
         ('rahul',14),
         ('Ramu',20),
         ('Rahul',13),
         ('rahul',14),
         ('rani' ,15),
         ('RANI' ,15),
         ('RANI' ,15),
         ('Raju' ,12),
         ('Raju' ,12),
         ('rashmi',18),
         ('Roza' ,17),
         ('rani' ,15),
         ('kush',23)

--***************************************************************

--Show Record
select * from stu_details

--***************************************************************
--Creating stored Procedure for Optional Search

Create proc usp_search
@id int=0,
@name nvarchar(30)=null,
@class int=0
as
BEGIN
select * from stu_details where
(id=@id or COalesce(@id,'')=0)
and (name=@name or COALESCE(@name,'')='')
and (class=@class or COALESCE(@class,'')=0)

END
--***************************************************************

How to Check a given number is prime or not?

How to check a given number is prime or not?









ASPX page Code:-
------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="primeNumber.aspx.cs" Inherits="primeNumber" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
    <script type="text/javascript" language="javascript">

        function valid() {
            var txtval = document.getElementById("txtnum").value;
            if (txtval == "" || txtval == null) {
                alert('Please Enter Value');
                document.getElementById("txtnum").focus();
                return false;
              

               
            }
        }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
     <br />
      
        <br /> <br />
      
        <br /> <br />
      
        <br /> <br />
      
        <br /> <br />
      
        <br />
    <div style="border:3px solid blue; width:30%;margin-left:auto;margin-right:auto;">
        <br />
      
        <br />
        <table class="style1">
            <tr>
                <td colspan="2" style="text-align: center">
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="right">
                    Enter&nbsp; a Number</td>
                <td align="left">
        <asp:TextBox ID="txtnum" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="right">
                    &nbsp;</td>
                <td align="left">
        <asp:Button ID="Button1" runat="server" Text="Check Prime or not?" onclick="Button1_Click" OnClientClick="return valid()"/>
                </td>
            </tr>
            <tr>
                <td align="right">
                    &nbsp;</td>
                <td align="left">
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

ASPX.CS page Code:-
----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class primeNumber : System.Web.UI.Page
{
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (isPrime(Convert.ToInt32(txtnum.Text)) == true)
        {
            Label1.Text = "Number is prime" + txtnum.Text;
        }
        else
        {
            Label1.Text = "Number is not prime" + txtnum.Text;
        }
    }
    public static bool isPrime(int num)
    {
        bool primnum = true;
        int factor = num -1;


        for(int i=2;i<=factor; i++)
        {
            if (num % i == 0)
            {
                primnum = false ;
            }
        }
        return primnum;
    }
}

Saturday, 23 November 2013

What is the page life cycle ?

What is the page life cycle ?
        1.   PreInit
        2.   Init
        3.   InitComplete
        4.   PreLoad
        5.   Load
        6.   ControlEvents
        7.   LoadComplete
        8.   PreRender
        9.   PreRenderComplete
      10.   SaveStateComplete
      11.   RenderComplete
 12.   Unload

What is the Maximum Time Duration for session

What is the Maximum Time Duration for session?


365 X 24 X 60=525600 minute.

Thursday, 21 November 2013

SQL Query for interview purpose

ROW_NUMBER()

--Use of row_number() function
select row_number() over (order by id) as row,id from alltable

Pivot

-- Use of Pivot key word
select id,stu_name,address,qualification from t1 pivot(min(value) for attribute in (stu_name,address,qualification)) t

To show Duplicate Records

--Create table

create table dup(id int,name nvarchar(50),age int)

--Insert Records

insert into dup(id,name,age) values(1,'ramu',23),(1,'ramu',23),(2,'Shachi',30),(2,'Shachi',30),(3,'rahul gupta',20),(3,'rahul gupta',20)

--Sql Query for find duplicate data in Sql Server

select id,name,age, count(*) from dup group by id,name,age having count(*) > 1
--Sql Query for Delete Duplicate Record

delete from dup where dup.%%physloc%% NOT IN (select MIN(b.%%physloc%%) from dup b group by b.id,b.name,b.age )


BACKUP DATABASE Ramu_db TO DISK = 'D:\Ramu_db.bak' WITH INIT
where Ramu_db is data base name and Ramu_db.bak' is database backup file name with drive location.






Tuesday, 19 November 2013

Difference between Function and Stored Procedure

Difference between Function and Stored Procedure
Function
ü  It’s return only value which is mandatory.
ü  Can have only input parameters.
ü  Try-catch block cannot be used in a function.
ü  We can’t use transaction management in function.
ü  Function can be called in stored procedure

Stored Procedure
ü  It’s return zero or n values.
ü  Can have input/output parameters.
ü  Procedure cannot  be called from Function
ü  Exception can be handle by try-catch block.

ü  Transaction management can be used in Stored Procedure.