Monday, 28 July 2014

Use of case in where clause

SELECT * 
FROM    tableName
WHERE   dateCol =   CASE WHEN @considerDate =1
                         THEN @date 
                         ELSE dateCol 
                    END

Friday, 25 July 2014

Upper Case in TextBox by CSS

<style>
        .uppercase {text-transform:uppercase;}
        .lowercase {text-transform:lowercase;}
        .capitalize {text-transform:capitalize;}
</style>

<asp:TextBox ID="txtName" runat="server" Width="555px" class="uppercase" MaxLength="75"></asp:TextBox>

Wednesday, 23 July 2014

DataTable To Excel in VB.NET

  Public Sub ExportToExcel(ByVal dt As DataTable)
        If dt.Rows.Count > 0 Then
            Dim filename As String = "DownloadMobileNoExcel.xls"
            Dim tw As System.IO.StringWriter = New System.IO.StringWriter()
            Dim hw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(tw)
            Dim dgGrid As DataGrid = New DataGrid()
            dgGrid.DataSource = dt
            dgGrid.DataBind()

            'Get the HTML for the control.
            dgGrid.RenderControl(hw)
            'Write the HTML back to the browser.
            'Response.ContentType = application/vnd.ms-excel;
            Response.ContentType = "application/vnd.ms-excel"
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "")
            Me.EnableViewState = False
            Response.Write(tw.ToString())
            Response.End()
        End If
    End Sub

Tuesday, 15 July 2014

Proper Case in SQL

CREATE FUNCTION ProperCase(@OriginalText VARCHAR(8000))
RETURNS VARCHAR(8000) 
BEGIN
DECLARE @CleanedText VARCHAR(8000)
;with
  a1 as (select 1 as N union all select 1 union all
         select 1      union all select 1 union all
         select 1      union all select 1 union all
         select 1      union all select 1 union all
         select 1      union all select 1),
  a2 as (select 1 as N from a1 as a cross join a1 as b),
  a3 as (select 1 as N from a2 as a cross join a2 as b),
  a4 as (select 1 as N from a3 as a cross join a2 as b),
  Tally as (select top (len(@OriginalText)) row_number() over (order by N) as N from a4)
 
SELECT @CleanedText = ISNULL(@CleanedText,'') + 
     --first char is always capitalized?
CASE WHEN Tally.N = 1 THEN UPPER(SUBSTRING(@OriginalText,Tally.N,1))
     WHEN SUBSTRING(@OriginalText,Tally.N -1,1) = ' '  THEN UPPER(SUBSTRING(@OriginalText,Tally.N,1))
     ELSE LOWER(SUBSTRING(@OriginalText,Tally.N,1))
END
    
FROM Tally           WHERE Tally.N <= LEN(@OriginalText)           
               
RETURN @CleanedText
END
GO


select dbo.ProperCase('WEST BENGAL')

Monday, 7 July 2014

Indian Pan card Validation by Regular Expressions

Indian Pan card Validation by Regular Expressions

// Total Length 10 Digit 
// 5 Character value  4 numeric value 1 Character value 
 System.Text.RegularExpressions.Regex rPan = 
      new System.Text.RegularExpressions.Regex 
          (@"^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}?$");
           if (txt_PanNo.Text.Trim().Length > 0)
           {
                if (!rPan.IsMatch(txt_PanNo.Text.Trim()))
                {
                   return true;
                }
                else
                {
                    return false;
                }
           }

Thursday, 3 July 2014

My First Use of Stored Procedures with LINQ to SQL

How to Use Stored Procedures with LINQ to SQL

try
        {
            MyFirstDataClassesDataContext db=new MyFirstDataClassesDataContext();

            var q = from p in db.Test_SD_TrTe_Details_fn("0000000175", "2014", "TR")

                    select new
                    {
                        name = p.Name,Address=p.Address
                    };

            GridView1.DataSource = q;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }

My First LINQ

            try            {                              
               MyFirstDataClassesDataContext db = new MyFirstDataClassesDataContext();
                var country =from c in db.All_Countries                   
                    select c;
                if (Enumerable.Count(country)>0)
                {                
                    GridView1.DataSource = country;
                    GridView1.DataBind();
                }
                    else
                {
                    GridView1.DataSource = null;
                    GridView1.DataBind();
                }
              
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
               //
            }