Wednesday, January 25, 2012

Month wise Data retriving using SQL query and how to validate correct data


SELECT  YEAR(EntryDate) AS 'Year', MONTH(EntryDate) AS 'Month', COUNT(*) AS monthly_Case_count
FROM dbo.SP_Case WHERE YEAR(EntryDate)=2011
GROUP BY YEAR(EntryDate), MONTH(EntryDate)
ORDER BY YEAR(EntryDate), MONTH(EntryDate);

SELECT COUNT(*) FROM dbo.SP_Case
WHERE YEAR(EntryDate)=2011 AND MONTH(EntryDate)=2

Thursday, January 19, 2012

Using HyperLink Field of GridView in asp.net

Supposer we have gridview as:


<ASP:GRIDVIEW id=GridView1 runat="server" autogeneratecolumns="False" datakeynames="CustomerID" datasourceid="SqlDataSource1">    
    <COLUMNS>    
        <ASP:HYPERLINKFIELD text="Detail" datanavigateurlfields="CustomerID" datanavigateurlformatstring="CustomerDetails.aspx?customerId={0}"></ASP:HYPERLINKFIELD>        
        <ASP:BOUNDFIELD datafield="CustomerID" headertext="CustomerID" readonly="True" sortexpression="CustomerID"></ASP:BOUNDFIELD>        
        <ASP:BOUNDFIELD datafield="CompanyName" headertext="CompanyName" sortexpression="CompanyName"></ASP:BOUNDFIELD>        
        <ASP:BOUNDFIELD datafield="ContactName" headertext="ContactName" sortexpression="ContactName"></ASP:BOUNDFIELD>    
    </COLUMNS>
</ASP:GRIDVIEW>

<ASP:SQLDATASOURCE id=SqlDataSource1 runat="server" connectionstring="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
selectcommand="SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]">
</ASP:SQLDATASOURCE>


1.

<asp:TemplateField>
                <ItemTemplate>
                <asp:HyperLink ID="hlDetails1" Text="Details" runat="server"
                NavigateUrl='<%# "CustomerDetails.aspx?customer=" + Eval("CustomerID") + "&CompanyName=" + Server.UrlEncode(Eval("CompanyName").ToString())%>' />
                </ItemTemplate>
    </asp:TemplateField>

2. Seting NavigateUrl property of HyperLink by Calling method in code-behind:



<asp:TemplateField> 
    <ItemTemplate> 
        <asp:HyperLink id="hlDetails2" Text="Details" Runat="server" 
        NavigateUrl='<%# GetUrl(Eval("CustomerID"),Eval("CompanyName"))%>' /> 
    </ItemTemplate> 
</asp:TemplateField>


public string GetUrl(object id, object companyname) 
 { 
 //here you can do validation e.g. if companyname is not null or something 
 //Also you can do some customization based on your logged-in user 
 //You can get the Page location dynamically from say web.config

 string url = 
"~/CustomerDetails.aspx?customerid=" + id.ToString() + "&companyname=" + 
Server.UrlEncode(companyname.ToString()); 

 return url; 
 }

3.
 Seting  NavigateUrl in RowDataBound event




<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" 
DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound">
...
...
</asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 

 if (e.Row.RowType == DataControlRowType.DataRow) 
 { 
    HyperLink hl = (HyperLink)e.Row.FindControl("hlDetails2"); 
    if (hl != null) 
    { 
        DataRowView drv = (DataRowView)e.Row.DataItem; 
        string id = drv["CustomerID"].ToString(); 
        string companyname = drv["CompanyName"].ToString(); 
        hl.NavigateUrl = "~/CustomerDetails.aspx?customerid=" + id.ToString() + "&companyname=" + Server.UrlEncode(companyname.ToString()); 
    } 
 } 
}