Enums

If you find yourself passing around a few specific strings or integers with particular meanings, than enums are for you.

In C#, you can declare them like so :

    /// <summary>
    /// Status of survey to particular user
    /// </summary>
    public enum Completion_Status
    {
        /// <summary>
        /// User completed survey already
        /// </summary>
        Closed,
        /// <summary>
        /// No record in database for this user, they should do it
        /// </summary>
        Open
    }

Then, in your code, you reference them like this :

Completion_Status.Open

This is a great way to better your code in terms of comprehensibility. Consider your new enum switch statement :

        String message = "";
        switch (thisSurveyStatus)
        {
            case Completion_Status.Open:
                message = "Please complete your survey";
                break;
            case Completion_Status.Closed:
                message  = "You have already completed this survey.";
                break;

        }

ASP.NET Repeater DataBind Custom Object

Suppose we wish to databind a List:

        //Or any kind of list...
        List ints = new List() { 1, 2, 3, 10, 19 };
        Repeater1.DataSource = ints;
        Repeater1.DataBind();
        <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                        <%# Container.DataItem %>
                </ItemTemplate>
        </asp:Repeater>

Or suppose we have a custom object:

        public class myFooBarCollection{
                public class FooBar{
                        public int myFooBarNumber = 0;
                        public String myFoorBarText = "";
                }

                public int somefield;
                public List foobarcollection = new List();
        }
        //...
        protected void Page_Load(object sender, EventArgs e){
                Repeater1.DataSource = (new myFooBarCollection()).foobarcollection;
                Repeater1.DataBind();
        }
        <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                        Index: <%# Container.ItemIndex %>
                        Item: <%# ((myFooBarCollection.FooBar)Container.DataItem).myFooBarText %>
                </ItemTemplate>
        </asp:Repeater>

Databinding to Custom Objects
Not much has changed. Note that in this example, myCustomObjectCollection is a collection of a class that has fields theTextFieldName and theValueFieldName that have get and set methods (ie, public int theValueFieldName {get; set;}).

myASPRadioButtonList.DataSource = myCustomObjectCollection;
myASPRadioButtonList.DataTextField = "theTextFieldName";
myASPRadioButtonList.DataValueField = "theValueFieldName";
myASPRadioButtonList.DataBind();

C# Variable Precedence Perversion

public class myclass{
	public int myint;
	public void aMethod(int myint){
		myint = myint; //set global variable to arg
	}
	public void aSecondMethod(){
		int myint = 9;
		myint=myint; //set global variable to local variable
	}
}

Looks correct, no?
Actually, it is not. Locals have a higher precedence in C# than globals. IE, aMethod and aSecondMethod will not change your global variable; they simply set your local variable to itself.

Quick C# Tricks

Declare a Generic list all in one line

using System.Collections.Generic;
List<string> mylist = new List<string>(3) { "this", "that", "other"};

Use of ? Operator
Quick and clean if, then equals, else equals. Here, a boolean bit from our SqlDataSource binding is being converted to a more human intuitive indicator.

<asp:Label ID="uxActiveLbl" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ClientActive").ToString() == "1" ? "Yes" : "No"%>' />
String emailID = (Session["UserEmailID"] == null ? "" : Session["UserEmailID"].ToString());

C# Inline ASP.NET Code

<html>
<head>
<script type="C#">
String myvariable = "my new variable";
protected void Page_Load(object sender, EventArgs e)
    {
        //do stuff
    }
</script>
</head>
<body>
Executed Code..
<% String myvariable="foobar" %>
<br /><br />
Rendered Code...
<%=myvariable%>
<%-- server side comments can be useful for commenting out asp tags <asp:Label id="lbl1" runat="server" text="hello" /> --%>
</body>
</html>

Better use of SqlConnection
Using finally to close a sql connection, even if an exception occurs…

        SqlConnection con = new SqlConnection("...");
        SqlCommand cmd = new SqlCommand("...", con);
        try
        {
            con.Open();
            //...
        }
        catch (Exception ex) 
        { 
           throw ex; 
        }
        finally
        {
            cmd.Dispose();
            con.Close();
        }

Even Better Use of SQLDataConnection
This makes use of the object inside of using and lets it use its own close/dispose/whatever methods, instead of DieYouGravySuckingPigDog();

    public static void myMethod(int arg1)
    {
        using (SqlConnection con = new SqlConnection("connectionString"))
        {
            using (SqlCommand cmd = new SqlCommand("mySproc", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@mySprocArg", arg1);
                con.Open();
                cmd.ExecuteNonQuery();
            } //end using command
        } //end using connection
    }

Two Column Repeater Layout

<asp:Repeater ID="uxRepeater" runat="server">
<HeaderTemplate>
<table><tr><td>ID</td><td>Data</td><td style="width:30px;">&nbsp;</td><td>ID</td><td>Data</td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr><td><%# DataBinder.Eval(Container.DataItem, "ID")%></td><td><%# DataBinder.Eval(Container.DataItem, "Data")%></td><td>&nbsp;</td>
</ItemTemplate>
<AlternatingItemTemplate>
<td><%# DataBinder.Eval(Container.DataItem, "ID")%></td><td><%# DataBinder.Eval(Container.DataItem, "Data")%></td></td></tr>
</AlternatingItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>a

Javascript Confirmation: Are You SURE?
Two options here, using onclientclick (because most likely onclick calls the method that does something), and wrapping a span.

<span onclick="return confirm('Are you sure you want to do this?')"><asp:LinkButton ID="linkButton1" runat="server" Text="Do Something" /></span>
<asp:LinkButton ID="uxPollListing_delete" runat="server" Text="delete" OnClick="DoSomething" OnClientClick="return confirm('Are you sure you want to do this?')"></asp:LinkButton>