Archive for Settembre, 2008

DeepCode

CodeKeep C# Feed Settembre 30th, 2008

Description: Use to deep clone an object

Link: http://www.codekeep.net/snippets/bef5e8e3-afab-4a9f-ab8f-80084ff5a672.aspx

public class MyObject  : ICloneable {
    public object Clone()
    {
        return ObjectUtility.CloneObject(this);
    }

}
public static class ObjectUtility {
	public static object CloneObject(object obj)
	{
   	 using (MemoryStream memStream = new MemoryStream())
   	 {
   	     BinaryFormatter binaryFormatter = new BinaryFormatter(null,
   	          new StreamingContext(StreamingContextStates.Clone));
   	     binaryFormatter.Serialize(memStream, obj);
  	      memStream.Seek(0, SeekOrigin.Begin);
     	   return binaryFormatter.Deserialize(memStream);
	    }
}
}

SqlCommandBuilder

CodeKeep C# Feed Settembre 30th, 2008

Description: SqlCommandBuilder object to automatically generate the DeleteCommand, the InsertCommand, and the UpdateCommand properties of the SqlCommand object for a SqlDataAdapter object,

Link: http://www.codekeep.net/snippets/9a6c1dd3-76d0-4490-8fb4-3b8d149d91aa.aspx

using System.Data;
using System.Data.SqlClient;
using System;
namespace Q308507 {

  class Class1 {
     static void Main(string[] args)	{

        SqlConnection cn = new SqlConnection();
        DataSet CustomersDataSet = new DataSet();
        SqlDataAdapter da;
        SqlCommandBuilder cmdBuilder;

        //Set the connection string of the SqlConnection object to connect
        //to the SQL Server database in which you created the sample
        //table.
        cn.ConnectionString = "Server=server;Database=northwind;UID=login;PWD=password;";

        cn.Open();      

        //Initialize the SqlDataAdapter object by specifying a Select command
        //that retrieves data from the sample table.
        da = new SqlDataAdapter("select * from CustTest order by CustId", cn);

        //Initialize the SqlCommandBuilder object to automatically generate and initialize
        //the UpdateCommand, InsertCommand, and DeleteCommand properties of the SqlDataAdapter.
        cmdBuilder = new SqlCommandBuilder(da);

        //Populate the DataSet by running the Fill method of the SqlDataAdapter.
        da.Fill(CustomersDataSet, "Customers");

        //Display the Update, Insert, and Delete commands that were automatically generated
        //by the SqlCommandBuilder object.
        Console.WriteLine("Update command Generated by the Command Builder : ");
        Console.WriteLine("==================================================");
        Console.WriteLine(cmdBuilder.GetUpdateCommand().CommandText);
        Console.WriteLine("         ");

        Console.WriteLine("Insert command Generated by the Command Builder : ");
        Console.WriteLine("==================================================");
        Console.WriteLine(cmdBuilder.GetInsertCommand().CommandText);
        Console.WriteLine("         ");        

        Console.WriteLine("Delete command Generated by the Command Builder : ");
        Console.WriteLine("==================================================");
        Console.WriteLine(cmdBuilder.GetDeleteCommand().CommandText);
	Console.WriteLine("         ");

        //Write out the value in the CustName field before updating the data using the DataSet.
        Console.WriteLine("Customer Name before Update : " + CustomersDataSet.Tables["Customers"].Rows[0]["CustName"]);

        //Modify the value of the CustName field.
        CustomersDataSet.Tables["Customers"].Rows[0]["CustName"] = "Jack";

        //Post the data modification to the database.
        da.Update(CustomersDataSet, "Customers");        

        Console.WriteLine("Customer Name updated successfully");

        //Close the database connection.
        cn.Close();

        //Pause
        Console.ReadLine();
      }
   }

}

SqlCommandBuilder

CodeKeep C# Feed Settembre 30th, 2008

Description: SqlCommandBuilder object to automatically generate the DeleteCommand, the InsertCommand, and the UpdateCommand properties of the SqlCommand object for a SqlDataAdapter object,

Link: http://www.codekeep.net/snippets/a81002c8-495b-429f-b424-996322a7a05b.aspx

using System.Data;
using System.Data.SqlClient;
using System;
namespace Q308507 {

