Search This Blog

Tuesday, 14 October 2014

How can I make window.showmodaldialog work in chrome 37 and above

I am adding here a workaround for window.showmodaldialog, although this is not a perfect workaround because this will not break the code execution as showmodaldialog was doing, instead this will open the popups.


Just put the below code in head section of page.


window.showModalDialog = function (url, arg, feature) {
        var opFeature = feature.split(";");
       var featuresArray = new Array()
        if (document.all) {
           for (var i = 0; i < opFeature.length - 1; i++) {
                var f = opFeature[i].split("=");
               featuresArray[f[0]] = f[1];
            }
       }
        else {

            for (var i = 0; i < opFeature.length - 1; i++) {
                var f = opFeature[i].split(":");
               featuresArray[f[0].toString().trim().toLowerCase()] = f[1].toString().trim();
            }
       }



       var h = "200px", w = "400px", l = "100px", t = "100px", r = "yes", c = "yes", s = "no";
       if (featuresArray["dialogheight"]) h = featuresArray["dialogheight"];
        if (featuresArray["dialogwidth"]) w = featuresArray["dialogwidth"];
       if (featuresArray["dialogleft"]) l = featuresArray["dialogleft"];
        if (featuresArray["dialogtop"]) t = featuresArray["dialogtop"];
        if (featuresArray["resizable"]) r = featuresArray["resizable"];
       if (featuresArray["center"]) c = featuresArray["center"];
      if (featuresArray["status"]) s = featuresArray["status"];
        var modelFeature = "height = " + h + ",width = " + w + ",left=" + l + ",top=" + t + ",model=yes,alwaysRaised=yes" + ",resizable= " + r + ",celter=" + c + ",status=" + s;

        var model = window.open(url, "", modelFeature, null);

       model.dialogArguments = arg;

    }

Saturday, 14 December 2013

How to zip or unzip large Dataset C#

Some time we need to zip large DataSet to transfer over the http or any other system.

Following are the full code block to zip and unzip DataSet.

To use this code you need icsharpcode library, you can download it here.

Required Namespace:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.GZip;

Method to zip DataSet:   

public byte[] ZipDataset(DataSet data)
        {
            MemoryStream mem = new MemoryStream();
            GZipOutputStream stream = new GZipOutputStream(mem);
            byte[] buffer;
 
            try
            {
                data.WriteXml(stream, XmlWriteMode.WriteSchema);
                stream.Finish();
                mem.Seek(0, SeekOrigin.Begin);
                buffer = mem.ToArray();
                return buffer;
            }
            catch
            {
                return null;
            }
            finally
            {
                mem.Close();
                mem.Dispose();
                stream.Close();
                stream.Dispose();
            } 
        }

Method to unzip DataSet:

public DataSet UnzipDataset(byte[] buf)
        {
            DataSet data = new DataSet();
            MemoryStream zStream = null;
            MemoryStream stream = new MemoryStream();
            Stream outStream = null;
            int num;
            int count = 0x8000;
 
            try
            {
                zStream = new MemoryStream(buf);
                stream = new MemoryStream();
                outStream = new GZipInputStream(zStream);
 
                byte[] output = new byte[count];
                while ((num = outStream.Read(output, 0, count)) > 0)
                {
                    stream.Write(output, 0, num);
                }
                stream.Seek(0L, SeekOrigin.Begin);
                data.ReadXml(stream, XmlReadMode.ReadSchema);
                return data;
            }
            catch
            {
                return null;
            }
            finally
            {
                stream.Close();
                stream.Dispose();
                outStream.Dispose();
                outStream.Close();
                zStream.Close();
                zStream.Dispose();
            }
        } 

Download the reference library       
        

Saturday, 30 November 2013

Create , Update and Get the data of XML file.

Methods to create, update and get the data from XML file.

Introduction:
Some time we need to save some temporary data to in XML file, this post will give you some best methods to create , update and get the data.
You can download the full code here
Required Namespace:

using System.Xml;
using System.IO;

Method to create XML File

private static readonly string fileName = "{0}Xfile.xml"; //Define the name of XML file

public static void CreateXmlFile()
{
           
    try
       {
          string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
          string strConfigFile = string.Format(fileName, path + "\\");
           if (!File.Exists(strConfigFile))
            {
               FileStream fs = new FileStream(strConfigFile, FileMode.Create);

                XmlWriter w = XmlWriter.Create(fs);

                    w.WriteStartDocument();
                    w.WriteStartElement("DATA");

                  
                    w.WriteEndDocument();
                    w.Flush();
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
              
            }
          

        }
  
Method to update data to XML File
  
public static void UpdateXml(string key, string value)
        {
          
            try
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //Define the path to save the XML file
                string strDataFile = string.Format(fileName, path + "\\");
                XmlDocument dom = new XmlDocument();
                dom.Load(strDataFile);
                Boolean keyFind = false;
                XmlNodeList nodeslist = dom.SelectNodes("/DATA/Add");
                foreach (XmlNode xn in nodeslist)
                {
                    string keyInner = xn["key"].InnerText;

                    if (keyInner == key)
                    {
                        keyFind = true;
                        xn["value"].InnerText = value;
                        break;
                    }
                }
                if (!keyFind)
                {
                    XmlNode rootNode = dom.SelectSingleNode("/DATA");
                    XmlNode newNode = rootNode.AppendChild(dom.CreateNode(XmlNodeType.Element,
                                                               "Add",
                                        dom.DocumentElement.NamespaceURI));
                    XmlNode keyNode = newNode.AppendChild(dom.CreateNode(XmlNodeType.Element, 
                                                               "key",
                                        dom.DocumentElement.NamespaceURI));
                    keyNode.InnerText = key;
                    XmlNode valNode = newNode.AppendChild(dom.CreateNode(XmlNodeType.Element, 
                                                              "value",
                                         dom.DocumentElement.NamespaceURI));
                    valNode.InnerText = value;
                }
                dom.Save(strDataFile);
            }
            catch (Exception ex)
            {
              
            }
          

        }



 Method to Get data from XML File


     public static string GetValuefromKey(string key)
        {
          
            string val = "";
            try
            {
                string path = 
                         Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                string strConfigFile = string.Format(fileName, path + "\\");
                XmlDocument dom = new XmlDocument();
                dom.Load(strConfigFile);

                XmlNodeList nodeslist = dom.SelectNodes("/DATA/Add");
                foreach (XmlNode xn in nodeslist)
                {
                    string keyInner = xn["key"].InnerText;

                    if (keyInner == key)
                    {
                        val = xn["value"].InnerText;

                    }
                }
            }
            catch (Exception ex)
            {
              
            }
           
            return (val); ;

        }