Thursday, May 31, 2012

How to create sub domain and upload with files using C# (Microsoft.Web.Administration)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Administration;
using Microsoft.Web.Administration;
using System.IO;


public partial class _Default : System.Web.UI.Page
{

    private const string SERVER_IP = "192.168.111.112";
  private const int PORT = 80;
  private const string WEB_DOMAIN_PATH = @"F:\\web\domains\{0}\";

    //Live server
    //private const string SERVER_IP = "192.168.111.111";

    protected void Page_Load(object sender, EventArgs e)
    {
   if (!string.IsNullOrEmpty(Request.QueryString["user"]))
   {

    try
    {
     string username = Request.QueryString["user"];
     string status = CreateUserSite(username, "abcdomain.my");

     Response.Write(status);
    }
    catch(Exception ex)
    {
     Response.Write(ex.Message);
    }
   }
   else
   {
    Response.Write("user parameter not supplied");
   }

  
    }


    private string CreateUserSite(string user, string domain)
    {


   string path = string.Format(WEB_DOMAIN_PATH, domain);

        string userpath = path + user;

        string userUrl = user + "." + domain;

        using (ServerManager serverManager = new ServerManager())
        {

            bool siteExists = false;
            int number = serverManager.Sites.Where(p => p.Name.ToLower().Equals(userUrl.ToLower())).Count();

            if (number == 0)
            {
                siteExists = false;
            }
            else
            {
                siteExists = true;
            }

            if (!siteExists)
            {

                //create user directory
                Directory.CreateDirectory(userpath);

                //copy every files from a-base to a new created folder
                FileInfo[] d = new DirectoryInfo(path + @"\a-base").GetFiles();
                foreach (FileInfo fi in d)
                {
                    File.Copy(fi.FullName, userpath + @"\" + fi.Name, true);
                }

                //create a directory
                Directory.CreateDirectory(userpath + @"\swfobject");

                FileInfo[] d1 = new DirectoryInfo(path + @"\a-base\swfobject").GetFiles();
                foreach (FileInfo fi in d1)
                {
                    File.Copy(fi.FullName, userpath + @"\swfobject\" + fi.Name, true);
                }



                //create site
                Site mySite = serverManager.Sites.Add(userUrl, path + user, PORT);
                mySite.ServerAutoStart = true;
                mySite.Applications[0].ApplicationPoolName = domain;

                //create bindings
                mySite.Bindings.Clear();
                mySite.Bindings.Add(string.Format("{0}:{2}:{1}", SERVER_IP, userUrl, PORT ), "http");
                mySite.Bindings.Add(string.Format("{0}:{2}:www.{1}", SERVER_IP, userUrl, PORT), "http");


                Configuration config = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection httpLoggingSection = config.GetSection("system.webServer/httpLogging", userUrl);
                httpLoggingSection["dontLog"] = true;

                serverManager.CommitChanges();

              //  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "success", "alert('" + userUrl + " created');", true);

            }
            else
            {
                //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "alert('user exists. Please use other name');", true);
       throw new Exception("user exists. Please use other name");
      }


      return userUrl + " has been successfully created";
        }
    }
}

Thursday, April 26, 2012

Wednesday, August 24, 2011

get database columns using mssql server

SELECT   schemas.name AS [Schema],
         tables.name AS [Table],
         columns.name AS [Column],
         CASE 
             WHEN columns.system_type_id = 34    THEN 'byte[]'
             WHEN columns.system_type_id = 35    THEN 'string'
             WHEN columns.system_type_id = 36    THEN 'System.Guid'
             WHEN columns.system_type_id = 48    THEN 'byte'
             WHEN columns.system_type_id = 52    THEN 'short'
             WHEN columns.system_type_id = 56    THEN 'int'
             WHEN columns.system_type_id = 58    THEN 'System.DateTime'
             WHEN columns.system_type_id = 59    THEN 'float'
             WHEN columns.system_type_id = 60    THEN 'decimal'
             WHEN columns.system_type_id = 61    THEN 'System.DateTime'
             WHEN columns.system_type_id = 62    THEN 'double'
             WHEN columns.system_type_id = 98    THEN 'object'
             WHEN columns.system_type_id = 99    THEN 'string'
             WHEN columns.system_type_id = 104   THEN 'bool'
             WHEN columns.system_type_id = 106   THEN 'decimal'
             WHEN columns.system_type_id = 108   THEN 'decimal'
             WHEN columns.system_type_id = 122   THEN 'decimal'
             WHEN columns.system_type_id = 127   THEN 'long'
             WHEN columns.system_type_id = 165   THEN 'byte[]'
             WHEN columns.system_type_id = 167   THEN 'string'
             WHEN columns.system_type_id = 173   THEN 'byte[]'
             WHEN columns.system_type_id = 175   THEN 'string'
             WHEN columns.system_type_id = 189   THEN 'long'
             WHEN columns.system_type_id = 231   THEN 'string'
             WHEN columns.system_type_id = 239   THEN 'string'
             WHEN columns.system_type_id = 241   THEN 'string'
             WHEN columns.system_type_id = 241   THEN 'string'
         END AS [Type],
         columns.is_nullable AS [Nullable]


FROM              sys.tables tables
    INNER JOIN    sys.schemas schemas ON (tables.schema_id = schemas.schema_id )
    INNER JOIN    sys.columns columns ON (columns.object_id = tables.object_id) 
 

WHERE     tables.name <> 'sysdiagrams' 
    AND   tables.name <> 'dtproperties' 

ORDER BY [Schema], [Table], [Column], [Type]

Wednesday, February 23, 2011

how to generate json file from .net object

    public class data_city
    {
        private string _city;

        public string city
        {
            get { return _city; }
        }

        public data_city(string city)
        {
            _city = city;
        }

    }


    public class data_state
    {
        public data_state(string id, string name)
        {
            _id = id;
            _name = name;
        }

        private string _id;

        public string id
        {
            get { return _id; }
        }
        private string _name;

        public string name
        {
            get { return _name; }
        }

        private List<data_city> _Cities = new List<data_city>();

        public List<data_city> cities
        {
            get { return _Cities; }
            set { _Cities = value; }
        }

    }


    private void GenerateJsonCities()
    {

        var state = from s in developerDataContext.States
                                orderby s.DisplayOrder
                                select new { id = s.StateCode, name = s.Description };



        List<data_state> dsl = new List<data_state>(); 
        foreach (var s in state)
        {
            data_state ds = new data_state(s.id, s.name);


            List<City> ss  = developerDataContext.Cities.Where(p => p.StateCode == s.id).OrderBy(p => p.City1).ToList();

            foreach (City c in ss)
            {
                ds.cities.Add(new data_city(c.City1));
            }

            dsl.Add(ds);

        }
       
           

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(dsl);

        string city_json = string.Format("var data_state = {0};", json);

        File.WriteAllText(MapPath("~/scripts/referenceData/state_city.js"), city_json);

       

    }

Tuesday, April 6, 2010

how to get optimum sizes of resized image with proportion in .NET


Private _MaxWidth As Integer
Private _MaxHeight As Integer

Public Property MaxWidth() As Integer
Get
Return _MaxWidth
End Get
Set(ByVal value As Integer)
_MaxWidth = value
End Set
End Property

Public Property MaxHeight() As Integer
Get
Return _MaxHeight
End Get
Set(ByVal value As Integer)
_MaxHeight = value
End Set
End Property



Private Function CalculateOptimumImageSize(ByVal src As System.Drawing.Image) As Integer()
Dim sizeImgDim As Integer() = New Integer(1) {}

sizeImgDim(0) = src.Width
sizeImgDim(1) = src.Height

'if the max and min are larger than the original size, therefore follow the original
If sizeImgDim(0) > _MaxWidth OrElse sizeImgDim(1) > _MaxHeight Then
Dim imageRatio As Decimal = CDec(sizeImgDim(0)) / CDec(sizeImgDim(1))
Dim w As Decimal = imageRatio * CDec(_MaxHeight)
If w > _MaxWidth Then

sizeImgDim(0) = _MaxWidth
Else
sizeImgDim(0) = Convert.ToInt32(w)
End If


Dim h As Decimal = CDec(_MaxWidth) / imageRatio
If h > _MaxHeight Then

sizeImgDim(1) = _MaxHeight
Else

sizeImgDim(1) = Convert.ToInt32(h)

End If
End If
Return sizeImgDim
End Function

Clean up html code and words tag in .NET

Private Shared Function CleanHTML(ByVal html As String) As String
html = Regex.Replace(html, "<[/]?(font|span|xml|del|ins|[ovwxp]:\w+)[^>]*?>", "", RegexOptions.IgnoreCase)
html = Regex.Replace(html, "<([^>]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>", "<$1$2>", RegexOptions.IgnoreCase)
html = Regex.Replace(html, "<([^>]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>", "<$1$2>", RegexOptions.IgnoreCase)
Return html
End Function

How to capitalize every first letter using .NET

Public Shared Function Capitalize(ByVal value As String) As String
Return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value)
End Function