Sunday, 26 May 2013

Exception Handling in SQL Server Stored Procedure

BEGIN TRY
---Write Your Code
END TRY
BEGIN CATCH
---Write Code to handle errors
END CATCH
In TRY block we will write our queries and in CATCH block we will write code to handle exceptions. In our SQL statements if any error occurs automatically it will move to CATCH block in that we can handle error messages. To handle error messages we have defined Error Functions in CATCH block those are

ERROR_LINE() - This function will return error line number of SQL query which cause to raise error.
ERROR_NUMBER() - This function will return error number which is unique and assigned to it.
ERROR_SEVERITY() - This function will return severity of error which indicates how serious the error is. The values are between 1 and 25.
ERROR_STATE() - This function will return state number of error message which cause to raise error.
ERROR_PROCEDURE() - This function will return name of the procedure where an error occurred.
ERROR_MESSAGE() - This function will return the complete text of the error message which cause to raise error.
Check below sample query to handle errors in stored procedure
BEGIN TRY
SELECT 300/0
END TRY
BEGIN CATCH
SELECT ErrorNumber = ERROR_NUMBER(), ErrorSeverity = ERROR_SEVERITY(), ErrorState = ERROR_STATE(),
ErrorProcedure = ERROR_PROCEDURE(), ErrorLine = ERROR_LINE(), ErrorMessage = ERROR_MESSAGE()
END CATCH
If we run above query we will get output like as shown below

Output


Friday, 24 May 2013

how to copy data from a datatable to another datatable in C#.net

 DataTable dt = new DataTable();
                dt.Columns.Add("EID", typeof(string));
                dt.Columns.Add("NAME", typeof(string));
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "1";
                dr[1] = "siv";
                dt.Rows.Add(dr);
                DataRow dr1;
                dr1 = dt.NewRow();
                dr1[0] = "2";
                dr1[1] = "sankar";
                dt.Rows.Add(dr1);
                DataTable dt1 = dt.Clone();
                IEnumerable<DataRow> idr = (from x in dt.AsEnumerable()  select x).ToArray();
                idr.CopyToDataTable(dt1, LoadOption.PreserveChanges);
                dataGridView1.DataSource = dt1;
Other process
 DataTable dt = new DataTable();
                dt.Columns.Add("EID", typeof(string));
                dt.Columns.Add("NAME", typeof(string));
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "1";
                dr[1] = "siv";
                dt.Rows.Add(dr);
                DataRow dr1;
                dr1 = dt.NewRow();
                dr1[0] = "2";
                dr1[1] = "sankar";
                dt.Rows.Add(dr1);
                DataTable dt1 = dt.Clone();
                List<DataRow> lst = dt.AsEnumerable().ToList();
                lst.CopyToDataTable(dt1, LoadOption.PreserveChanges);
                dataGridView1.DataSource = dt1;
Other Process
DataTable dt = new DataTable();
                dt.Columns.Add("EID", typeof(string));
                dt.Columns.Add("NAME", typeof(string));
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "1";
                dr[1] = "siv";
                dt.Rows.Add(dr);
                DataRow dr1;
                dr1 = dt.NewRow();
                dr1[0] = "2";
                dr1[1] = "sankar";
                dt.Rows.Add(dr1);
                DataTable dt1 = dt.Clone();
                IEnumerable<DataRow> lst = dt.AsEnumerable();
                lst.CopyToDataTable(dt1, LoadOption.PreserveChanges);
                dataGridView1.DataSource = dt1;

