Archive

Archive for the ‘DEV’ Category

Tuple in .net 4.0

In .net 4.0 we have a new datatype… the Tuple… if you are familiar with software engineer, you know the idea behind it… to have a strongly typed datastructure that cannot be changed during the time…

// Instantiate using constructor 
Tuple<int, string, int> t1 = new Tuple<int, string, int>(4, "Stelio", 7);

// Instantiate using create method 
Tuple<int, int, int> t2 = Tuple.Create(2, 7, 9);
Categories: DEV Tags:

GeoLoaction in Javascript

I found this free service for GeoLocation in Javascript…

<script language='JavaScript' src='http://j.maxmind.com/app/geoip.js'></script>
<script language='JavaScript'>document.write(geoip_country_code());</script>
<script language='JavaScript'>
if(geoip_country_code()=="IT")
{
//Redirect ALL users from ITALY on google.com
location.replace("http://www.google.com");
}
</script>

the code is extremely easy to use, anyway I am looking forward for the proper GeoLocation API shipped with HTML5

Categories: DEV Tags: ,

how to script an FTP upload in a BAT file

In several projects you have the need to automate your deployment procedures… unfortunately often your code is hosted by an external provider that for security reasons give you the access only with FTP…

This code is perfect to automate the upload of your deployment package.

1) Create a Bat File with the following code and name it Upload.bat

@echo off
echo user FTPUserName> ftpcmd.dat
echo FTPPWD >> ftpcmd.dat
echo cd FolderName >>ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat FTPserverURL
del ftpcmd.dat

as you can understand you have to put into your username and password and eventually the code to switch directory if you are going to upload something on the root…

2) call your bat file with the following syntax: upload.bat testFile.zip

Happy scripting to everybody Smile

Categories: DEV, Infrastructure Tags: ,

Hello World with BackgroundWorker

in .Net 2.0 MS introduced the BackgroundWorker component to simplify the life of WinForm developers… you can check the following article to know more information http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

the simples sample of use of the BackgroundWorker is a WinForm application that count from 1 to 999 billion and every time that add 1 million, update a simple textBox on the form…

As you can see code is extremely simple… just remember to enable the WorkerReportProgress to true and use the UserState to pass values between events…

ps. this sample does not support user cancellation and does not handle more than 1 click…

public Form1()
       {
           InitializeComponent();
           backgroundWorker1.WorkerReportsProgress = true;
       }

       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           var bckwork = (BackgroundWorker)sender;
           for (Int64 i = 1; i <= 9999999999; i++)
           {
               if ((i % 1000000) == 0)
               {
                   int  prog =  (int)(i / 1000000) ;               
                   bckwork.ReportProgress( prog, i);
               }
           }
       }

       private void button1_Click(object sender, EventArgs e)
       {
           backgroundWorker1.RunWorkerAsync();
       }

       private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
       {
           textBox1.Text = e.UserState.ToString();
       }
Categories: DEV Tags: , ,

Source Control on Mercurial

There are several tools available for Mercurial…

On my last project we have decided to migrate to Mercurial from TFS and we are enjoying the following technologies stack:

Mercurial – Bitbucket– Jira – TeamCity

How can you do all of this???

Jira with the agile plugin is an excellent tool to run an Agile project, and you can integrate it fairly easily with Mercurial…

TeamCity can be also integrated fairly easily with Mercurial to keep under control build and deployment processes

Bitbucket is the connector to see your Mercurial repository “onLine”

as useful tools, I would also recommend TortoiseHG and VisualHG and Jira Connector for VS

these are few command to remember in the everyday life using mercurial…

How to switch Branch: HG update "default"

TFS CheckIn
hg push 

TFS get latest
hg pull 

after pull sometime hg up o hg update

hg merge to mix changes after a pull

how to download an existing repository from bitbucket
hg clone https://username@bitbucket.org/repCreator/projectName

TO KNOW ALL THE EXISTING BRANCHES
hg branches

Access to an external Sharepoint List with Sharepoint Client API

