gridview add new row

In this post we will see how to add new row to grid. Gridview have a footer template. By default the visibility is false for this footer template . On add new row button click event, we will set the visibility of Gridview footer row to true and bind the grid again.

Aspx code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
<Columns> 
<asp:TemplateField HeaderText="ID"> 
<ItemTemplate> 
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'> 
</ItemTemplate> 
<FooterTemplate> 
 
</FooterTemplate> 

</asp:TemplateField> 
<asp:TemplateField HeaderText="First Name"> 
<ItemTemplate> 
<asp:Label ID="lblfirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'> 
</ItemTemplate> 
<FooterTemplate> 
<asp:TextBox ID="txtFirstName" runat="server"> 
</FooterTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Last Name"> 
<ItemTemplate> 
<asp:Label ID="lblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'> 
</ItemTemplate> 
<FooterTemplate> 
<asp:TextBox ID="txtLastName" runat="server"> 
</FooterTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Email"> 
<ItemTemplate> 
<asp:Label ID="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label> 
</ItemTemplate> 
<FooterTemplate> 
<asp:TextBox ID="txtEmail" runat="server"> 
</FooterTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Check all"> 
<HeaderTemplate> 
<asp:CheckBox ID="CheckBox2" AutoPostBack="true" runat="server"  OnCheckedChanged="CheckBox2_CheckedChanged1" /> 
</HeaderTemplate> 
<ItemTemplate> 
<asp:CheckBox ID="CheckBox1"  runat="server" /> 
</ItemTemplate> 
<FooterTemplate> 
<asp:LinkButton ID="lnkSave" runat="server" CommandName="Save" OnClick="lnkSave_Click">Save 
</FooterTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView>



C# code behind
using System; 
using System.Data; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
BindGrid(); 
} 
} 
private void BindGrid() 
{ 
GridView1.DataSource = GridDataProvider.GetData(); 
GridView1.DataBind(); 
} 

protected void CheckBox2_CheckedChanged1(object sender, EventArgs e) 
{ 
foreach (GridViewRow r in GridView1.Rows) 
{ 
((CheckBox)r.FindControl("CheckBox1")).Checked = true; 
} 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
GridView1.ShowFooter = true; 
BindGrid(); 
} 
protected void lnkSave_Click(object sender, EventArgs e) 
{ 
string ID = (  (TextBox)  GridView1.FooterRow.FindControl("txtID") ).Text ; 
string firstName = (  (TextBox)  GridView1.FooterRow.FindControl("txtFirstName") ).Text; 
// similarly you can find other controls and save 
} 





Vb.Net Code

Imports System 
Imports System.Data 
Imports System.Configuration 
Imports System.Web 
Imports System.Web.Security 
Imports System.Web.UI 
Imports System.Web.UI.WebControls 
Imports System.Web.UI.WebControls.WebParts 
Imports System.Web.UI.HtmlControls 
Public Partial Class _Default 
Inherits System.Web.UI.Page 
Protected Sub Page_Load(sender As Object, e As EventArgs) 
If Not Page.IsPostBack Then 
BindGrid() 
End If 
End Sub 
Private Sub BindGrid() 
GridView1.DataSource = GridDataProvider.GetData() 
GridView1.DataBind() 
End Sub 

Protected Sub CheckBox2_CheckedChanged1(sender As Object, e As EventArgs) 
For Each r As GridViewRow In GridView1.Rows 
(DirectCast(r.FindControl("CheckBox1"), CheckBox)).Checked = True 
Next 
End Sub 
Protected Sub Button1_Click(sender As Object, e As EventArgs) 
End Sub 
Protected Sub Button2_Click(sender As Object, e As EventArgs) 
GridView1.ShowFooter = True 
BindGrid() 
End Sub 
Protected Sub lnkSave_Click(sender As Object, e As EventArgs) 
Dim ID As String = (DirectCast(GridView1.FooterRow.FindControl("txtID"), TextBox)).Text 
Dim firstName As String = (DirectCast(GridView1.FooterRow.FindControl("txtFirstName"), TextBox)).Text 
' similarly you can find other controls and save 
End Sub 
End Class 

C# read text file

In this post we are going to discuss how to read text file in c#. .Net provides File class in System.IO namespace which contains methods to read file from the disk.
We can read all the contents of text file in one go using ReadAllText() method of File class.
Also we can use ReadAllLines() method of File class and read each line of the file and store it in the string array and then by iterating through string array we can show each line.
System.IO namespace also contains StreamReader class which has ReadLine() method that we can use to read a Text file one line at a time.We will now see code samples to read file in C# by all the ways we discussed above.


