How to get missing leading zero while conversion of number to string in javascript

In this tutorial I will discuss about how to get missing leading zero while conversion of number to string in javascript. I have faced this problem today and at that time i thought to share with you people so that you can save your half hour that i have wasted today for solving this problem :)

How to get missing leading zero while converting number to string in javascript

Today while developing the countdown module, i got a requirement to show the days value which was a number like 01,02,03,04.... up to 30, in two different spans in such a way that the leading 0 of 01 value should be shown in first span and then 1 in second span, likewise if we have 21 as a value then 2 will be shown in first span and 1 will be shown in second span. I Just want to clear your confusion, in my example the value of days variable change dynamically when countdown start. I was very much amazed when i saw the conversion of number 01 to string resulting in only 1, the conversion of number 02 to string resulting in only 2, and then suddenly i thought where is leading zero???
<script language="javascript" type="text/javascript">
var days=01;
var daysString=Number(days);
daysString=daysString.toString();
n1=daysString.substring(0,1);
n2=daysString.substring(1,2);
alert(n1);//it should give you 0 but it will give you 1
alert(n2);//It should give you 1 but it will give you nothing
</script>

So to solve this problem and get missing leading zero, you have to follow the example given below
<script language="javascript" type="text/javascript">
var days=01;
var daysString=Number(days);
daysString=daysString.toString();
if(daysString.length>1)
{
n1=daysString.substring(0,1);
n2=daysString.substring(1,2);
}
else if(daysString.length==1)
{
n1="0";
n2=daysString.substring(0,1);
}
else
{
n1="0";
n2="0";
}
alert(n1);//Now it will give you 0
alert(n2);//Now it will give you 1
</script>

The variable days store integer(Number, Numeric) value that comes dynamically, that's why the value is not surrounded by any " ". If days="01" then there was no problem by getting 01 as a 0 and 1 because any thing inside the double quotes is called string and we can get substring easily from string.

I know this tutorial is a bit technical but you will find this very much handy.

Thanks and Keep in touch.

Read more...

How to get the IP address in php

In this tutorial you will learn how to get the IP address in php. Sometimes $_SERVER['REMOTE_ADDR'] cannot give you the exact IP address. If your visitor is connected to the Internet through Proxy Server then $_SERVER['REMOTE_ADDR'] just returns the IP address of the proxy server not of the visitor's machine. Let's have a look over how to get the almost real one.

How to get IP address in php