Do you need to access a Sharepoint List from a NonSharepoint Application???! With Sharepoint 2010 it is extremely easy… you can benefit of the Sharepoint Client API and it would be extremely easy to control resource hosted on SharePoint external list.
Obviously, whenever it is possible, I would suggest to cache the Sharepoint List in your application cache layer to avoid performance issues….

 public static ListItemCollection GetClientList(string listPath)
  {
  string url = ConfigurationManager.AppSettings["sharepointConfigSite"];
  using (Microsoft.SharePoint.Client.ClientContext ctx = new Microsoft.SharePoint.Client.ClientContext(url))
     {
        var list = ctx.Web.Lists.GetByTitle(listPath);
       ctx.Load(list);
       CamlQuery camlQuery = new CamlQuery();
       camlQuery.ViewXml = "<View/>";
       ListItemCollection listItems = list.GetItems(camlQuery);
        ctx.Load(listItems);
        if (ConfigurationManager.AppSettings["sharepointUserConfigSite"] != null)
          {
           string username = ConfigurationManager.AppSettings["sharepointUserConfigSite"];
           string pwd = ConfigurationManager.AppSettings["sharepointPwdConfigSite"];
            string domain = ConfigurationManager.AppSettings["sharepointDomConfigSite"];
            var cred = new System.Net.NetworkCredential(username, pwd, domain);
            ctx.Credentials = cred;
           }   else  {
                    ctx.AuthenticationMode = ClientAuthenticationMode.Anonymous;
                }
            ctx.ExecuteQuery();
            return listItems;
      }
 }
Categories: DEV, Infrastructure Tags: ,

Using SQL Table Parameters in ADO.NET

Some time is much better to use in your Store Procedures Table parameters for reduce the round-trip with the server and have better performances…. this is an example on how to do implement it.

SQL Store Procedure with a TABLE Parameter

   1: Create PROC [dbo].[getData]

   2:  

   3:  @MyTableParam ProductsTableType  READONLY ,

   4:  @InputId varchar (200)

   5:  

   6:  AS

   7:  

   8:       SELECT  *

   9:       FROM  table1 sub

  10:       

  11:       inner join @MyTableParam p on  sub.Productid = p.productId and sub.VariantId = p.VariantId

  12:       

  13:       INNER JOIN somethingelse ON sub.SubscriptionID = Subscription.ID

  14:         

  15:     where Field =@InputId

  16:       

and this is the code to call the storeProc

   1: private void AssignProductDataTableRow(DataTable products, CatalogItemsDataSet.CatalogItem catalogItem)

   2:        {

   3:            DataRow dr = products.NewRow();

   4:            dr[0] = catalogItem.ProductId;

   5:            dr[1] = catalogItem.VariantId;

   6:            products.Rows.Add(dr);

   7:        }

   8:  

   9:        public static DataTable PrepareProductsDataTable()

  10:        {

  11:            var products = new DataTable();

  12:  

  13:            var prIdCol = new DataColumn("ProductId", Type.GetType("System.String"));

  14:            var varIdCol = new DataColumn("VariantId", Type.GetType("System.String"));

  15:  

  16:            products.Columns.Add(prIdCol);

  17:            products.Columns.Add(varIdCol);

  18:            return products;

  19:        }

  20:  

  21:      using (

  22:                var conn =

  23:                    new SqlConnection(ConfigurationManager.ConnectionStrings["CustomProductData"].ToString()))

  24:            {

  25:                SqlCommand cmd = conn.CreateCommand();

  26:                cmd.CommandType = CommandType.StoredProcedure;

  27:                cmd.CommandText = "dbo.getData";

  28:  

  29:                cmd.Parameters.AddWithValue("@MyTableParam", products);

  30:                cmd.Parameters.AddWithValue("@InputId", "1");

  31:  

  32:                conn.Open();

  33:                SqlDataReader res = cmd.ExecuteReader();

  34:  

  35:  

  36:                while (res.Read())

  37:                {

  38:                 //Read Row...

  39:                 }

  40: }

Categories: DEV Tags: , ,

Using 7 Zip to compress a folder

"C:\Program Files (x86)\7-Zip\7z.exe" a c:\YourFile.zip c:\YourFolder\*
Categories: DEV, Infrastructure Tags: ,

SQL Tips

A few tips useful for your Sql Query…

Update with a join:

   1: UPDATE MyTable

   2: SET Column = NewValue

   3: FROM MyTable

   4: INNER JOIN OtherTable ON MyTable.Key = OtherTable.Key

trimming…

   1: select LTRIM(RTRIM(columnOne)) from table1

select into

   1: SELECT Persons.LastName,Orders.OrderNo

   2: INTO Backup

   3: FROM Persons

   4: INNER JOIN Orders

   5: ON Persons.P_Id=Orders.P_Id

Categories: DEV Tags:

Automapper

A cool tool for automatic mapping of classes specially useful to map entity and Models and so on…

Categories: DEV Tags: ,