MS Excel Tricks

Reference the same cell (A1 in this case) no matter where the equation moves to :


Convert boolean TRUE or FALSE to a number (1 or 0 respectively):

= TRUE*1

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

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

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);
        catch (Exception ex) 
           throw ex; 

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);
            } //end using command
        } //end using connection

Two Column Repeater Layout

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

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>