Monday, May 2, 2016

Reading data from a xml string

string xmlString =  "Names" +
                        "Name" +
                            "FirstName John /FirstName" +
                            "LastName Smith /LastName" +
                        "/Name" +
                        "Name" +
                            "FirstName James /FirstName" +
                            "LastName White /LastName" +
                        "/Name" +
                    "/Names";

XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlString);

XmlNodeList xnList = xml.SelectNodes("/Names/Name");
foreach (XmlNode xn in xnList)
{
    string firstName = xn["FirstName"].InnerText;
    string lastName = xn["LastName"].InnerText;
    Console.WriteLine("Name: {0} {1}", firstName, lastName);
}

Thursday, October 15, 2015

Logging into database using Enterprise Logging


Database _database = DatabaseFactory.CreateDatabase();
DbCommand _command = _database.GetStoredProcCommand("STOREDPROCEDURENAME");
_database.AddInParameter(_command, "@FIELDNAME", DbType.String, "FIELDVALUE");
_database.ExecuteNonQuery(_command);

Wednesday, July 22, 2015

Get the query string value from URL using javascript

 function getQueryString(key){
         var regex=new RegExp('[\\?&]'+key+'=([^&#]*)');
         var qs=regex.exec(window.location.href);
         return qs[1];
       }

Friday, February 20, 2015

Print a selected area on a page

On the style tag add the below contents

style type="text/css" media="print"
.no-print { display: none; }

The contents which do not have to be printed can be given under a div tag with the class name "no=print"


Friday, February 13, 2015

Upload multiple files using jQuery plugin


  • Refer the jquery.MultiFile.js
  • Add a ASP.Net file upload control to the page
  • Add the following code on the button click event for uploading the files

HttpFileCollection fileCollection = Request.Files;
for (int i = 0; i < fileCollection.Count; i++)
{
    HttpPostedFile uploadfile = fileCollection[i];
    string fileName = Path.GetFileName(uploadfile.FileName);
    if (uploadfile.ContentLength > 0)
    {
        uploadfile.SaveAs(Server.MapPath("~/UploadedFiles/") + fileName);
        lblMessage.Text += fileName + "Saved!!!";
    }
}

The multiple file upload plugin can be downloaded from here

Friday, January 23, 2015

Monday, December 29, 2014

Consuming a REST service

            string jsonRequest = "REST URL";
            CredentialCache credCache = new CredentialCache();
            credCache.Add(new Uri(jsonRequest), "NTLM", CredentialCache.DefaultNetworkCredentials);
            HttpWebRequest spRequest = (HttpWebRequest)HttpWebRequest.Create(jsonRequest);
            spRequest.Credentials = credCache;
            spRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+5.01;+Windows+NT+5.0";
            spRequest.Method = "GET";
            spRequest.Accept = "application/json;odata=verbose";
            HttpWebResponse endpointResponse = (HttpWebResponse)spRequest.GetResponse();

            if (endpointResponse.StatusCode == HttpStatusCode.OK)
            {
                using (endpointResponse)
                {
                    using (var reader = new StreamReader(endpointResponse.GetResponseStream()))
                    {
                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        try
                        {
                            string jSON = reader.ReadToEnd();
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
            }