Thursday, May 3, 2012

Silverlight Isolated Storage | Set machine id from web application |

hi Guys,
This is my First article from Silverlight.In General we might have come crossed a situation like restricting a computer that access a web app. we don't have such trust level and it is impossible to get the machine id(finger print), CPU ID,or IP Address - we could get ip address if user uses dynamic ip or hide my ip, then it is meaningless. after i googled for a week and a study, i turned to Silverlight Isolated Storage.

here we will generate a 4digit GUID which is a Machine ID i generated and stored; later i verify this Guid and make my changes with Isolated Storage.

There are some issues(Browser compatibility) with key,value pairs. so best practice is to store a file and read from it - IsolatedStorageFile.GetUserStoreForApplication();

Here is my sample code:


#region GetMacID
        /// <summary>
        /// <para>It Sets and Gets the MacID in Client Machine</para>
        /// <b>returns MacID</b>
        /// </summary>
        /// <returns>returns MacID</returns>
        public static string GetMacID()
        {
            string FolderName = "13aw_13en"; // Specify a strong name; so that user/developer cannot search your application folder / fileanme
            string ISOLATED_FILE_NAME = @FolderName + "/ 13aw_13en.xml";
            string _strMacID = string.Empty;
            try
            {
                var store = IsolatedStorageFile.GetUserStoreForApplication(); // Initializing IsolatedStorageFile

                if (store.FileExists(ISOLATED_FILE_NAME))
                {
                    using (StreamReader reader = new     StreamReader(store.OpenFile(ISOLATED_FILE_NAME, FileMode.Open)))
                    {
                        XDocument _xDoc = XDocument.Parse(reader.ReadToEnd()); // XDocument  - best way to read XML; Linq to XML add System.XML.Linq
                        _strMacID = _xDoc.Element("MachineID").Element("macid").Attribute("value").Value;
                        return _strMacID;
                    }
                }
                else
                {
                    if (!store.DirectoryExists(FolderName))
                    {
                        store.CreateDirectory(FolderName);
                    }
                    if (!store.FileExists(ISOLATED_FILE_NAME)) // Creating Folders and writing the info
                    {
                        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(ISOLATED_FILE_NAME, FileMode.Create, store))
                        {
                            XmlWriterSettings settings = new XmlWriterSettings();
                            settings.Indent = true;
                            using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
                            {
                                writer.WriteStartElement("RavenMachine");
                                writer.WriteStartElement("macid");
                                writer.WriteStartAttribute("value");
                                writer.WriteString(Guid.NewGuid().ToString().Replace("-", string.Empty).Substring(0, 4));
                                writer.WriteEndAttribute();
                                writer.WriteEndElement();
                                writer.Flush();
                            }
                        }
                    }
                    if (store.FileExists(ISOLATED_FILE_NAME))
                    {
                        using (StreamReader reader = new StreamReader(store.OpenFile(ISOLATED_FILE_NAME, FileMode.Open)))
                        {
                            XDocument _xDoc = XDocument.Parse(reader.ReadToEnd());
                            _strMacID = _xDoc.Element("RavenMachine").Element("macid").Attribute("value").Value;
                            return _strMacID;
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                //ISFS Exception
            }
            return _strMacID;
        }
        #endregion
IsolatedStorageFile are Disposable methods so surround with using for Garbage Collection.
Please let me do know if you have any queries.
Thank you

Tuesday, November 22, 2011

Asp.Net 3.5 | C#: How to show the message and redirect to other page in ASP.NET

Asp.Net 3.5 | C#: How to show the message and redirect to other page in ASP.NET

How to show the message and redirect to other page in ASP.NET

The below code snippet is used to show the message and redirect to other page: 

ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Access Denied. Please contact Administrator for access to this section.');location.href = 'login.aspx';", true);

The below code snippet is used to show the message and redirect to other page with parameters:

ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Data updated successfully. You will now redirected to search page.');location.href = 'somepage.aspx?name=" + name + "&address=" + address + "&state=" + state + "';", true);

The below code snippet is used to show the message and redirect to other page with parameters based on user confirm:

ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "var r = confirm('Data updated successfully. You will now redirected to search page.'); if (r == true) var str= 'somepage.aspx?name=" + name + "&address=" + address + "&state=" + state + "'; location.href = str ;", true);

Monday, October 31, 2011

How do you use the Query Designer in SQL Server 2008?

Well, there are a few steps you need to follow. This note shows how you may create 
and execute a query in the SQL Server Management Studio. 
I am using a copy of the Northwind database.

Step 1:

Click on New Query toolbar item
[directly below File in SQL Server Management Studio]

Step 2:

In the new empty query created [herein SQLQuery1.sql - Hodentek2\...].

Right click on an empty area and choose Design Query in Editor





















Query Designer pops-up with the Add Table modal window on top of it as shown.






















Step 3:

Hold down Control Key and highlight Products and Suppliers in the Add table list

and click on the Add button. After the tables are added close the window.

The Tables get added to the Query Designer together with the relationship between them as shown.











Step 4:

Place check marks for the columns in Products and Suppliers as shown.
You do not need to do anything with the Grid Pane or the SQL Pane below it.

just observe them as you place the check marks.















Step 5:

Execute the query in the designer and if you are satisfied click OK.
The SQL Statement gets in to the Query window.
Study the SQL Statement.

You are done!!
You successfully used the Query Editor to query two tables in the database.

Wednesday, August 31, 2011

IIS resetting Commands | Failed to access metabase


Open cmd then type the below commands:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -i
and Hit enter
and again
c:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -ga ASPNET

Hit, then open your IIS & Check your virtual directory.

Thursday, July 28, 2011

Show Message Box in C# Web application

string message = "Type your message here !";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");                           
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());   
   

asp.net | Create HTTP request to Web service


//sample.cs/asmx

[ScriptMethod(UseHttpGet = true)]
[WebMethod]
public XmlDocument login(string username, string password)
{
   XmlDocument oXmlDoc = new XmlDocument();

   XmlNode oXmlmessage = oXmlDoc.CreateNode(XmlNodeType.Element, "message", "");
   XmlAttribute oxmllogin = oXmlDoc.CreateAttribute("loginstatus");
   oxmllogin.InnerText = "OK";

   XmlAttribute oXmluserid = oXmlDoc.CreateAttribute("userid");
   oXmluserid.InnerText = iRegID.ToString();

   oXmlmessage.Attributes.Append(oxmllogin);
   oXmlmessage.Attributes.Append(oXmluserid);

   oXmlDoc.AppendChild(oXmlmessage);

   return oXmlDoc;
}

invoking method: find the http request....
StringBuilder sb = new StringBuilder();

    byte[] buf = new byte[8192];


HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/mylytica/apibridge.asmx/login?username=Shankar@solveitcorp.com&password=secure2010");

    HttpWebResponse response = (HttpWebResponse)
        request.GetResponse();

    Stream resStream = response.GetResponseStream();

    string tempString = null;
    int count = 0;

    do
    {
        count = resStream.Read(buf, 0, buf.Length);
        if (count != 0)
        {
            tempString = Encoding.ASCII.GetString(buf, 0, count);
            sb.Append(tempString);
        }
    }

    while (count > 0);

    Literal1.Text = sb.ToString();