Sunday, May 6, 2012

Satyamev Jayate 6th may 2012 Episode


The opening episode showed a most important issue of inda Female foeticide  




I hope u really enjoyed the episode but one request from me "Don't just watch Satyamev Jayate, Female foeticide is an imp issue. Its about protecting n loving our girlchild. Let us start from our home .

Tuesday, January 10, 2012

Best way to bind gridview


This is the optimized code to bind a gridview in asp.net in C#. As all know it is most favorite question in interview so answer it with optimization



private static void Fn_GridBInd()
        {
            try
            {
                using (var odt = new DataTable())
                {
                    using (var ocon = new SqlConnection("Data Source=121.0.0.1; Initial catalog=master; User ID=sa; pwd=admin"))
                    {
                        using (var od = new SqlDataAdapter("select * from sys.tables", ocon))
                        {
                            od.Fill(odt);
                        }
                    }
                    if (odt.Rows.Count > 0)
                    {
                        //Bind the gridview with smile :)
                    }
                }
            }
            catch (Exception ex)
            {
                //Handle the exception.... :(
            }
        }

Wednesday, October 19, 2011

Best way to create [CheckUserNameAvailability] Strored procedure


Hello friends, As we know in all our projects we have [CheckUserNameAvailability] SP in which check user availability at the time of registration





CREATE PROCEDURE [CheckUserNameAvailability] @UserName VARCHAR(30)
AS 
    BEGIN
        SET NOCOUNT ON 
        
        IF EXISTS ( SELECT  1
                    FROM    Users
                    WHERE   UserName = @UserName ) 
            SELECT  '1'
        ELSE 
            SELECT  '0'
            
        SET NOCOUNT OFF             
    END
GO




Thanks
Rahul

Tuesday, October 18, 2011

Using multiple authentication modes in a single ASP.NET application


Summary
ASP.NET only allows a single authentication mode to be configured for each application. Some applications may require different modes for different paths. For example, if an application uses Forms authentication for users of the public site, and Windows authentication for users of the administration site.
Resolution
One solution is to create two separate applications; one for the public site using Forms authentication, and one for the administration site using Windows authentication. However, this will mean that resources in the public application cannot be accessed from the administration application using app-relative paths (e.g. /images/logo.jpg).
A better solution is to use the global.asax file to handle theFormsAuthentication_OnAuthenticate event, and apply Windows authentication where appropriate.
  1. In IIS, open the properties of the administration folder;
     
  2. Under "Directory Security", edit the anonymous access and authentication settings, and disable anonymous access;
     
  3. Edit the web.config file, and configure the application to use Forms authentication;
     
  4. Add the following code to the global.asax file in the root of the application:



<%@ Application Language="C#" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
 
<script runat="server">
 
public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs e)
{
    if (null == e) throw new ArgumentNullException("e");
     
    // Check that we have a Windows user
    WindowsIdentity winUser = e.Context.Request.LogonUserIdentity;
    if (null == winUser) return;
     
    // Check that the path allows Windows authentication
    string path = VirtualPathUtility.ToAppRelative(e.Context.Request.Path);
    if (!IsWindowsAuthenticated(path, e.Context)) return;
     
    // Don't allow guest accounts to access
    if (winUser.IsAnonymous || winUser.IsGuest || winUser.IsSystem) return;
     
    WindowsPrincipal winPrincipal = new WindowsPrincipal(winUser);
    if (winPrincipal.IsInRole("Guests")) return;
     
    e.User = winPrincipal;
}
 
private static bool IsWindowsAuthenticated(string path, HttpContext context)
{
    if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
    if (null == context) throw new ArgumentNullException("context");
     
    // Check that the path starts with the restricted folder:
    if (path.StartsWith("/restricted/", StringComparison.OrdinalIgnoreCase)) return true;
     
    return false;
}
 
</script>



      5. Requests to the administration folder will now use Windows authentication; the rest of the site will continue to use Forms authentication

Friday, October 14, 2011

Extension method for conversion generics List to datatable

Hello guys,

In this article i have posted extension method for conversion generics List to datatable i have used in one of my business logic.



public static class Cls
    {
        public static DataTable ToDataTable<T>(this IList<T> data)
        {
            PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(typeof (T));
            var table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            var values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }
    }
In ths code we PropertyDescriptorCollection class for geberic description.

we can also this task with the help of reflection but i have work this code only once so i don't bother more.


Thanks
  - Rahul