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()); 
    } 
 } 
}






Friday, October 21, 2011

How to hide/display item of gridview like button or Image



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

         if (e.Row.RowType == DataControlRowType.DataRow)

        {

            // Here Documentname : Is the columan name of gridview

            string docName = DataBinder.Eval(e.Row.DataItem, "Documentname").ToString();



            if (docName.Length == 0)

            {  // Here lnkDetails : is the control embeded in gridview

                ((LinkButton)e.Row.FindControl("lnkDetails")).Visible = false;



            }



        }

    }






Wednesday, August 3, 2011

Javascript for calculating age

<script type="text/javascript">
       function showAge() {
          
           var d = document.getElementById('<%= txtDateOfBirth.ClientID %>').value.split('/');
           var today = new Date();
           var bday = new Date(d[2], d[1], d[0]);
           var by = bday.getFullYear();
           var bm = bday.getMonth() - 1;
           var bd = bday.getDate();
           var age = 0; var dif = bday;
           while (dif <= today) {
               var dif = new Date(by + age, bm, bd);
               age++;
           }
           age += -2;
           document.getElementById("<%=txtCurrentAge.ClientID %>").value = age;
           alert('You are ' + age + ' years old')
       }
</script>

//On page load write this..
this.txtDateOfBirth.Attributes.Add("OnBlur", "javascript:return  showAge();");

Validation on Checkbox

 <script language="javascript" type="text/javascript"> 

function validateCheckBox1(source, args)
        {
            args.IsValid = document.getElementById('<%= Chk1.ClientID %>').checked;
        }
</script>

// call this with custom validator in HTML page

CheckBox ID="Chk1" runat="server" />
                      
<asp:CustomValidator runat="server" ID="CustomValidator4" Visible="true"
ClientValidationFunction="validateCheckBox2"
 ErrorMessage="Please confirm the authorisation 1" Display="Dynamic"  />

Validation in Radiobutton List


<script language="javascript" type="text/javascript">

 function ValidateRadioButtonListSIALenece() {
            var RBL = document.getElementById('<%= RbtnSIA.ClientID %>');
            var radiobuttonlist = RBL.getElementsByTagName("input");
            var counter = 0;
            var atLeast = 1;
            for (var i = 0; i < radiobuttonlist.length; i++) {
                if (radiobuttonlist[i].checked) {

                    if (radiobuttonlist[i].value == "Yes") {
                        // alert('y');
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").value = "";
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").readOnly = false;
                        document.getElementById("<%=Img29.ClientID %>").style.visibility = 'visible';
                        document.getElementById("<%=RegularExpressionValidator10.ClientID %>").readOnly = false;
                    }
                    if (radiobuttonlist[i].value == "No") {
                        // alert('N');
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").value = "";
                        document.getElementById("<%=txtDateOfApplication.ClientID %>").readOnly = true;
                        document.getElementById("<%=Img29.ClientID %>").style.visibility = 'hidden';
                        document.getElementById("<%=RegularExpressionValidator10.ClientID %>").readOnly = true;

                    }

                    counter++;
                }
            }
        }
</script>

I am calling this on radiobutton list click event
this.RBtnList.Attributes.Add("OnClick", "javascript:return  ValidateRadioButtonListSIALenece();");