Tuesday, March 24, 2015

How to get IP Address of Visitors Machine || Client Machine in ASP.Net

Description: -This post explains you to know how to get the client IP Address from ASP.Net c#
C#
string ipaddress;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
    ipaddress = Request.ServerVariables["REMOTE_ADDR"];

When users are behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to checkHTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address. If there are multiple proxy servers the IP Addresses of all of them are appended to the client machine IP Address.
Hence we need to first check HTTP_X_FORWARDED_FOR and then REMOTE_ADDR.

Your IP Address like …………
192.658.55.36
Note: - While executing this code it will show the wrong IP Address in local development in some case. After deploy in IIS it will show the correct address.


Read More »

How to create the Capture Image || Login Image authentication creation in asp.net c#

How to create the Capture Image || Login Image authentication creation in asp.net c#

Step 1:- Create one asp page and name it as CaptureImage.aspx

In .CS file write below code
namespace IDMSUI
{
    public partial class CaptureImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetCaptureImage();
        }
        public void GetCaptureImage()
        {
            using (Bitmap b = new Bitmap(150, 40, PixelFormat.Format32bppArgb))
            {
                using (Graphics g = Graphics.FromImage(b))
                {
                    Rectangle rect = new Rectangle(0, 0, 149, 39);
                    //Rectangle rect = new Rectangle(0, 0, 100, 25);
                    g.FillRectangle(Brushes.White, rect);

                    // Create string to draw.
                    Random r = new Random();
                    int startIndex = r.Next(1, 5);
                    int length = r.Next(5, 10);
                    String drawString = Guid.NewGuid().ToString().Replace("-", "0").Substring(startIndex, length);
                    Session["CaptureImageCode"] = drawString;
                    // Create font and brush.
                    Font drawFont = new Font("Arial", 16, FontStyle.Italic);
                    //Font drawFont = new Font("Arial", 16, FontStyle.Italic | FontStyle.Strikeout);
                    //Font drawFont = new Font("Arial", 16, FontStyle.Italic);
                    using (SolidBrush drawBrush = new SolidBrush(Color.Black))
                    {
                        // Create point for upper-left corner of drawing.
                        PointF drawPoint = new PointF(15, 10);

                        // Draw string to screen.
                        g.DrawRectangle(new Pen(Color.Red, 0), rect);
                        g.DrawString(drawString, drawFont, drawBrush, drawPoint);
                    }
                    //b.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                    //context.Response.ContentType = "image/jpeg";
                    //context.Response.End();
                    this.Response.Clear();
                    this.Response.ContentType = "image/jpeg";
                    // Write the image to the response stream in JPEG format.
                    b.Save(this.Response.OutputStream, ImageFormat.Jpeg);
                    // Dispose of the CAPTCHA image object.
                    b.Dispose();
                }
            }
        }
    }
}

Read More »

How to bind the Status type Columns with the grid view using ENUM & Dictionary

How to bind the Status type Columns with the grid view using ENUM & Dictionary

Step 1:- Create one class with  enum and Dictionary
namespace Utility
{
    public static class Constants
    {
        public enum ConstantStatus
        {
            [Description("New")]
            New = 0,
            [Description("In process")]
            Inprocess = 1,
            [Description("Completed")]
            Completed = 2,
        };

        public static Dictionary<int, string> StatusDictionary = new Dictionary<int, string>()
        {
          {0,"New"},
          {1,"In process"},
          {2,"Completed"}
        };
    }
}

Read More »

Monday, March 9, 2015

How to bind the Status type Columns with the grid view using ENUM & Dictionary

How to bind the Status type Columns with the grid view using ENUM & Dictionary

Step 1:- Create one class with  enum and Dictionary
namespace Utility
{
    public static class Constants
    {
        public enum ConstantStatus
        {
            [Description("New")]
            New = 0,
            [Description("In process")]
            Inprocess = 1,
            [Description("Completed")]
            Completed = 2,
        };

        public static Dictionary<int, string> StatusDictionary = new Dictionary<int, string>()
        {
          {0,"New"},
          {1,"In process"},
          {2,"Completed"}
        };
    }
}

Step 2:- In Grid
How to bind the template field with enum and dictionary process.
ENUM
<ItemTemplate>
    <asp:Label ID="lblstatus" runat="server" Text='<%# Enum.GetName(typeof(Utility.Constants.ConstantStatus),Convert.ToInt32(Eval("Status"))) %>'></asp:Label>
</ItemTemplate>
Dictionary
<ItemTemplate>
    <asp:Label ID="lblstatus" runat="server" Text=' <%#( Utility.Constants.StatusDictionary[Convert.ToInt32(Eval("Status"))]) %>'></asp:Label>
</ItemTemplate>


Read More »

Wednesday, March 4, 2015

Convert EXCEL to DataTable in the asp.net c#

Convert EXCEL to DataTable in the asp.net c#


Step 1:- Get the absolute path for the Excel sheet .I used “Sample.xlsx”
string data = MapPath("sample.xlsx").ToString();

Step2:- Create the connection string for the OLEDB connection
String strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
               "Data Source=" + data + ";" +
               "Extended Properties=Excel 12.0 Xml";

Step3:- Get the list of sheets names in the excel sheet in to list
List<string> listSheet = new List<string>();
            using (OleDbConnection conn = new OleDbConnection(strConn.ToString()))
            {
                conn.Open();
                System.Data.DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
               
                foreach (DataRow drSheet in dtSheet.Rows)
                {
                    if (drSheet["TABLE_NAME"].ToString().Contains("$"))
                    {
                        listSheet.Add(drSheet["TABLE_NAME"].ToString());
                    }
                }
            }

Step4:- By using that sheet name get the table in the sheets and passed to the data table.
OleDbDataAdapter da = new OleDbDataAdapter
                    ("SELECT * FROM [" + listSheet[0]+"]", strConn);
            da.Fill(ds);

The Complete Code is here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Excel;
using System.Data;
using System.Data.OleDb;

namespace SampleTestingCodes
{
    public partial class ExcelToDataTable : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
            string data = MapPath("sample.xlsx").ToString();
            String strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
               "Data Source=" + data + ";" +
               "Extended Properties=Excel 12.0 Xml";

            DataSet ds = new DataSet();
            List<string> listSheet = new List<string>();
            using (OleDbConnection conn = new OleDbConnection(strConn.ToString()))
            {
                conn.Open();
                System.Data.DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
               
                foreach (DataRow drSheet in dtSheet.Rows)
                {
                    if (drSheet["TABLE_NAME"].ToString().Contains("$"))
                    {
                        listSheet.Add(drSheet["TABLE_NAME"].ToString());
                    }
                }
            }
            OleDbDataAdapter da = new OleDbDataAdapter
                    ("SELECT * FROM [" + listSheet[0]+"]", strConn);
            da.Fill(ds);

        }
    }
}


Thanks & Regards
LOKESH


Read More »