Monday, January 30, 2012

Getting Class Name and Method name using reflection

To get the current method and class name in which the code is executing, we can use reflection.

Method Details

System.Reflection.MethodBase.GetCurrentMethod().Name, 

Class Details
System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name

Handy to log the errors .. 

Thursday, January 26, 2012

Error :Attempted to access an unloaded appdomain

Attempted to access an unloaded appdomain. (Exception from HRESULT: 0x80131014)

This occurs when we try to open two different connections in the same  Transaction Scope . Use Transaction Scope.Suppress for one of the connection if you are not performing any transaction in one of the connection.


But if you are making transactions in both of the connections as a single one ... i need to study further about this ... probably you have to open a new transaction under the main one .. will verify later 

Dynamic Grouping in SSRS

To achieve dynamic grouping in SSRS , i.e you group by a parameter chosen by the user.

1) Send the group by parameter to the stored procedure/sql statement and let SQL handle the grouping by case statement
 Ex :
    SELECT   case(@groupby)
when 'gender' then dummy.Gender
when 'race' then dummy.Race
when 'district' then dummy.District
end "groupbyfield",
count(*)"Count",
 FROM  dbo.dummy
WHERE DummyDate BETWEEN @startdate and @enddate
     GROUP BYcase(@groupby)
when 'gender' then dummy.Gender
when 'race' then dummy.Race
when 'district' then dummy.District
end "groupbyfield"

2) Sort by the "groupbyfield"  in SSRS.

Wednesday, January 25, 2012

Generating Enums from Database for Lookup Tables

These are the two links i found for this problem.



http://erraticdev.blogspot.com/2011/01/generate-enum-of-database-lookup-table.html


http://idisposable.co.uk/2010/03/using-t4-to-generate-enums-from-database-lookup-tables/#viewSource





 I implemented the second one as i found it easier and more flexible in naming the enums differently from the database look-ups.I struggled with this for a few hours as i did not one thing . i.e When we add t4 template to VS make sure that it  in the properties of the t4 file the custom tool is set to "TextTemplatingFileGenerator". The default one is the text processing .. so i could not see the enum file .. Finally after some time i could identify this.




This is really nice because you find your code more readable while not loosing flexibility of look-ups. Every time you have a new look-up you have to regenerate the t4 template and build your project.


Thanks to people who wrote these templates :). It made my life easier.

Thursday, January 12, 2012

Required Field Validator for Dropdown


Set the RequiredFieldValidator.InitialValue property to the value of your prompt item like so: 
<asp:dropdownlist id="DropDownList1" runat="server" autopostback="True"         onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <asp:listitem value="-1">Select Option</asp:listitem>
        <asp:listitem>A</asp:listitem>
        <asp:listitem>B</asp:listitem>
        <asp:listitem>C</asp:listitem>
</asp:dropdownlist>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server"         errormessage="RequiredFieldValidator" controltovalidate="DropDownList1"         display="Dynamic" initialvalue="-1" setfocusonerror="True">
</asp:requiredfieldvalidator>
<asp:button id="Button1" runat="server" text="Button" />