how to copy from a list to another list

 private void UserControl1_Load(object sender, EventArgs e)
        {
            try
            {
                 List<emp> s = o.EMP1s.Select(x => new emp { EID = x.EID, NAME = x.NAME }).ToList();
                 dataGridView1.DataSource = s;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        public class emp
        {
            public int EID
            {
                get;
                set;
            }
            public string NAME
            {
                get;
                set;
            }
        }

Monday, 20 May 2013

modal popup

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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>
        .grid
        {
            width: 99%;
            height:120px;
            border: solid 2px #c8c8c8;
            float: left;
          
        }
        .grid_top
        {
            border-top-left-radius: 10px;
            border-top-right-radius: 10px;
            width: 99%;
            height: auto;
            border: solid 2px #c8c8c8;
            float: left;
            background-color: #c8c8c8;
            -moz-border-radius-topleft: 10px;
            -webkit-border-top-left-radius: 10px;
            -khtml-border-radius-topleft: 10px;
            -moz-border-radius-topright: 10px;
            -webkit-border-top-right-radius: 10px;
            -khtml-border-radius-topright: 10px;
        }
        .PoPUpCloseBtnCRM
        {
            border-radius: 10px;
            background-color: #D3D3D3;
            border: 0;
            border-style: groove;
            font-weight: bold;
            margin-top: 5px;
            margin-right: 5px;
        }
        .PoPUpCloseBtnCRM:hover
        {
            background-color: #FF5050;
        }

       .ModalBackgroundCSS
        {
            width: 100%;
            background-color:Black;
            moz-opacity: 0.5;
            khtml-opacity: .5;
            opacity: .5;
            filter: alpha(opacity=50);
            z-index: 120;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
        }
        .ModalPopUpPanelCommonViewCRM
        {
            z-index: 200;
            background-color: White;
            position: absolute;
            top: 0pt;
            left: 0pt;
            text-align: center;
            border-top-left-radius: 10px;
            border-top-right-radius: 10px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
     <div>
     <center>
    <asp:HyperLink ID="hp" runat="server" NavigateUrl="#">Add New</asp:HyperLink><br />

 <asp:GridView ID="gv" runat="server"  DataKeyNames="EID"
             onrowcancelingedit="gv_RowCancelingEdit" onrowdeleting="gv_RowDeleting"
             onrowediting="gv_RowEditing" onrowupdating="gv_RowUpdating">
     <Columns>
         <asp:CommandField HeaderText="UPDATE" ShowEditButton="True" ShowHeader="True" />
         <asp:CommandField HeaderText="DELETE" ShowDeleteButton="True"  ShowHeader="True" />
     </Columns>
         </asp:GridView>
 </center>
    <asp:Panel ID="pnlModal" runat="server" Width="400px" CssClass="ModalPopUpPanelCommonViewCRM" style="display:none" >
            <div align="right" style="background-color: #7DA5DB; height: 30px" class="grid_top">
                <asp:Button ID="btnClose" runat="server" Text="X" CssClass="PoPUpCloseBtnCRM" CausesValidation="False"
                    ToolTip="Close" />
            </div>
            <div class="grid">
             <ul style="width:350px; list-style:none">
             <li style="width:120px; list-style:none; float:left; text-align:right; padding-top:3px">EID</li>
              <li style="width:10px; list-style:none; float:left; text-align:center; padding-top:3px">:</li>
              <li style="width:220px; list-style:none; float:left; text-align:left; padding-top:3px"><asp:TextBox ID="tb" runat="server"></asp:TextBox></li>
             </ul>
             <ul style="width:350px; list-style:none">
             <li style="width:120px; list-style:none; float:left; text-align:right; padding-top:3px">NAME</li>
              <li style="width:10px; list-style:none; float:left; text-align:center; padding-top:3px">:</li>
              <li style="width:220px; list-style:none; float:left; text-align:left; padding-top:3px"><asp:TextBox ID="tb1" runat="server"></asp:TextBox>&nbsp;<asp:HyperLink ID="hlp" runat="server" Text="New" NavigateUrl="#"></asp:HyperLink>
              </li>
             </ul>
              <ul style="width:350px; list-style:none">
             <li style="width:120px; list-style:none; float:left; text-align:right; padding-top:5px">&nbsp;</li>
              <li style="width:10px; list-style:none; float:left; text-align:center; padding-top:5px">&nbsp;</li>
              <li style="width:220px; list-style:none; float:left; text-align:left; padding-top:5px">
                  <asp:Button ID="btn" runat="server" Text="Save" Width="80"
                      onclick="btn_Click" />&nbsp;<asp:Button ID="Button1" runat="server" Text="Reset" Width="80" /></li>
             </ul>
            </div>
          
        </asp:Panel>
        <asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="hp" PopupControlID="pnlModal" CancelControlID="btnClose" BackgroundCssClass="ModalBackgroundCSS">
        </asp:ModalPopupExtender>
         <asp:Panel ID="Panel1" runat="server" Width="400px" CssClass="ModalPopUpPanelCommonViewCRM" style="display:none" >
            <div align="right" style="background-color: #7DA5DB; height: 30px" class="grid_top">
                <asp:Button ID="Button2" runat="server" Text="X" CssClass="PoPUpCloseBtnCRM" CausesValidation="False"
                    ToolTip="Close" />
            </div>
            <div class="grid">
             <ul style="width:350px; list-style:none">
             <li style="width:120px; list-style:none; float:left; text-align:right; padding-top:3px">DID</li>
              <li style="width:10px; list-style:none; float:left; text-align:center; padding-top:3px">:</li>
              <li style="width:220px; list-style:none; float:left; text-align:left; padding-top:3px"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></li>
             </ul>
             <ul style="width:350px; list-style:none">
             <li style="width:120px; list-style:none; float:left; text-align:right; padding-top:3px">DNAME</li>
              <li style="width:10px; list-style:none; float:left; text-align:center; padding-top:3px">:</li>
              <li style="width:220px; list-style:none; float:left; text-align:left; padding-top:3px"><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>&nbsp;<asp:HyperLink ID="HyperLink1" runat="server" Text="New" NavigateUrl="#"></asp:HyperLink>
              </li>
             </ul>
              <ul style="width:350px; list-style:none">
             <li style="width:120px; list-style:none; float:left; text-align:right; padding-top:5px">&nbsp;</li>
              <li style="width:10px; list-style:none; float:left; text-align:center; padding-top:5px">&nbsp;</li>
              <li style="width:220px; list-style:none; float:left; text-align:left; padding-top:5px"><asp:Button ID="Button3" runat="server" Text="Save" Width="80" />&nbsp;<asp:Button ID="Button4" runat="server" Text="Reset" Width="80" /></li>
             </ul>
            </div>
          
        </asp:Panel>
         <asp:ModalPopupExtender ID="ModalPopupExtender2" TargetControlID="hlp" PopupControlID="Panel1" CancelControlID="Button2"  runat="server">
         </asp:ModalPopupExtender>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
  
    </form>
</body>
</html>

Friday, 17 May 2013

Pivot in sql server 2008

Press MePress Me
Press Me
Press Me
Press Me
 This table name is piv


select *
from
(
select  month,saleamount from piv
) as a
pivot
(
sum(saleamount) for month  in([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG])
)
as b
Another example
this table name is product

SELECT  * from
(
select 
 left(DATENAME(month, DATE),3) AS month,
price FROM    PRODUCT
) as a
pivot
(
sum(price) for month in([JAN],[FEB],[MAR])
)
as b
 Another example
SELECT  * from
(
select   datename(year,date) as year,
 left(DATENAME(month, DATE),3) AS month,
price FROM    PRODUCT
) as a
pivot
(
sum(price) for month in([JAN],[FEB],[MAR])
)
as b
Result

Tuesday, 14 May 2013

example of listbox in c#.net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication36
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //Remove from listbox;
        private void button1_Click(object sender, EventArgs e)
        {
            List<emp> lv = new List<emp>();
            lv = (List<emp>)listBox1.DataSource;
            if (listBox1.SelectedIndex >= 0)
            {
                lv.RemoveAt(listBox1.SelectedIndex);
                listBox1.DataSource = lv.ToList();
                listBox1.DisplayMember = "NAME";
                listBox1.ValueMember = "ID";
            }
        }
        public class emp
        {
            public int ID
            {
                get;
                set;
            }
            public string NAME
            {
                get;
                set;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
            List<emp> li = new List<emp>();
            emp o = new emp();
            o.ID=1;
            o.NAME="siv";
            li.Add(o);
            emp o1 = new emp();
            o1.ID = 2;
            o1.NAME = "sankar";
            li.Add(o1);
            listBox1.DataSource = li;
            listBox1.DisplayMember = "NAME";
            listBox1.ValueMember = "ID";
        }
        //Add in list box
        private void button2_Click(object sender, EventArgs e)
        {
            List<emp> lv1 = new List<emp>();
            emp o = new emp();
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                lv1.Add((emp)listBox2.Items[i]);
            }
            o = (emp)listBox1.SelectedItem;
            lv1.Add(o);
            listBox2.DataSource = lv1.ToList();
            listBox2.DisplayMember = "NAME";
            listBox2.ValueMember = "ID";

        }
    }
}

Monday, 6 May 2013

date different in sql server


SELECT        MU.Employee_ID, LDL.Status,LDL.Login_Date as [Date] ,
CONVERT(varchar(50),LDL.Login_Date, 108) AS checkIn,
CONVERT(varchar, LDL.Logout_Date, 108) AS checkOut, MU.Register_ID,
SR.Register_Name AS WorkStation, POST.Store_Name,
 DATEADD(SECOND, 0, CONVERT(TIME(0), ('23:59:59.000' -(LDL.Login_Date - LDL.Logout_Date))))AS Result,
 DATEDIFF(ss,LDL.Login_Date,LDL.Logout_Date)AS Result_InSec

FROM            Pos.LoginDataLog AS LDL INNER JOIN
                         Pos.ManageUser AS MU ON LDL.User_ID = MU.User_ID INNER JOIN
                         Pos.StoreRegister AS SR ON MU.Register_ID = SR.Register_ID INNER JOIN
                         Pos.POS_Store AS POST ON LDL.Store_ID = POST.Store_ID