Sunday, 12 February 2012

To reset password of specified length in Membership provider


for the following you need to set the allowpasswordretrieval property to "true" in web.config in SqlServerMemberShipProvider  

using System.Web.Security;
MembershipUser user = Membership.GetUser(userName);
// to get current password
string oldPassword = user.GetPassword();
// to generate a password of 10chars with 4 special chars
string newPassword = Membership.GeneratePassword(10, 4);
// ResetPassword generates a password of 14chars, so we use changepassword.
user.ChangePassword(oldPassword, newPassword);

C# Inheritance with example

   public class BaseClass
   {
       public void Method1()
       {
           Console.WriteLine("In BaseClass Method1");
       }
       public virtual void Method2()
       {
           Console.WriteLine("In BaseClass Method2");
       }
   }
   public class DerivedClass : BaseClass
   {
       public new void Method1()
       {
           Console.WriteLine("In DerivedClass Method1");
       }
       public override void Method2()
       {
           Console.WriteLine("In DerivedClass Method2");
       }
   }
   public class sample
   {
       static void Main(string[] args)
       {
           BaseClass b1 = new BaseClass();
           b1.Method1();
           b1.Method2();

           DerivedClass d1 = new DerivedClass();
           d1.Method1();
           d1.Method2();

           BaseClass b2 = new DerivedClass();
           b2.Method1();
           b2.Method2();

           DerivedClass d2 = new BaseClass();
           d2.Method1();
           d2.Method2();

           Console.ReadLine();  
       }
   }

Output for the above is:
In BaseClass Method1
In BaseClass Method2
In DerivedClass Method1
In DerivedClass Method2
In BaseClass Method1
In DerivedClass Method2

DerivedClass d2 = new BaseClass();
d2.Method1();
d2.Method2();
Above statement won’t get compiled, it will give compiler Error “Cannot implicitly convert type 'BaseClass' to 'DerivedClass'. An explicit conversion exists (are you missing a cast?)”

Explanation
In BaseClass Method1
In BaseClass Method2
We invoked methods of BaseClass

In DerivedClass Method1
In DerivedClass Method2
We invoked methods of DerivedClass

In BaseClass Method1
In DerivedClass Method2
We invoked Method1 of derived class using base class reference, so the base class method1 is invoked.
We invoked Method2 of derived class using base class reference, as the base class method2 is overridden in derived class derived class Method2 is invoked.

In this example we used the following OOPS concepts
  1. Shadowing or Method Hiding, new keyword in signature of Method1 in Derived Class.
  2. Overriding, override keyword in signature of Method2 in Derived Class.

Tuesday, 10 January 2012

working with local reports rdlc files from a dataset

created a local reports (rdlc) - which takes heading as a parameters.
data also comes dynamically through a datatable.
 
DataTable dtPrint = (DataTable)Session["Table"];  // data to be bound can be from session or viewstate
string rptHeading = Request.QueryString["heading"];   // heading
rpvPrint.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
rpvPrint.LocalReport.ReportPath = Page.MapPath(".") +  "\\Report.rdlc";
ReportParameter[] rp =new ReportParameter[1];
rp[0] = new ReportParameter("Heading", rptHeading);
rpvPrint.LocalReport.SetParameters(rp);  
ReportDataSource rds1 = new ReportDataSource("PrintData", dtPrint);
rpvPrint.LocalReport.DataSources.Clear();   
rpvPrint.LocalReport.DataSources.Add(rds1);
rpvPrint.LocalReport.Refresh();

regular expressions for datetime dd/MM/yyyy , MM/dd/yyyy, MM-DD-YYYY and DD-MM-YYYY

Regex DD/MM/YYYY Format

(((0[1-9]|[12][0-9]|3[01])([/]
)(0[13578]|10|12)([/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])(\d{4}))|((29)([/])(02)([/])([02468][048]00))|((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9][13579][26 ])))

Regex MM/DD/YYYY Format

