Thursday, September 15, 2011

Loop through all folders and display files in a Gridview using ASP.NET

Loop through all folders and display files in a Gridview using ASP.NET This is one of the usefull code snippet everybody looking for.
When I search google I found lot of methods to accomplish this task but all seems to be very complex and lengthy code.
This is just a 4 line code to loop through all the folders, including subfolder to get the file detals and display it in Gridview.
You may alter this based on your need.
    
 protected void Page_Load(object sender, EventArgs e)
     {
       ArrayList arrFileList = new ArrayList();
       string[] strFiles = Directory.GetFiles("C:\\Test", "*", SearchOption.AllDirectories);
       foreach (string fileName in strFiles)
       {  
         arrFileList.Add(fileName); 
       }  
       GridView1.DataSource = arrFileList;  
       GridView1.DataBind();  
    }  

Thursday, July 14, 2011

How to check SQL table exists or not, if exists delete the same and create a new one

Nowadays it became my habit that I am posting the code or article only when people ask the questions in forums.

This is one another regular question I used to see in forums "How to check SQL table exists or not, if exists delete the same and create a new one".

What normally people suggests is query the table and if nothing returned then it doesn't exists, but if you do that execution will take some time.
But if you check directly the Object ID of the table it will be much quicker.

Below is a simple query which helps you to check whether table exists or not in the database and if table exists then this query drops the table and create a new one with the same name.
IF OBJECT_ID('dbo.Users') IS NOT NULL
DROP TABLE dbo.Users 
GO
 
CREATE TABLE dbo.Users 
(
  ID INT,
  UserID Varchar(50)
) 
GO
 
select * from Users  
GO
I believe above code is self explanatory and you don’t need much explanation about that. Happy to provide more details if you have any questions.

Thursday, May 19, 2011

Check and uncheck all checkbox in GridView using JavaScript.


This is one of the most common query I saw in Microsoft forums when a beginner start coding. So I thought I will take some time and post a simple and useful code here.
 
<html>
<head id="Head1" runat="server">
  <title>Untitled Page</title>
  <script type="text/javascript">
      function CheckAllRow(chkBox) {
          var gridViewCtlId = '<%=GridView1.ClientID%>';
          var grid = document.getElementById(gridViewCtlId);
          var gridLength = grid.rows.length;
          for (var i = 1; i < gridLength; i++) {
              cell = grid.rows[i].cells[1];
              for (var j = 0; j < cell.childNodes.length; j++) {
                  if (cell.childNodes[j].type == 'checkbox') {
                      cell.childNodes[j].checked = chkBox;
                  }
              }
          }
      }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:LinkButton ID="chkAll" Text="Check all" OnClientClick=" CheckAllRow(true); return false;"
      runat="server"></asp:LinkButton><br />
    <asp:LinkButton ID="lnkUncheck" Text="Uncheck all" OnClientClick=" CheckAllRow(false); return false;"
      runat="server"></asp:LinkButton><br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="True"
       OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
      OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound">
      <Columns>
        <asp:TemplateField>
          <HeaderTemplate>
            Select All
            <asp:CheckBox ID="chkBoxAll" runat="server" onclick="CheckAllRow(this.checked);" />
          </HeaderTemplate>
          <ItemTemplate>
            <asp:CheckBox ID="chkBoxSel" runat="server" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Course ID">
          <ItemTemplate>
            <%# Eval("ID") %>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Course Name">
          <ItemTemplate>
            <%# Eval("Name")%>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title">
          <ItemTemplate>
            <%# Eval("Address")%>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Description">
          <ItemTemplate>
            <%# Eval("Phone")%>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Course Type">
          <ItemTemplate>
            <%# Eval("Description") %>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
  </div>
  </form>
</body>
</html>

Wednesday, May 18, 2011

How to add website in favorites list using ASP.NET and JavaScript


There are couple of ways to add website URL in browser Favorites list,
Just a have a look at below code snippet,

First method,
 <asp:Button ID="btnAddToFavorites" runat="server" Text="Add Me To Favorites List" OnClientClick="window.external.AddFavorite(location.href, document.title);" />

Second method,
 <html>

<head>

<script language="javascript">

function AddToFav()

{

bookmarkurl= "http://asheej.blogspot.com/";

bookmarktitle="Good .NET Articles"

if (document.all)

window.external.AddFavorite(bookmarkurl,bookmarktitle)

else if (window.sidebar) // firefox

window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");

}

</script>

</head>

<body>

<input TYPE="button" VALUE="Add to Favorites" onClick='javascript:AddToFav()' />

</body>

</html> 

Wednesday, April 27, 2011

ExcelDataReader to read excel

ExcelDataReader is a Lightweight library used for reading Microsoft Excel files in .NET.

This Dll will avoid lot of overhead which you may face during Excel operation in your .NET project.

First download the dll using the link, ExcelDataReader DLL Download

Then use below code,
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
  
 IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
  
 IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
  
 DataSet result = excelReader.AsDataSet();
  
 excelReader.IsFirstRowAsColumnNames = true;
  
 DataSet result = excelReader.AsDataSet();
  
 while (excelReader.Read())
 {
   //excelReader.GetInt32(0);
 }
 excelReader.Close();  

Tuesday, April 26, 2011

Stop loading page load for few seconds using JavaScript


Add below JavaScript function under head tag of your .aspx page.
 <script type="text/javascript">
  
 function DelayAlert()
  
 {
  
 alert("Page will load after 30 seconds");
  
 }
  
 function DelayPageLoad()
  
 {
  
 setTimeout("DelayAlert()", 30000);
  
 }
  
 </script>
  
You can call above function from body onload like below,

 <body onload="DelayPageLoad">
  
 //page content goes here...
  
 </body>  

Wednesday, March 30, 2011

Where is GAC physical Location in windows Operating System

Where is GAC physical Location in windows Operating System:-

I Noticed that many people asking this question in many forums. So I thought I will share this with everyone through my blog.

GAC is located in C:\windows\assembly

To open the folder directly
Start-->Run--> Type Assembly and press Enter.