Pages

Tuesday, 19 February 2013

Comparing dates without times in SQL Server

WHERE expire_date >= CAST(FLOOR(CAST( getdate() AS float)) AS datetime)

SELECT CAST(FLOOR(CAST( getdate() AS float)) AS datetime)
returns “2007-01-20 00:00:00.000″ – the date without the time.

Monday, 18 February 2013

How to Get DataKey value in RowDataBound or RowCommand Event in ASP.Net GridView control ?

How to Get DataKey value of a Row in RowDataBound or RowCommand Event in ASP.Net GridView control ?


We will normally set the primary key field to the DataKeyNames property of GridView control to identify the row. This little code snippet will help us to find the data keys assocciated with a row in DataBound and RowCommand event in codebehind file.

Refer the below code,
ASPX
  <asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False"
            DataKeyNames="UserID" OnRowDataBound="gvUsers_RowDataBound"
            RowStyle-CssClass="Row" onrowcommand="gvUsers_RowCommand">
                    <Columns>                      
                        <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True" />     
                         <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" /> 
                         <asp:TemplateField>
                         <ItemTemplate>
                        <asp:Button runat="server" Text="SELECT" CommandName="Select" />                             
                        </ItemTemplate>
                         </asp:TemplateField>                       
                    </Columns>                   
                    <HeaderStyle BackColor="#F06300" Font-Bold="True" ForeColor="#FFFFCC" />
     </asp:GridView>


RowDataBound Event
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            object objTemp = gvUsers.DataKeys[e.Row.RowIndex].Value as object;
            if (objTemp != null)
            {
                string id = objTemp.ToString(); //Do your operations
            }
          
        }
    }


RowCommand Event
    protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      Control ctl = e.CommandSource as Control;
      GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
      object objTemp = gvUsers.DataKeys[CurrentRow.RowIndex].Value as object;
      if (objTemp != null)
      {
          string id = objTemp.ToString(); //Do your operations
      }
    }


Refer this link
http://codes.codedigest.com/CodeDigest/156-How-to-Get-DataKey-value-in-RowDataBound-or-RowCommand-Event-in-ASP-Net-GridView-control--.aspx

Friday, 15 February 2013

Select different CSS files based on IE version in ASP.NET

Determine the browser type from the HTTP request:
System.Web.HttpBrowserCapabilities browser = Request.Browser
Then render the page accordingly:
<% if (browser.Browser == "IE" && browser.MajorVersion == 6) { %>
  <link rel="stylesheet" type="text/css" href="style/ie6.css" media="screen" />
<% } else if (browser.Browser == "IE" && browser.MajorVersion == 7) { %>
  <link rel="stylesheet" type="text/css" href="style/ie7.css" media="screen" />
<% } else if (browser.Browser == "IE" && browser.MajorVersion == 8) { %>
  <link rel="stylesheet" type="text/css" href="style/ie8.css" media="screen" />
<% } %>
 
I believe you have to set the runat="server" attribute within the <head> element of the page for this to work.
This isn't a very good way of doing it though. A better way would be to do it client-side using either JavaScript or the method used in the question.
 

if IE Conditional CSS using ASP.NET Themes

 string appPath = Request.ApplicationPath;
        if (appPath.Length == 0)
            appPath = "/";
        else if (!appPath.EndsWith("/"))
            appPath += "/";

        String themeDir = Page.Theme;
        if (themeDir == null || themeDir.Length == 0)
            themeDir = Page.StyleSheetTheme;
        String conditionalCode = 
@"<!--[if IE 6]><link rel='stylesheet' href='" + 
  appPath + @"app_themes/" + themeDir + 
  @"/style.css.ie6' type='text/css' /><![endif]-->
  <!--[if IE 7]><link rel='stylesheet' href='" + 
  appPath + @"app_themes/" + themeDir + 
  @"/style.css.ie7' type='text/css' /><![endif]-->
";
        Page.ClientScript.RegisterClientScriptBlock
            ("".GetType(), 
            "iespecific", 
            conditionalCode);

Style sheet from ASP.NET Code behind Based on Browser Type

Code:
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicStyleSheet.aspx.cs" Inherits="DynamicStyleSheet" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body>
<form id="form1" runat="server">
<div id="container">
<h1>Sample Div
</h1>
</div>
</form>
</body>
</html>

Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class DynamicStyleSheet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// HTML control of link type
HtmlLink stylesheetLink = new HtmlLink();
// Adding attributes based on std html link tag for stylesheet
// example output: <link type="text/css" rel="stylesheet" href="" />
stylesheetLink.Attributes.Add("type", "text/css");
stylesheetLink.Attributes.Add("rel", "stylesheet");
// Browser Detection for IE or Chrome
if (Request.Browser.Browser.Contains("IE"))
{
// Attaching stylesheet url to href attribute of link
stylesheetLink.Attributes.Add("href", ResolveClientUrl("~/styles/IEStyle.css"));
}
else if (Request.Browser.Browser.Contains("Chrome"))
{
stylesheetLink.Attributes.Add("href", ResolveClientUrl("~/styles/Chrome.css"));
}
// Adding control to Page's header section
Page.Header.Controls.Add(stylesheetLink);
}
}

Stylesheet for IE. Name: IEStyle.css
div#container
{
background-color:lightyellow;
width:200px;
height:200px;
border:1px dotted black;
}

Stylesheet for Chrome. Name: Chrome.css
div#container
{
background-color:lightGreen;
width:200px;
height:200px;
border:1px solid black;


Final output:

How to assign correct permissions to App_Data folder of WebMail Pro ASP.NET

If you got one of the following errors during WebMail Pro ASP.NET installation:

Creating/deleting folders Error, can't create folders in the data folder.

Creating/deleting files Error, can't create files in the data folder.

Error, can't read/write "C:\Inetpub\wwwroot\WebMailPro\App_Data\settings\*.xml" file.

This means WebMail Pro ASP.NET doesn't have permissions enough to read/write contents of App_Data subfolder.

Note: In this article, we assume your WebMail Pro ASP.NET is deployed to C:\Inetpub\wwwroot\WebMailPro\ folder, but if you've deployed it to another folder, you should take this into account when looking at the paths here.

To resolve the issue, you should grant Full Control permission to ASPNET, NETWORK SERVICE and Internet Guest Account system accounts over App_Data folder:

1. In Windows Explorer, go to C:\Inetpub\wwwroot\WebMailPro\ folder, right-click App_Data folder and choose Properties:


2. In General tab, make sure "Read-only" option is not set:


3. In Security tab, gran "Full Control" permission to ASPNET, NETWORK SERVICE and Internet Guest Account accounts:


4. Click OK. Now, WebMail Pro ASP.NET should have enough permissions to read/write files and folders in the App_Data folder.