Feeds:
Posts
Comments

For a two character string you can do this…

declare @number tinyint
set @number = 2
select case when len( @number ) = 1 then ’0′ else ” end + cast( @number as varchar(2) )

For a number that will be greater than two characters in length you can do this…

declare @number int
declare @string varchar(10)
declare @size_of_fixed_string tinyint
set @size_of_fixed_string = 10
set @number = 40
print replicate( ’0′, @size_of_fixed_string )
set @string = left( replicate( ’0′, @size_of_fixed_string ), @size_of_fixed_string – len( @number ) ) + cast( @number as varchar(10) )
print @string

Indian Currency Format

This post is to discus about Currency formats.

I have Searched on google for showing the price in indian currency like thousand, laks, crores(ex : 1,25,485.86) but there is no simple way to achieve that. Some one write a function to display in laks or crores, that was the base of my function. I have modified and wrote my own function, it may be usefull to all of you.

In VB.Net

——————————————-

Public Function IndianCurrency(ByVal Amount As Object) As String

        If IsDBNull(Amount) Or Amount.ToString() = “” Then

            Return “0.00″

        End If

        Amount = Convert.ToDecimal(Amount)

        Dim strTmp As String

        Dim strResult As String

        Dim intLen As Integer

        strTmp = Format(Amount, “#.00″)

        strResult = Right(strTmp, 6)

        intLen = Len(strTmp) – 6

        Do While intLen > 0

            strTmp = Left(strTmp, intLen)

            strResult = Right(strTmp, 2) & “,” & strResult

            intLen = Len(strTmp) – 2

        Loop

        Return strResult

    End Function 

 

In C#.Net

——————————————

public string IndianCurrency(object Amount)

     {

         if (Convert.ToString(Amount) == “” || Amount == null)

         {             return “0.00″;         }

         Amount = Convert.ToDecimal(Amount);

         string strTmp = null;

         string strResult = null;

         int intLen = 0;

         strTmp = string.Format(“{0:f}”, Amount);

         if (strTmp.Length > 6)

         {

             strResult = strTmp.Substring(strTmp.Length – 6);

             intLen = strTmp.Length – 6;

             while (intLen > 0)

             {

                 strTmp = strTmp.Substring(0, intLen);

                 if (intLen < 2)

                 {          strResult = strTmp.Substring(0, intLen) + “,” + strResult;       }

                 else

                 {         strResult = strTmp.Substring(intLen – 2, 2) + “,” + strResult;       }

                 intLen = strTmp.Length – 2;

             }

         }

         else

         {             strResult = strTmp;         }

         return strResult;

     }

You can call this function in databinding also

<%# IndianCurrency(Eval(“SellingPrice”))%>

if you want to assign a value to a control then

lblTotal.Text = IndianCurrency( 58745895.5844521);

Another formats

Response.Write(string.Format(“{0:#,##0.00;0.00}”, 558742255555.85562));            -       Ans : 558,742,255,555.86

Response.Write(string.Format(“{0:f}”, 558742255555.85562));                  -      Ans:  558742255555.86

Response.Write(string.Format(“{0:c}”, 558742255555.85562));                -         Ans: £558,742,255,555.86

happy coding. Continue Reading »

A simple line of code to popup browser favorites window is

window.external.addfavorite(“favoriteURL”,”prefered Name”);

Some times we are opening pages in new window by using javascript when we click on links. in this case some times we have to get the data(values) from parent window. this is posible by javascript.

parent window code

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>Untitled Page</title>
    <script type=”text/javascript” language=”javascript”>
    function openWindow()
    {
        window.open(‘child.aspx’,'Preview’,'scrollbars=yes,toolbar=no,width=50,height=50,top=40,left=40′);
    }   
    </script>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox><br />
        <asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>
        <br />
        <input id=”Button1″ type=”button” value=”preview” onclick=”openWindow();” />
    </div>
    </form>
</body>
</html>

Child window code

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>Untitled Page</title>
    <script type=”text/javascript” language=”javascript”>
    function displayValues()
    {
        document.getElementById(‘Label1′).innerText = window.opener.document.form1.TextBox1.value;
        document.getElementById(‘Label2′).innerText = window.opener.document.form1.TextBox2.value;
    }
    </script>
</head>
<body onload=”displayValues();”>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label><br />
        <asp:Label ID=”Label2″ runat=”server” Text=”Label”></asp:Label>
    </div>
    </form>
</body>
</html>

 I used this code to show preview of the submited data.

we can add items to ultraweblist directly if items are static is as follows.

<iglbar:UltraWebListbar ID=”UltraWebListbar1″ runat=”server” BorderWidth=”4px” Height=”350px”
      ViewType=”ExplorerBar” Width=”236px” BarWidth=”100%” BorderStyle=”None” GroupSpacing=”5px”
      ItemIconStyle=”SmallWithInsetText” MergeStyles=”True” HeaderClickAction=”ExpandOnClick”>
      <Groups>
      <iglbar:Group Text=”&amp;nbsp;Module1″ TextAlign=”Left”>
      <Labels Collapsed=”" Expanded=”" Selected=”" />
      <Items>
      <iglbar:Item Text=”<img src=’images/image1.gif’ alt=” height=’22px’ width=’22px’ /> Sub Module 1″ TargetUrl=”page1.aspx”>
      <Labels Selected=”" />
      </iglbar:Item>
      <iglbar:Item Text=”<img src=’images/image2.gif’ alt=” height=’22px’ width=’22px’ /> Sub Module 2″ TargetUrl=”page2.aspx”>
      <Labels Selected=”" />
      </iglbar:Item>
      <iglbar:Item Text=”<img src=’images/image3.jpg’ alt=” height=’22px’ width=’22px’ /> Sub Module 3″ TargetUrl=”page3.aspx”>
      <Labels Selected=”" />
      </iglbar:Item>
      <iglbar:Item Text=”<img src=’images/image4.jpg’ alt=” height=’22px’ width=’22px’ /> Sub Module 4″ TargetUrl=”page4.aspx”>
      <Labels Selected=”" />
      </iglbar:Item>
      </Items>
      </iglbar:Group>
      <iglbar:Group Text=”&amp;nbsp;Module2″ TextAlign=”Left”>
      <Labels Collapsed=”" Expanded=”" Selected=”" />
      <Items>
      <iglbar:Item Text=”<img src=’images/image5.JPG’ alt=” height=’25px’ width=’22px’ /> Sub Module 5″ TargetUrl=”page5.aspx”>
      <Labels Selected=”" />
      </iglbar:Item>
      </Items>
 <Items>
        <iglbar:Item Text=”<img src=’images/image6.JPG’ alt=” height=’25px’ width=’22px’ /> Sub Module 5″ TargetUrl=”page6.aspx”>
        <Labels Selected=”" />
        </iglbar:Item>
      </Items>
      </iglbar:Group>
      </Groups>
      <DefaultItemHoverStyle ForeColor=”Blue” Cursor=”Hand”>
      </DefaultItemHoverStyle>
      <DefaultItemSelectedStyle BorderStyle=”Solid” BorderWidth=”1px” Cursor=”Default”>
      </DefaultItemSelectedStyle>
      <DefaultItemStyle Cursor=”Default” Font-Names=”Verdana” Font-Size=”8pt” Height=”25px”>
      <Margin Left=”1px” />
      <BorderDetails ColorBottom=”Silver” ColorLeft=”Silver” ColorRight=”Silver” ColorTop=”Silver”
      StyleBottom=”Solid” StyleLeft=”None” StyleRight=”None” StyleTop=”None” WidthBottom=”2px” />
      </DefaultItemStyle>
      <DefaultGroupStyle BackColor=”WhiteSmoke” Cursor=”Default”>
      </DefaultGroupStyle>
      <DefaultGroupHeaderAppearance>
      <CollapsedAppearance ExpansionIndicatorImage=”downarrows_white.gif”>
      <Images>
      <ExpansionIndicatorImage Url=”downarrows_white.gif” />
      </Images>
      </CollapsedAppearance>
      <HoverAppearance>
      <Style BackgroundImage=”orangeExplorer.gif”></Style>
      </HoverAppearance>
      <ExpandedAppearance ExpansionIndicatorImage=”uparrows_white.gif”>
      <Images>
      <ExpansionIndicatorImage Url=”uparrows_white.gif” />
      </Images>
      <Style BackgroundImage=”blueExplorer.gif” Cursor=”Default” Font-Names=”Verdana”
      Font-Size=”10pt” Height=”23px” Width=”100%”>
      </Style>
      </ExpandedAppearance>
      </DefaultGroupHeaderAppearance>
</iglbar:UltraWebListbar>

some times we have to add items dynamically taking from database. for that code in vb.net is as follows

my data table is look like this.

Module

SubModule       

Order   

link        

ImageUrl

module1             

submodule1

1

url1

image1.gif

Module2

Submodule2

1

url2

Image2.gif

Module3

Submodule3

2

url3

Image3.gif

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dtbNavigation As New DataTable
        Dim da As New SqlDataAdapter(“Your Querry”, “your connection string”)
        da.Fill(dtbNavigation)
        If dtbNavigation.Rows.Count > 0 Then
            Dim i As Integer = 0
            ‘Dim grs As New WebUI.UltraWebListbar.Groups(UltraWebListbar1)
            Dim dtbModules As New DataTable
            dtbModules = dtbNavigation.DefaultView.ToTable(True, “Module”)
            For Each dr As DataRow In dtbModules.Rows
                Dim j As Integer = 0
                Dim gr As New WebUI.UltraWebListbar.Group()

                gr.Text = dr.Item(“Module”)
                gr.TextAlign = “left”
                gr.Labels.Collapsed = “”
                gr.Labels.Expanded = “”
                gr.Labels.Selected = “”
                Dim its As New WebUI.UltraWebListbar.Items(gr)
                Dim drSubModules() As DataRow
                drSubModules = dtbNavigation.Select(“ModuleName=’” & dr.Item(“Module”) & “‘”, “Order”, DataViewRowState.CurrentRows)

                For Each dr1 As DataRow In drSubModules
                    Dim it As New WebUI.UltraWebListbar.Item
                    it.Text = “<img src=’images/” & dr1.Item(“ImageUrl”) & “‘ alt=” height=’22px’ width=’22px’ />” & dr1.Item(“subModule”)
                    it.TargetUrl = dr1.Item(“link”)
                    ‘it.Image = “<img src=’~/images/” & dr1.Item(“ImageUrl”) & “‘ alt=” height=’22px’ width=’22px’ />”
                    it.Labels.Selected = “”
                    its.Insert(j, it)
                    gr.Items.Add(it)
                    j = j + 1

                Next
                UltraWebListbar1.Groups.Add(gr)
                i = i + 1
            Next
        End If
    End Sub

Hi in this post i am telling how to send a mail without using smtp server. this is possible in web application i didn,t check in windows. By using System.web.Mail namespace it is possible

code in VB.Net

Imports System.Web.Mail

Public Shared Function SendEmail(ByVal FromAddress As String, ByVal ToAddress As String, ByVal CCAddress, ByVal BccAddress, ByVal EmailSubject As String, ByVal MessageText As String) As Boolean
    Try
            Dim objMailMessage As New MailMessage()
            Dim obj As SmtpMail
            obj.SmtpServer = “”
            objMailMessage.From = FromAddress
            objMailMessage.To = ToAddress
     If CCAddress.Length > 0 Then
             objMailMessage.Cc = CCAddress
            End If
            If BccAddress.Length > 0 Then
             objMailMessage.Bcc = BccAddress
            End If
            objMailMessage.Subject = EmailSubject
            objMailMessage.Body = MessageText
            objMailMessage.BodyFormat = MailFormat.Html
            obj.Send(objMailMessage)
            Return True
        Catch ex As Exception
            WriteErrorToLog(GetExecutingScriptName() & ” – SendMail”, ex.Message)
            Return False
        End Try
End Function

To send a mail call the above function as
SendEmail(txtFromEmailID.Text.Trim(), txtToEmailID.Text.Trim(),”",”", txtSubject.Text.Trim(), strEMailText.ToString())

 

Code in C#.Net

using System.Web.Mail;

public static Boolean SendMail(string FromAddress,string ToAddress, string CCAddress, string BccAddress,string MailSubject, string MailBody)
    {
        try
        {
            System.Web.Mail.MailMessage msgMail = new MailMessage();
            msgMail.From = FromAddress;
            msgMail.To = ToAddress;
            if (CCAddress.Length > 0)
            {
                msgMail.Cc = CCAddress;
            }
            if (BccAddress.Length > 0)
            {
                msgMail.Bcc = BccAddress;
            }
            msgMail.Subject = MailSubject;
            msgMail.Body = MailBody;
            msgMail.BodyFormat = MailFormat.Html;
            SmtpMail.SmtpServer = “”;
            SmtpMail.Send(msgMail);

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

call this function as

SendEmail(txtFromEmailID.Text.Trim(), txtToEmailID.Text.Trim(),”",”", txtSubject.Text.Trim(), strEMailText.ToString())

We can attach a file also to this mail.

If you want to show sender Name in Inbox rather than mailid just give from address like this

objMailMessage.From = “SenderName<sendermailid@domai.com>”

Exporting grid to Excel

This is to get the gridview values of your page in to microsoft excel. Recently i tried this. we can export values from gridview or DataList or any DataRenderControl. For this code is as follows..

first in aspx page put a gridview and a button to export to excel and in page load bind data to gridview.

in button click write this code.

Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles  btnExcel.Click

Response.ContentType = “application/vnd.ms-excel”

Dim objStringWriter As New IO.StringWriter

Dim objHtmlTextWriter As New Html32TextWriter(objStringWriter)gdvDetails.RenderControlobjHtmlTextWriter)

Response.AppendHeader( ”Content-Disposition”, “attachment; filename=Details.xls”)

Response.Write(objStringWriter)

Response.Flush()

Response.End()

End Sub

when you click on this button it gives a popup like “download file menu” with the name “Details.xls”. you can open or save into your system

Client IP Address

when ever you want to find the IP Adress of client use one of these two Response.Write(Request.UserHostAddress) or
Response.Write(Request.ServerVariables(“LOCAL_ADDR”))
But remember one thing you have to access this web page with server address as

http://yourSyatemIP(or)Servername/projectName/page.aspx

not with localhost like 

http://localhost/projectName/page.aspx

Below code is to find the Different types of server variables

Dim NameArray() As String
Dim VisitorSV As NameValueCollection
Dim I As Integer
VisitorSV = Request.ServerVariables
NameArray = VisitorSV.AllKeys
For I = 0 To UBound(NameArray)
Response.Write(“<br>”)
Response.Write(NameArray(I) & “: ” & VisitorSV.Item(I) & “<BR>”)
Next

System.AppDomain.CurrentDomain.BaseDirectory

This returns the string of your project path including project name

ex: c:\Inetpub\wwwroot\mysite\

Email validation

checking email validation by java script.

aspx page

<script language=”javascript” type=”text/javascript”>

function checkmail()

{

var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

     var MailID = document.getElementById(‘txtMailID’);

     var returnval=emailfilter.test(MailID.value)

     if (returnval==false)

      {

         alert(“Please enter a valid email address.”);

        MailID.focus();
      }

     return returnval;

}

<script>

<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” />

then add an attribute in codebehind file to submit buttion

 btnSubmit.Attributes.Add(

“OnClick”, “return checkmail();”
 its equal to email validator in .net

Follow

Get every new post delivered to your Inbox.