Sample code to read text file in one go [C#]


We will use here ReadAllText() method in File class read all the contents of text file as one string [C#]

class ReadFile
{
 static void Main()
 {
   // Example to Read the file as a whole string.
    string fileText =  System.IO.File.ReadAllText( @"C:\MyFolder\TextFile.txt"); 
   // Show read content of text file in text box .
    TextBox1.Text = fileText ;
}
}

Sample code to read text file in line by line in string array [C#]

we will use here to read all lines by line in text file using ReadAlllines() method in File class.


class ReadFileLines
{
 static void Main()
{ 
   // Example to Read the file lines into a string array.
   string[] allLines = System.IO.File.ReadAllLines (@"C:\MyFolder\TextFile.txt"); 
   //Write text file contents to text box
  foreach(string line in allLines)
  {
   TextBox.Text += "\t\n" + line;
  }
 }
}

we will now look how we can use ReadLine() method of StreamReader class to read each line of text file.


Sample code to read each line of text file [C#]


int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
while((line = file.ReadLine()) != null)
{
System.Console.WriteLine (line);
counter++;
}
file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
System.Console.ReadLine();

delete folder, directory c#

In order to remove a directory or folder using C#, .Net framework provides System.IO namspace which provides Directory.Delete method.
Directory.Delete
is a static method that gives you easy way to delete or remove folder or directory. Also we can delete directory or folder using DirectoryInfo class of System.IO namespace.

Examples of deleting directory or folder Directory.CreateDirectory:

Following is sample code for deleting or removing directory or folder on root i.e. at C:/ drive

using System.IO;

class DeleteFolder
{
static void Main()
{

// Delete Directory/folder on C:\ Drive.
Directory.Delete("C:\\TestDirectory");
}
}


Sample code to delete directory using System.IO DirectoryInfo class

using System.IO;

class CreateNewFolder
{
static void Main()
{

// Get Directory info or folder info.
DirectoryInfo dir = new DirectoryInfo(@"c:\TestDirectory");
dir.Delete();
}
}

Create new folder, directory

create folder c#
In this post we will see how to create create folder or directory using C# on server to perform some operations dynamically. .Net provides System.IO namespace to perform operation related to file system. System.IO namespace also provides ways to create new folder or directory.In order to create a directory or new folder using C#, we can either use Directory.CreateDirectory method of the System.IO namespace or we can use DirectoryInfo class of System.IO namespace.


Directory.CreateDirectory is a static method that gives you easy way to create new folder or new directory. we will see example of creating new directory or folder using Directory.CreateDirectory:

Sample code that shows how to create directory or folder on root i.e. at C:/ drive


usingSystem.IO;
class CreateNewFolder
{
 static void Main()
 {
   // Create new Directory or folder on C:\ Drive.
   Directory.CreateDirectory("C:\\TestDirectory");
 }
}

We can also add subfolders to the existing folder, so below C# code shows how to create subdirectory or sub folder in existing folder.


using System.IO;
class CreateNewFolder
{
 static void Main()
 {
   // Create new Directory or folder on C:\ Drive.
   Directory.CreateDirectory("C:\\TestDirectory\\SubFolder");
}
}

There are also alternative ways to create sub directory or sub folders. We can create directory or folder using DirectoryInfo class of System.IO namespace.

Sample code to create directory using System.IO DirectoryInfo class

using System.IO;
    class CreateNewFolder
    {
    static void Main()
    {
    // Get Directory info or folder info.
    DirectoryInfo dir = new DirectoryInfo(@"c:\TestDirectory");
    dir.create();
    }
    }
    

ASP.Net Authentication Modes

ASP.Net supports below authentication modes:
1. Windows
2. Forms
3. Passport
4. None

To enable authentication provider or mode we need to use authentication element from config file.
<system.web>
   <!-- mode=[Windows|Forms|Passport|None] -->
   <authentication mode="Windows" />
</system.web>
1.Windows:
  This is a default authentication mode in asp.net. This uses windows credentials for authentication. It relies on IIS to authenticate the user. Aftre authenticating the user it passes security token to asp.net. Windows authentication provides below ways:

Anonymous: IIS allows everybody to access asp.net application, no authentication is done.

Basic: User must have to provide username and password to get access to application. But username and password get sent over network in plain text format so it is bit insecure.

Digest: Same as Basic windows authentication but password is hashed before sending it over the netwrok and also user need to be use IE5 or later and windows accounts should stored in active directory.

Windows Integrated: User still need to provied username and password but it never sent over the network. Application either uses kerberos or challenge response protocol to authenticate the user. Kerberos provides tools for authentication and strong cryptography for secure communication.

2. Forms Authentication mode:  Forms authentication uses own customised HTML form to collect users credentials. Client or user directly sends credentials to asp.net application code for authenticatio. If application authenticates user it issues a cookie to ensure user is present on subsequent requests.

<!-- Web.config file -->
<system.web>
   <authentication mode="Forms">
      <forms forms="demoApp" loginUrl="/login.aspx" />
   </authentication>
</system.web>
 3. Passport Authentication: Passport authentication mode provides centralized authentication process provided by microsoft passport service. When user site registered with passport, the passport service grants site specific key.
<!-- Web.config file -->
<system.web>
   <authentication mode="Passport" />
</system.web>