(((0[13578]|10|12)([/])(0[1-9]|[12][0-9]|3[01])([/])(\d{4}))|((0[469]|11)([/])([0][1-9]|[12][0-9]|30)([/])(\d{4}))|((02)([/])(0[1-9]|1[0-9]|2[0-8])([/])(\d{4}))|((02)([/])(29)([/])([02468][048]00))|((02)([/])(29)([/])([13579][26]00))|((02)([/])(29)([/])([0-9][0-9][0][48]))|((02)([/])(29)([/])([0-9][0-9][2468][048]))|((02)([/])(29)([/])([0-9][0-9][13579][26 ])))


Regex DD-MM-YYYY Format

(((0[1-9]|[12][0-9]|3[01])([-])(0[13578]|10|12)([-])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-])(0[469]|11)([-])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-])(02)([-])(\d{4}))|((29)([-])(02)([-])([02468][048]00))|((29)([-])(02)([-])([13579][26]00))|((29)([-])(02)([-])([0-9][0-9][0][48]))|((29)([-])(02)([-])([0-9][0-9][2468][048]))|((29)([-])(02)([-])([0-9][0-9][13579][26 ])))

Regex MM-DD-YYYY Format

(((0[13578]|10|12)([-])(0[1-9]|[12][0-9]|3[01])([-])(\d{4}))|((0[469]|11)([-])([0][1-9]|[12][0-9]|30)([-])(\d{4}))|((02)([-])(0[1-9]|1[0-9]|2[0-8])([-])(\d{4}))|((02)([-])(29)([-])([02468][048]00))|((02)([-])(29)([-])([13579][26]00))|((02)([-])(29)([-])([0-9][0-9][0][48]))|((02)([-])(29)([-])([0-9][0-9][2468][048]))|((02)([-])(29)([-])([0-9][0-9][13579][26 ])))

click event does not work / trigger/fire, click event triggers/fires in double click

when working on click event in javascript or jquery, click won't fire the event 
in a single click. it requires double click. in that case 
your code may look like the following
 
<ul id="drop-menu" style="display:none;">
<li><a href="..">xxxx xxxx</a> </li>
<li><a href="..">** *** *** **</a> </li>
<li><a href="..">... ... .. </a> </li>
<li><a href="LogOff">Log Off</a> </li>
</ul>
 
 $("a#my-link").click(function(e) {
$("#drop-menu").slideToggle('medium');
});
$("body").click(function(e) {
$("#drop-menu").hide();
}); 
 
in reality, click event fires / triggers the click event, however along with the 
click event body click event also fires thus hiding your drop-menu.
here we cannot stop body click event, removing which our drop-menu doesnot get 
closed. to stop closing or hiding of our drop-menu on body click, 
we have to check the target of the event
 
like the one in the following code
$("a#my-link").click(function(e) {
$("#drop-menu").slideToggle('medium');
});
$("body").click(function(e) {
if (!$(e.target).is('#my-link')) {
$("#drop-menu").hide();
}
});
 
here in body click i am hiding the drop menu, only when the target of the click
event is my-link.
 

how to stop skype from formatting phone numbers on your webpage

include this, to stop skype from formatting the phone numbers on your webpage
<meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE" />

implementing sql "in" in linq to sql

using (DataContext dc = new DataContext("connectionstring"))

   {

     List<string> names = new List<string>();

    names.Add("Anderson");

    names.Add("Johnson");

    List<Person> list = (from p in dc.GetTable<Person>() where names.Contains(p.LastName) select p).ToList();

  }

equivalent Sql generated by the above is

DECLARE @p1 NVarChar(8);

DECLARE @p0 NVarChar(7);

Select @p1 = 'Johnson';

Select @p0 = 'Anderson';

-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8

SELECT [t0].[Id], [t0].[FirstName], [t0].[LastName]

FROM [dbo].[Person] AS [t0]

WHERE [t0].[LastName] IN (@p0, @p1)

this is found in googling, don't remember url