Tuesday, July 30, 2013

Example project for MySQL with Entity Framework

Official exmaple:
http://dev.mysql.com/doc/refman/5.1/en/connector-net-tutorials-entity-framework-winform-data-source.html

Another one:
http://www.codeproject.com/Tips/426790/Using-MySQL-with-Entity-Framework

How to get Open ticket number for one queue in OTRS MySQL database by .Net ?

using MySql.Data.MySqlClient;
public class MySQLDBConnector
{
    private MySqlConnection connection;
    private string server;
    private string database;
    private string uid;
    private string password;

    //Constructor
    public MySQLDBConnector()
    {
        Initialize();
    }

    //Initialize values
    private void Initialize()
    {
        server = "10.0.x.x"; // "otrs.chatham.teksavvy.ca";
        database = "otrs";
        uid = "xxxx";
        password = "xxxx";
        string connectionString = "SERVER=" + server + ";Port=3306;" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

        connection = new MySqlConnection(connectionString);
    }

    //open connection to database
    public bool OpenConnection()
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (MySqlException ex)
        {
            switch (ex.Number)
            {
            }
            return false;
        }
    }

    public int Count()
    {
        string query = @"select count(*) from ticket inner join queue on ticket.queue_id= queue.id inner join ticket_state on ticket_state.id = ticket.ticket_state_id 
where queue.name ='Junk' and ticket.ticket_state_id = 4";
;
        int Count = -1;

        //Open Connection
        if (this.OpenConnection() == true)
        {
            //Create Mysql Command
            MySqlCommand cmd = new MySqlCommand(query, connection);

            //ExecuteScalar will return one value
            Count = int.Parse(cmd.ExecuteScalar() + "");

            //close Connection
            connection.Close();

            return Count;
        }
        else
        {
            return Count;
        }
    }
}

Monday, July 29, 2013

How to get Open ticket count for specic queue in OTRS by SQL query?

select count(*) from ticket
inner join queue on ticket.queue_id= queue.id
inner join ticket_state on ticket_state.id = ticket.ticket_state_id
where queue.name ='Postmaster' and ticket.ticket_state_id = 4

How to get table columns list from MySQL database?


SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tableName'

How to open 3306 port on Windows 2008 for MySQL?

In command line as administrator, run following command:
netsh advfirewall firewall add rule name="MySQL Server" action=allow protocol=TCP dir=in localport=3306

Reference:
http://www.activeservers.com/page3610203.aspx

Monday, July 22, 2013

How to use two views for one Action in ASP.NET MVC?

public ActionResult Process(id)
{
  if (Condition)
  {
    return View ('SpecialView')
  }
  else
  {
    return View (); //Default view 
  }
}

Friday, July 19, 2013

How to add a field to save current time in SQL Server?


In property window for that culumn:
set the "Default value or binding" getdate()

Thursday, July 18, 2013

How to add System.Web.Extensions reference in Console project?


In Visual Studio, project property page
Change Target Framework from Client profile to the full framework

How to make ASP.NET MVC action no caching?

        [OutputCache(Duration = 0, VaryByParam = "None")]
        public ActionResult ActionName()
        {
        }

15 jQuery Code Snippets for Developers

http://codegeekz.com/15-jquery-code-snippets-for-developers/

Wednesday, July 17, 2013

What is the event called for closing window in Javascript?

Onbeforeunload
Reference: https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

Wednesday, July 10, 2013

In Visual Studio, how to make post build event only happening in DEBUG mode?

Add a condition like:
if $(ConfigurationName) == Debug "$(TargetDir)i18n.PostBuild.exe" "$(ProjectDir)"

Formula.js

JavaScript implementation of most formula functions supported by Microsoft Excel 2013 and Google Spreadsheets


http://stoic.com/formula/

Monday, July 8, 2013

How to do server side validation for ASP.NET MVC Model?

Implement IValidatableObject interface inside model :

public System.Collections.Generic.IEnumerable Validate(ValidationContext validationContext)
 {
            if(true)
            {
                yield return new ValidationResult("error message", new string[] { "Validated Object Name" });                
            }
 }


http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.ivalidatableobject.validate.aspx

Friday, July 5, 2013

How to get RouteData in Application_BeginRequest or Application_EndRequest


RouteData routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
            if (routeData == null) return;
            string action= routeData.GetRequiredString("action");

Wednesday, July 3, 2013

How to make a CodeSnippet in Visual Studio for i18n?

If use i18n for ASP.NET MVC, need to add a lot of surroundings for strings.
A CodeSnippet could help a little bit:
In VS, Tools menu, click Code Snippets Manager. then import following xml file:


 
  
i18n i18n Code snippet for @_ Microsoft Corporation Expansion SurroundsWith
expression Exception type SimpleTypeName(global::System.Exception)