  class Class1 {
     static void Main(string[] args)	{

        SqlConnection cn = new SqlConnection();
        DataSet CustomersDataSet = new DataSet();
        SqlDataAdapter da;
        SqlCommandBuilder cmdBuilder;

        //Set the connection string of the SqlConnection object to connect
        //to the SQL Server database in which you created the sample
        //table.
        cn.ConnectionString = "Server=server;Database=northwind;UID=login;PWD=password;";

        cn.Open();      

        //Initialize the SqlDataAdapter object by specifying a Select command
        //that retrieves data from the sample table.
        da = new SqlDataAdapter("select * from CustTest order by CustId", cn);

        //Initialize the SqlCommandBuilder object to automatically generate and initialize
        //the UpdateCommand, InsertCommand, and DeleteCommand properties of the SqlDataAdapter.
        cmdBuilder = new SqlCommandBuilder(da);

        //Populate the DataSet by running the Fill method of the SqlDataAdapter.
        da.Fill(CustomersDataSet, "Customers");

        //Display the Update, Insert, and Delete commands that were automatically generated
        //by the SqlCommandBuilder object.
        Console.WriteLine("Update command Generated by the Command Builder : ");
        Console.WriteLine("==================================================");
        Console.WriteLine(cmdBuilder.GetUpdateCommand().CommandText);
        Console.WriteLine("         ");

        Console.WriteLine("Insert command Generated by the Command Builder : ");
        Console.WriteLine("==================================================");
        Console.WriteLine(cmdBuilder.GetInsertCommand().CommandText);
        Console.WriteLine("         ");        

        Console.WriteLine("Delete command Generated by the Command Builder : ");
        Console.WriteLine("==================================================");
        Console.WriteLine(cmdBuilder.GetDeleteCommand().CommandText);
	Console.WriteLine("         ");

        //Write out the value in the CustName field before updating the data using the DataSet.
        Console.WriteLine("Customer Name before Update : " + CustomersDataSet.Tables["Customers"].Rows[0]["CustName"]);

        //Modify the value of the CustName field.
        CustomersDataSet.Tables["Customers"].Rows[0]["CustName"] = "Jack";

        //Post the data modification to the database.
        da.Update(CustomersDataSet, "Customers");        

        Console.WriteLine("Customer Name updated successfully");

        //Close the database connection.
        cn.Close();

        //Pause
        Console.ReadLine();
      }
   }

}

start exe

CodeKeep C# Feed Settembre 30th, 2008

Description: start exe

Link: http://www.codekeep.net/snippets/9f355e19-1955-4874-87c7-656963978355.aspx

            Process notePad = new Process();

            notePad.StartInfo.FileName   = "notepad.exe";
            notePad.StartInfo.Arguments = "ProcessStart.cs";

            notePad.Start();

open a document

CodeKeep C# Feed Settembre 29th, 2008

Description: open a document

Link: http://www.codekeep.net/snippets/1af1459d-0491-4b33-82b0-043e26fdb879.aspx

    System.Diagnostics.Process.Start(fileName);

StringBuilder instantiation

CodeKeep C# Feed Settembre 29th, 2008

Description: Instantiates a StringBuilder and returns the result

Link: http://www.codekeep.net/snippets/d0a474c9-6536-4579-9c76-9bde52606162.aspx

StringBuilder sb = new StringBuilder();

return sb.ToString();

The Prototype JavaScript Framework

WebReference News Settembre 29th, 2008

Programmers tend to collect utility functions to perform common tasks. In this article we look at some of the key features of the Prototype JavaScript Framework and demonstrate how it can make your life easier. By Rob Gravelle. 0925

How to Get Window NT Logged User Name Using ASP.NET

CodeKeep C# Feed Settembre 29th, 2008

Description:
How to Get Window NT Logged User Name Using ASP.NET

Link: http://www.codekeep.net/snippets/24ed5597-f3d1-4022-a9ca-af4a20f5eac2.aspx

1) System.Security.Principal.WindowsPrincipal p = System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;

string strName = p.Identity.Name;

[ OR ]

2) string strName = HttpContext.Current.User.Identity.Name.ToString();

[ OR ]

3) string strName = Request.ServerVariables["AUTH_USER"]; //Finding with name

string strName = Request.ServerVariables[5]; //Finding with index

Get current windows user - username

CodeKeep C# Feed Settembre 29th, 2008

Description: Get current windows user - username

Link: http://www.codekeep.net/snippets/47a962c3-eaa1-419f-b2be-7298b69947be.aspx

using System.Security.Principal;

namespace ConsoleApplicationWindowsUser
{
    class Program
    {
        static void Main(string[] args)
        {
            string a;
            a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

            Console.WriteLine(a);
            Console.Read();
        }
    }
}

Tokenizer with delimeters

CodeKeep C# Feed Settembre 28th, 2008

Description: tokenizer with delimeters

Link: http://www.codekeep.net/snippets/cb1142ef-cf37-4f95-b328-7bea2723ac9a.aspx

using System;
using System.Text.RegularExpressions;

public static string[] Tokenize(string equation)
{
   Regex RE = new Regex(@"([\+\-\*\(\)\^\\])");
   return (RE.Split(equation));
}

public void TestTokenize( )
{
   foreach(string token in Tokenize("(y - 3)(3111*x^21 + x + 320)"))
      Console.WriteLine("String token = " + token.Trim( ));
}

Next »