2017/12/05

How to find client's MAC Address in Asp.Net and C#.net

For Web Application

using System.Runtime.InteropServices;

[DllImport("Iphlpapi.dll")]
    private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
    [DllImport("Ws2_32.dll")]
    private static extern Int32 inet_addr(string ip);

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string userip = Request.UserHostAddress;
            string strClientIP = Request.UserHostAddress.ToString().Trim();
            Int32 ldest = inet_addr(strClientIP);
            Int32 lhost = inet_addr("");
            Int64 macinfo = new Int64();
            Int32 len = 6;
            int res = SendARP(ldest, 0, ref macinfo, ref len);
            string mac_src = macinfo.ToString("X");
            if (mac_src == "0")
            {
                if (userip == "127.0.0.1")
                    Response.Write("visited Localhost!");
                else
                    Response.Write("the IP from" + userip + "" + "<br>");
                return;
            }

            while (mac_src.Length < 12)
            {
                mac_src = mac_src.Insert(0, "0");
            }

            string mac_dest = "";

            for (int i = 0; i < 11; i++)
            {
                if (0 == (i % 2))
                {
                    if (i == 10)
                    {
                        mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
                    }
                    else
                    {
                        mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                    }
                }
            }

            Response.Write("welcome" + userip + "<br>" + ",the mac address is" + mac_dest + "."

             + "<br>");
        }
        catch (Exception err)
        {
            Response.Write(err.Message);
        }
    }
-----------------------------------------------------------------------------------------------------------------------


For Window Application

public string GetMACAddress()
{
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    String sMacAddress = string.Empty;
    foreach (NetworkInterface adapter in nics)
    {
        if (sMacAddress == String.Empty)// only return MAC Address from first card 
        {
            IPInterfaceProperties properties = adapter.GetIPProperties();
            sMacAddress = adapter.GetPhysicalAddress().ToString();
        }
    } return sMacAddress;

}

2017/11/10

Use XML Data For Save in Database

CREATE PROCEDURE [dbo].[SaveData]
@MH INT,
@TxTest INT,
@ID INT,
@CreatedBy INT,
@strXML VARCHAR(MAX)
AS
BEGIN

SET NOCOUNT ON;

DECLARE @XMLDocHandle INT;

DECLARE @InvestorID INT
SET @ID=(SELECT Id FROM tbl_Test WHERE  ID=@ID)

IF(@strXML IS NOT NULL AND @strXML<>'')
BEGIN
EXEC sp_xml_preparedocument @XMLDocHandle OUTPUT, @strXML

CREATE TABLE #temp(ID INT,ClientID INT,InvID INT,Name VARCHAR(100),MaxAmount NVARCHAR(20),BankName NVARCHAR(500),
AN VARCHAR(30),Status VARCHAR(20),CreatedDate DATETIME,CreatedBy VARCHAR(10),StartDate DATETIME)

INSERT INTO #temp
SELECT ID,ClientID,@InvestorID,MName,MA,BName,AN,Status,GETDATE(),@CreatedBy,GETDATE()
FROM OPENXML (@XMLDocHandle, 'NewDataSet/Table1',2) 
WITH(
ID VARCHAR(10) 'ID',
ClientID VARCHAR(10) 'ClientID',
MN VARCHAR(100) 'MN',
MA NVARCHAR(20) 'MA',
BN NVARCHAR(500) 'BN',
ANumber VARCHAR(30) 'ANumber',
Status VARCHAR(20) 'Status',
CreatedBy VARCHAR(10) 'CliID'
)


MERGE INTO IMan IM
USING #temp T ON IM.InvestorID=T.InvID
WHEN MATCHED AND IM.InvestorID=T.InvID THEN

UPDATE SET
IM.CliID=T.ClientID,
IM.InID=T.InvestorID,
IM.MName=T.MName
WHEN NOT MATCHED  THEN
INSERT (ClID,IID,MName,MAmount,BName,ANumber,Status,CreatedDate,CreatedBy,SponsorBank,
SpCode,UCode,CompanyName,Action,DAmount,SOrRefNo,Frequency,UCancelled,CustomerAdditionalInfo,StartDate)
VALUES(T.ClientID,T.InvestorID,T.MandateName,T.MAmount,T.BName,T.ANumber,T.Status,T.CreatedDate,T.CreatedBy,
'HD','Xyz','Xyz','Xyz','Debit','Upto Max Amount',T.MName,'As and when Presented',
1,'NA',T.StartDate);

SELECT 1 'Status'
END


END

2017/09/11

ASP.NET Session States in SQL Server Mode

ASP.NET Session States in SQL Server Mode 


1. Run --> Cmd press enter
2. on DOS screen , type "C:\Windows\Microsoft.NET\Framework64\v4.0.30319" press enter
3. type "aspnet_regsql.exe -S  Test_DB -U sa -P bajaj@123 -ssadd -sstype p" press enter

Then ASP State is in Database created.


Then Go to web application and define the session state mode in web.config

<system.web>
<sessionState mode="SQLServer" timeout="30" sqlConnectionString="Data Source=192.168.68.10; User ID=sa;Password=Test@123;" cookieless="false" />
</system.web>