Math Comma , Colon : operators

I found some odd math the other day that went like this…

Question: What is 5,03% : 3,3411 ?
Translation: What is 5.03% / 3.3411 ?
Answer: 0.01505

Advertisements

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>