Saturday 30 June 2012

Read Excel file in C#




protected void BtnReadExcel_Click(object sender, EventArgs e)
        {
            try
            {
              OleDbConnection con = new OleDbConnection("
                                                Provider=Microsoft.Jet.OLEDB.4.0;
                                                Data Source="
+ Server.MapPath("TestExcel.xlsx") + ";
                                                Extended Properties='Excel 8.0;HDR=Yes;'"
 

                                               );
                OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]",
                                                                                 con);
                DataSet ds = new DataSet();
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

Wednesday 27 June 2012

Remove ViewState from ASPX Page.



Just Copy/Paster following Code in your Code.
 
Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
        Return (New LosFormatter().Deserialize(DirectCast(Session("ViewState"), String)))
    End Function

    Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal state As Object)
        Dim los As New System.Web.UI.LosFormatter()
        Dim sw As New System.IO.StringWriter()
        los.Serialize(sw, state)
        Dim vs As String = sw.ToString()
        Session("ViewState") = Nothing
        Session("ViewState") = vs
    End Sub

Tuesday 26 June 2012

Check JavaScript is Disabled Or NOT


This tag data will not display when JavaScript is enabled.

Write it in HTML 

<noscript>
  Write your content you want to display when JavaScript is disabled
  <div>JavaScript is not enabled</div>
</noscript>

Friday 22 June 2012

Find css Class using JavaScript


Find css Class and its css Attributes in the Page.



function setClassName() {

    var v = document.styleSheets    // Diaply Style Tags in Page
     for (i = 0; i < v.Length; i++) {
           var w = document.styleSheets[0].cssRules     // Also Array of Class in that Style TAg
           for (j = o; j < w.Length; j++) {
                w[j].selectorText == "ClassName in Style Tag"
                w[j].style.width = "100px";   /// Set width in Class of Style, 
                                                               all style attribute can got from here
           }
      }

   }

Check Radio button from JavaScipt


Find which Radio button is Checked from JavaScript : 

JS :
 function check(obj) {
           var a = null;
           var f = document.forms[0];
           var e = f.elements[obj];
if (e[0].checked == true) {               
alert(“1 radio button is checked”)
}
           else {
alert(“2 radio button is checked”)
           }
       }


 HTML :
  <asp:RadioButtonList ID="RDB" runat="server" >
           <asp:ListItem Value="1">Ajay</asp:ListItem>
           <asp:ListItem Value="2">Patel</asp:ListItem>
        </asp:RadioButtonList>
        <asp:Button ID="btn" runat="server" Text="Button" />
        <input type="submit" value="submit" />

Thursday 14 June 2012

All in one Validation in JavaScript


<script type="text/javascript">
        function Validation(obj1, req, email, Numeric) {
            try {
                var val1 = document.getElementById(obj1).value

                if (req == 1) { // For Required field Validation                   
                    if (val1.trim() == '') {
                        alert("Please enter valid value"); return false
                    }
                    return true;
                }
                if (email == 1) {   // For Email ID Validation
                    var filter1 = /^.+@.+\..{2,3}$/;
                    if (filter1.test(val1) != true) {
                        alert("Email ID is not valid");
                        return false;
                    }
                }
                if (Numeric == 1) {  // for Numeric Only Validation
                    var reg = /^-{0,1}\d*\.{0,1}\d+$/;
                    if (reg.test(val1) != true) {
                        alert("Please enter numeric only");
                        return false
                    }
                }

            }
            catch (e) {
            }
        }    
    </script>

In HTML:
<input type="submit" name="btn" value="Button" onclick="return Validation('txt',1,0,1);" 
          id
="Submit" />

AutoCompleter in Ajax

Register Assembly in Page Directive :

<%@ Register Assembly="AjaxControlToolkit"
                    
 Namespace="AjaxControlToolkit"
                      TagPrefix="cc1" %>

IN HTML 00Test.aspx:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="txt" runat="server”></asp:TextBox>
 <cc1:AutoCompleteExtender ID="AC" runat="server"
                            
EnableViewState="False"
                             FirstRowSelected="True"
ServicePath="00Test.aspx"
ServiceMethod="ListSearch"
TargetControlID="txt"
DelimiterCharacters=":,;"
CompletionListCssClass="ac_completionListElement" CompletionSetCount="10"
CompletionListItemCssClass="ac_listItem" CompletionListHighlightedItemCssClass="ac_highlightedListItem"
                             MinimumPrefixLength="1"
CompletionInterval="1000"
OnClientItemSelected="MY" >
</cc1:AutoCompleteExtender>

In Code behind 00Test.aspx.vb:

Partial Class _00Test
    Inherits System.Web.UI.Page

<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
  Public Shared Function ListSearch(ByVal prefixText As String,
                                                ByVal count As Integer) As String()
 If count > 0 Then
    Dim con As New SqlClient.SqlConnection("Connestion String")
    Dim cmd As New SqlClient.SqlCommand("USP_Page", con)
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.AddWithValue("@SearchTxt", prefixText)
    Dim DT As New DataTable
    Dim DA As New SqlClient.SqlDataAdapter(cmd)
    DA.Fill(DT)
    Dim lstCode As New Generic.List(Of String)
    For i As Integer = 0 To DT.Rows.Count - 1
         lstCode.Add(DT.Rows(i)("AutoSearch").ToString)
    Next
       Return lstCode.ToArray
    End If
    End Function
End Class


// call back function onItemSelect
    this function will called when any item will selected from AC List
function MY(source, eventArgs) {

            alert(eventArgs._text);

        }

.css
.ac_completionListElement
{
    padding-left: 0px;   
    background-color: inherit;
    color: #696969;
    border: 1px solid buttonshadow;       
    text-align: left;
    list-style-type: none;
    margin-top:0;
}

/* AutoComplete highlighted item */

.ac_highlightedListItem
{
    background-color: #EAF1F7;
    color: black;
    padding: 4px
    cursor:pointer
}

/* AutoComplete item */

.ac_listItem
{
    background-color: window;
    color: #696969;
    padding: 4px;
   
}