Wednesday, November 25, 2009

Cool Sql Tip, Auto Inc On Update

DECLARE @counter int
SET @counter = 0
UPDATE #tmp_Users
SET @counter = counter = @counter + 1

Please read the rest of the cool article:
http://haacked.com/archive/2004/02/28/sql-auto-increment.aspx

Friday, October 30, 2009

Set a Property Value to Null given a tree of objects and the path

 

This code was developed by Snippet Compiler (once again).

These code could be used in other things. The original prupose was to set some properties to null, from a given path to the property as a string.

 

using System;
using System.Collections.Generic;
using System.Reflection;

public class Type1
{
    string _Value1 = "Value1";
    public string Value1
    {       
        get { return _Value1; }
        set {_Value1 = value; }
    }
    public string Dummy
    {
        get {return "dummy";}
    }
}

public class Type2
{
    protected string _Value2;
    public Type2(string v)
    {
        this._Value2 = v;
    }
    public string Value2
    {
        get { return _Value2; }
        set {  Console.WriteLine("someone is setting me to" + (value??"null"));  _Value2 = value; }
    }

}

public class Type3
{
    Type2[] v = new Type2[2]{ new Type2("Value221"), new Type2("Value222")  };
    public Type1 Value11
    {  get { return new Type1(); }}
    public Type2[] Value22
    {  get { return v; } set {v=value;} }
    public void PrintMe()
    {       
        object obj1 = this.GetType().InvokeMember("Value11", BindingFlags.GetProperty, null, this, new Object[0]);       
        System.Console.WriteLine(obj1.ToString());       
        object obj2 = this.GetType().InvokeMember("Value22", BindingFlags.GetProperty, null, this, new Object[0]);
        object[] objArray2 = (object[])obj2;
        foreach(object currentObj in objArray2)
        {
            System.Console.WriteLine(currentObj);
        }
    }
}

public class MyClass
{

    public static object SetPropertyToNull(object obj, string propertyName, string propertyValues, bool setFatherToNull)
        {
            if (obj == null)
            {
                return null;
            }
            int firstDotPropertyName = propertyName.IndexOf(".");
            string childPropertyName = String.Empty;
            string remainingPropertyName = String.Empty;

            //Get the property name no eval and the remaining string of the property name
            if (firstDotPropertyName < 0)
            {
                firstDotPropertyName = 0;
                childPropertyName = propertyName;
                remainingPropertyName = String.Empty;
            }
            else
            {
                int remainningPropertyLength = (propertyName.Length - firstDotPropertyName - 1);
                remainingPropertyName = propertyName.Substring(firstDotPropertyName + 1, remainningPropertyLength);
                childPropertyName = propertyName.Substring(0, firstDotPropertyName);
            }

            //Get the type
            Type initType = obj.GetType();
            //Get the child object
            object objChild = initType.InvokeMember(childPropertyName,
                BindingFlags.GetProperty, null, obj, new object[0]);

            //As default the object will not be changed
            object returnedObj = objChild;

            if (String.IsNullOrEmpty(remainingPropertyName))
            {
                //If the element is the rigth one, set it to null
                if (String.IsNullOrEmpty(propertyValues) || (propertyValues.IndexOf(String.Format(".{0}.", objChild))) >= 0)
                {
                    initType.InvokeMember(childPropertyName,
                        BindingFlags.SetProperty, null, obj, new Object[] { null });
                    if (setFatherToNull)
                    {
                        obj = null;
                    }
                }
            }
            else
            {
                //If dealing with an array, process all the elements
                if (objChild is Array)
                {

                    for (int arrayCount = 0; arrayCount < (objChild as object[]).Length; arrayCount++)
                    {
                        returnedObj = SetPropertyToNull((objChild as object[])[arrayCount], remainingPropertyName, propertyValues, setFatherToNull);
                        (objChild as object[])[arrayCount] = returnedObj;
                    }
                    returnedObj = objChild;
                }
                //otherwise process the remaining object tree
                else
                {
                    if (!String.IsNullOrEmpty(remainingPropertyName))
                    {
                        returnedObj = SetPropertyToNull(objChild, remainingPropertyName, propertyValues, setFatherToNull);
                    }
                }
                initType.InvokeMember(childPropertyName,
                            BindingFlags.SetProperty, null, obj, new Object[] { returnedObj });
            }
            return obj;
        }

    public static void Main()
    {
        Type3 obj = new Type3();
        WL("Before...");
        obj.PrintMe();
        SetPropertyToNull(obj, "Value22.Value2", null, true);
        WL("After");
        obj.PrintMe();   
        RL();
    }
    #region Helper methods

    private static void WL(object text, params object[] args)
    {
        Console.WriteLine(text.ToString(), args);   
    }
    private static void RL()
    {
        Console.ReadLine();   
    }
    private static void Break()
    {
        System.Diagnostics.Debugger.Break();
    }

    #endregion
}

Monday, October 12, 2009

Email in dev

 

Cool config to save the emails on a folder without the need for a smtp server:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="c:\temp\maildrop\"/>
      </smtp>
    </mailSettings>
  </system.net>

 

 

Check the full post:

http://blog.donnfelker.com/post/Sending-Email-in-a-Development-Environment-without-an-SMTP-Server.aspx

Thursday, August 13, 2009

Ajax Culture Sys.CultureInfo (Ajax em Português)

 

How to change the ajax culture to a new one?

1. Save a copy of MicrosoftAjax.js ( you can find it on System.Web.Extensions thru Reflector tool)