function getIpAddress()
{
    if (trim($_SERVER['HTTP_CLIENT_IP'])!="")   //checking ip address from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    else if (trim($_SERVER['HTTP_X_FORWARDED_FOR'])!="")   //checking whether ip address is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$ip_address = getIpAddress(); // Get the visitor's IP Address
echo $ip_address;

The above code snippet first tries to get the direct IP address of visitor’s machine, if not available then try for forwarded for IP address using HTTP_X_FORWARDED_FOR. And if this is not available too then it will finally get the IP address using REMOTE_ADDR. So that's it.


Happy Coding!!!
Read more...

Difference between varchar and nvarchar

The wide range of data types in MS SQL Server can sometimes throw people through a loop, especially when the data types look highly interchangeable. VARCHAR and NVARCHAR seem to be highly interchangeable but there is some sort of difference between them. Let’s discuss it.

Difference between varchar and nvarchar

VARCHAR is an abbreviation for variable-length character string. It is a string of text characters that can be as large as the page size for the database table holding the column in question. Normally the size for a table page is 8,196 bytes, and no one row in a table can be more than 8,060 characters. So the maximum size of a VARCHAR is 8,000 bytes. Varchar stores ascii data.

The additional "N" in NVARCHAR means uNicode. Essentially, NVARCHAR is identical to VARCHAR but it supports two-byte characters. The most common use for this type of thing is to store character data that is a mixture of English and non-English symbols such as English and Chinese. Simply nvarchar stores unicode data and takes twice space than varchar

So that's it. I love your feedback.
Read more...

Send email with CC and BCC in php

In this tutorial you will learn how to send email with CC and BCC in php. I have already written a tutorial about sending html emails using php in which I have written a custom made function send_email($from, $to, $subject, $message) to send html email. You just have to add the two lines of code in that function and that's it. Let's have a look over how to do so

Send email with CC and BCC in php

function send_email($from, $to, $subject, $message){
$headers = "From: ".$from."\r\n";
$headers .= "Reply-To: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n"; 
$headers .= "CC: sombodyelse@gmail.com\r\n";
$headers .= "BCC: hiddenemail@gmail.com\r\n";
//set content type to HTML 
$headers .= "Content-type: text/html\r\n"; 

if ( mail($to,$subject,$message,$headers) ) 
{
header("location:yourdesirepage.php?msg=sent");

} 
else 
{
header("location:yourdesirepage.php?msg=notsent");
}
}
//Function ends here
$from_email="fromyou@gmail.com";
$to_email="tosomeone@gmail.com";
$subject="Subject of Email";
$message.="<html><body>";
$message.="<strong>Your message here will be look bold when visible to reader</strong>";
$message.="Some more message";
$message.="</body></html>";
send_email($from_email,$to_email,$subject,$message);



In the send_email() function, i used two more headers CC and BCC to send email with CC and BCC in php.

$headers .= "CC: sombodyelse@gmail.com\r\n";
CC means Carbon Copy. A comma separated list of more recipients that will be seen by all other recipients.
$headers .= "BCC: hiddenemail@gmail.com\r\n";

BCC means Blind Carbon Copy. A comma separated list of more recipients that will not be seen by any other recipients.

Note:-Be remember, Header names are case sensitive. Each header is ended with the Return and Newline characters \r\n. There are no spaces among header name.


So that's it.




Happy coding!!!
Read more...

Import excel data to gridview

In this tutorial you will learn how to import excel data to gridview control. I have already written multiple posts for exporting gridview data to excel with different techniques, now its time to import records. Let's have a look over how to do so

Import excel data to gridview

.aspx page

<table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td align="left" class="text" style="width: 114px;">
                           Import Data from Excel File
                        </td>
                        <td align="left">
                            <asp:FileUpload ID="uploadExcel" runat="server" />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:GridView ID="GridView1" runat="server">
                                <Columns>
                                    <asp:TemplateField HeaderText="First Name">
                                        <ItemTemplate>
                                            <asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("first_name") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                    <asp:TemplateField  HeaderText="Last Name
                                        <ItemTemplate>
                                            <asp:Label ID="lblLastName" runat="server" Text='<%# Bind("last_name") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Left" />
                                    </asp:TemplateField>
                                    <asp:TemplateField  HeaderText="Gender">
                                        <ItemTemplate>
                                            <asp:Label ID="lblGender" runat="server" Text='<%# Bind("gender") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                        </td>
                    </tr>
                </table>



Simply we have a fileupload control, button control and gridview control in our page. We will use file upload control to upload the excel file and then using its physical path we will get records from it and display them to gridview control.


.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Configuration;

protected void btnImport_Click(object sender, EventArgs e)
    {
        try
        {
            string path = uploadExcel.FileName;
            uploadExcel.SaveAs(Server.MapPath("~/Upload/" + path));
            OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(path)+";Extended Properties=Excel 8.0");                
            oconn.Open();    
//Getting records from Sheet1 of excel file. As you know one excel file may have many sheets                    
            OleDbCommand ocmd = new OleDbCommand("select * from [Sheet1$]", oconn);
            DataSet ds = new DataSet();
            OleDbDataAdapter odapt = new OleDbDataAdapter(ocmd);
            odapt.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            oconn.Close();            
        }
        catch(Exception exp)
        {
            throw exp;
        }            
    }

Note:- Don't forget to import namespaces that i have used above.


So this is the way to import excel data to gridview


Happy Coding!!!
Read more...