2. Put it in the web server, in my case “/scripts”

3. Duplicate the file and rename it to MicrosoftAjax.PT-pt.js

4. Edit the file MicrosoftAjax.PT-pt.js and do some replace of strings (ex: month names)

5. Edit the ScriptManager (The value “PT-pt” from the ResourcesUICultures attribute is used to get the MicrosoftAjax.PT-pt.js)

<asp:ScriptManager ID="ScriptManager" runat="server" EnableScriptLocalization="true" EnableScriptGlobalization="true">
<Scripts>
    <asp:ScriptReference Name="MicrosoftAjax.js" Path="~/Scripts/MicrosoftAjax.js" ResourceUICultures="PT-pt" />
</Scripts>
</asp:ScriptManager>

 

Ref: http://msdn.microsoft.com/en-us/magazine/cc135974.aspx

and http://msdn.microsoft.com/pt-br/magazine/cc135974.aspx

You are done.

Wednesday, August 12, 2009

Use Custom Objects in Project Settings

 

1.Add a new setting, set the name and one of displayed types.

2.Inside your VS.Net open the Settings with the Xml Editor.

3.Locate your new setting:

<Setting Name="ReplaceSomething" Type="System.String" Scope="Application">
        <Value Profile="(Default)">            
        </Value>
    </Setting>

 

4.Edit the Type attribute in my case, i changed to:

<Setting Name="ReplaceSomething" Type="RegExHttpModule.RegExReplaceRule" Scope="Application">
        <Value Profile="(Default)">            
        </Value>
    </Setting>

5.And now place the Xml Serialization version of the object:

<Setting Name="ReplaceSomething" Type="RegExHttpModule.RegExReplaceRule" Scope="Application">
        <Value Profile="(Default)">           
            &lt;RegExReplaceRule xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
            &lt;UrlRegularEx&gt;1&lt;/UrlRegularEx&gt;
            &lt;FileContentRegularEx&gt;2&lt;/FileContentRegularEx&gt;
            &lt;FileContentReplace&gt;3&lt;/FileContentReplace&gt;
            &lt;/RegExReplaceRule&gt;
        </Value>
    </Setting>

6.Now you can close de Xml Editor and click over the Settings file.

Disable multiple postback

How to disable multiple postbacks from a asp.net page (  the easy way, not the best):

1. Use asp.net Button control, check for OnClick and OnClientClick Property

2.OnClientClick you should add a call to the function DisableButtonOnPostBack:

<asp:Button ID="Button1" runat="server" OnClientClick="return Navigate();" onclick="Button1_Click" Text="Button" />

3.And then add the javascript.

<script type="text/javascript">

function DisableButtonOnPostBack()  {

if (!this.disabled) {
              this.disabled = true;
              return true;
          }
          else {
              return false;
          }

}

</script>

Friday, June 26, 2009

Free UnZip And Run Mdx Query Tool (Tested on Sql Server Analysis Services 2005)

Are you looking for a nice mdx qyery tool:

http://www.mosha.com/msolap/mdxstudio.htm

 

You don’t need to install it, just unzip and run it.

I tested it on SQL Server Analysis Services 2005, and worked.

Tuesday, May 19, 2009

Manage your Windows Services

Check out the sc.exe from the command line.

Tuesday, May 12, 2009

MSDTC TCP/IP Ports

http://support.microsoft.com/kb/250367/en-us

Monday, May 04, 2009

Dll not found. Reflection.

Hi,

If you have a error something like this one:

File Not Found. at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.Load(String assemblyString) at System.Web.Configuration.CompilationSection.LoadAssembly(String assemblyName, Boolean throwOnFail) at System.Web.UI.TemplateParser.LoadAssembly(String assemblyName, Boolean throwOnFail) at System.Web.UI.TemplateParser.AddAssemblyDependency(String assemblyName, Boolean addDependentAssemblies) at System.Web.UI.TemplateParser.ProcessDirective(String directiveName, IDictionary directive) at System.Web.UI.BaseTemplateParser.ProcessDirective(String directiveName, IDictionary directive) at System.Web.UI.TemplateControlParser.ProcessDirective(String directiveName, IDictionary directive) at System.Web.UI.PageParser.ProcessDirective(String directiveName, IDictionary directive) at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding)

Try this:
From the Vs.net command ptompt, run the tool Fuslogvw.exe.
On settings select the "log bind failures to disk" option, and choose a local path for the log, and click ok.
Run you app, and after the error, select the refresh button, the dll missing should appear. If you select it, with double click you sould have some info to help you.
WARNING: After you are done, disable the log.
Thanks Mister Daniel for this info.
Cheers.

Tuesday, March 03, 2009

Configure the S.O. proxy settings to all users

Use proxycfg to display the current proxy setting.

Use the option “proxycfg –d”, to disable the proxy setting.

Use the option “proxycfg -u” to copy the proxy settings from the Internet Explorer user current settings

Search strings in files, the good old way

 

FINDSTR

 

The Command Help (FINDSTR /?)

Utilize espaços para separar múltiplas cadeias de procura, excepto se o
argumento incluir o prefixo /C. Por exemplo, 'FINDSTR "quem está" x.y'
procura "quem" ou "está" no ficheiro x.y.  'FINDSTR /C:"quem está" x.y'
procura "quem está" no ficheiro x.y.

 

Online Help

http://technet.microsoft.com/en-us/library/bb490907.aspx