//using Ionic.Zip;
//take one file upload control and click event write
if (FileUpload1.HasFile)
{
String TargetDirectory = FileUpload1.PostedFile.FileName;
using (Ionic.Zip.ZipFile zip1 = Ionic.Zip.ZipFile.Read(TargetDirectory))
{
zip1.ExtractAll(Server.MapPath("UploadFile")
, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently);
}
}
Monday, February 21, 2011
Sunday, February 20, 2011
Singleton Example
//Class , Only one instance is created of Singleton class.
public class Singleton
{
public int x=0;
private static Singleton Instance;
private Singleton() { }
public static Singleton instance
{
get
{
if (Instance == null)
{
Instance = new Singleton();
}
return Instance;
}
}
}
//Page Load Check for Instance
Singleton s = Singleton.instance;
Singleton s1 = Singleton.instance;
public class Singleton
{
public int x=0;
private static Singleton Instance;
private Singleton() { }
public static Singleton instance
{
get
{
if (Instance == null)
{
Instance = new Singleton();
}
return Instance;
}
}
}
//Page Load Check for Instance
Singleton s = Singleton.instance;
Singleton s1 = Singleton.instance;
Labels:
Singleton Example
Friday, February 18, 2011
data table in ASP.NET 2.0(C#)
//Add a GridView control, one lable controls, four textbox controls and button control in the page .
DataTable myDt;
private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "id";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "username";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "firstname";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "lastname";
myDataTable.Columns.Add(myDataColumn);
return myDataTable;
}
private void AddDataToTable(string id,string username, string firstname, string lastname, DataTable myTable)
{
DataRow row;
row = myTable.NewRow();
row["id"] = id;
row["username"] = username;
row["firstname"] = firstname;
row["lastname"] = lastname;
myTable.Rows.Add(row);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myDt = new DataTable();
myDt = CreateDataTable();
Session["myDatatable"] = myDt;
this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
this.GridView1.DataBind();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "")
{
this.lblTips.Text = "You must fill a username.";
return;
}
else
{
AddDataToTable(this.TextBox1.Text.Trim(),this.txtUserName.Text.Trim(), this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim(), (DataTable)Session["myDatatable"]);
GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
GridView1.DataBind();
this.txtFirstName.Text = "";
this.txtLastName.Text = "";
this.txtUserName.Text = "";
this.TextBox1.Text = "";
this.lblTips.Text = "";
}
}
DataTable myDt;
private DataTable CreateDataTable()
{
DataTable myDataTable = new DataTable();
DataColumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "id";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "username";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "firstname";
myDataTable.Columns.Add(myDataColumn);
myDataColumn = new DataColumn();
myDataColumn.DataType = Type.GetType("System.String");
myDataColumn.ColumnName = "lastname";
myDataTable.Columns.Add(myDataColumn);
return myDataTable;
}
private void AddDataToTable(string id,string username, string firstname, string lastname, DataTable myTable)
{
DataRow row;
row = myTable.NewRow();
row["id"] = id;
row["username"] = username;
row["firstname"] = firstname;
row["lastname"] = lastname;
myTable.Rows.Add(row);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myDt = new DataTable();
myDt = CreateDataTable();
Session["myDatatable"] = myDt;
this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
this.GridView1.DataBind();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "")
{
this.lblTips.Text = "You must fill a username.";
return;
}
else
{
AddDataToTable(this.TextBox1.Text.Trim(),this.txtUserName.Text.Trim(), this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim(), (DataTable)Session["myDatatable"]);
GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
GridView1.DataBind();
this.txtFirstName.Text = "";
this.txtLastName.Text = "";
this.txtUserName.Text = "";
this.TextBox1.Text = "";
this.lblTips.Text = "";
}
}
Labels:
data table in ASP.NET 2.0(C#)
Grid View with check boxe Insert all the vlue in maping tble
//DataBase
//first table
CREATE TABLE tbluser(
[UId] [int] IDENTITY(1,1) NOT NULL,
[usreId] [varchar](100) CONSTRAINT [pkuser] PRIMARY KEY CLUSTERED ,
[UserName] [varchar](100)
)
//second table
CREATE TABLE tblGroup(
[GroupId] [int] IDENTITY(1,1)CONSTRAINT [pkGroup] PRIMARY KEY CLUSTERED ,
[GroupName] [varchar](100) )
//third table
CREATE TABLE MapUserGroup(
[usreId] [varchar](100) FOREIGN KEY([Groupid]) REFERENCES [dbo].[tblGroup] ([GroupId]),
[Groupid] [int] FOREIGN KEY([usreId]) REFERENCES [dbo].[tbluser] ([usreId]))
//.cs code
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var sql = "select GroupId,GroupName from tblGroup";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds);
if (ds.Tables.Count > 0)
{
gUser.DataSource = ds;
gUser.DataBind();
}
unCheckUserGrid();
}
}
private void unCheckUserGrid()
{
for (int groupcount = 0; groupcount < gUser.Rows.Count; groupcount++) { CheckBox chkGroup = (CheckBox)gUser.Rows[groupcount].FindControl("chkUser"); chkGroup.Checked = false; } } protected void gUser_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { CheckBox chkGHeader = (CheckBox)e.Row.FindControl("chkUHeader"); chkGHeader.Attributes.Add("onclick", "return maintainState('" + chkGHeader.ClientID + "','" + gUser.ClientID + "');"); } if (e.Row.RowIndex >= 0)
{
CheckBox chkGroup = (CheckBox)e.Row.FindControl("chkUser");
chkGroup.Attributes.Add("onclick", "return uncheckHeader('" + gUser.ClientID + "');");
}
}
string sql = string.Empty;
private void saveall()
{
ArrayList arr = new ArrayList();
CheckBox chk;
Label lbl;
int i;
foreach (GridViewRow d in gUser.Rows)
{
chk = (CheckBox)d.FindControl("chkUser");
if (chk.Checked)
{
lbl = (Label)d.FindControl("lblGroupId");
arr.Add(lbl.Text);
}
}
string userId = txtUserName.Text.Trim().Replace("'", "''");
SqlDataAdapter adp = new SqlDataAdapter("Select usreId from tbluser where usreId='" + userId + "'", con);
DataSet ds = new DataSet();
adp.Fill(ds);
SqlCommand cmd ;
if (ds.Tables[0].Rows.Count > 0)
{
string UId = ds.Tables[0].Rows[0]["usreId"].ToString();
if (arr.Count > 0)
{
for (i = 0; i <= arr.Count - 1; i++) { sql += "insert into MapUserGroup(usreId,groupid) values('" + UId + "','" + arr[i] + "')"; } con.Open(); cmd = new SqlCommand(sql, con); cmd.ExecuteNonQuery(); con.Close(); unCheckUserGrid(); } } } protected void BtnSave_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("insert into tbluser(usreId) values('" + txtUserName.Text.Trim().Replace("'", "''") + "')", con); cmd.ExecuteNonQuery(); con.Close(); //after save in maping value saveall(); } //Design .aspx page table align="center" >
tr>
td>
span>UserName /span>
/td>
td>
asp:TextBox runat="server" ID="txtUserName" > /asp:TextBox>
/td>
/tr>
/table>
table align="center" >
tr>
td>
div style="overflow: auto">
asp:GridView ID="gUser" AutoGenerateColumns="False"
runat="server"
BackColor="White" BorderColor="#999999" BorderStyle="Double"
BorderWidth="1px" CellPadding="0" Width="100%" Font-Overline="False"
Font-Size="Small" HorizontalAlign="Center" onrowdatabound="gUser_RowDataBound" >
FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
RowStyle BackColor="#EEEEEE" ForeColor="Black" />
Columns>
asp:TemplateField>
HeaderTemplate>
asp:CheckBox ID="chkUHeader" runat="server" />
/HeaderTemplate>
ItemTemplate>
asp:CheckBox ID="chkUser" runat="server" />
/ItemTemplate>
ItemStyle Width="10%" />
/asp:TemplateField>
asp:TemplateField HeaderText="Group Id" HeaderStyle-HorizontalAlign="Left">
ItemTemplate>
asp:Label ID="lblGroupId" runat="server" Font-Size="100%" Text='<%# Bind("GroupId") %>' >
/ItemTemplate>
HeaderStyle HorizontalAlign="Left">
/asp:TemplateField>
asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign="Left">
ItemTemplate>
asp:Label ID="lblGroupName" Font-Size="100%" runat="server" Text='<%# Bind("GroupName") %>'>
/ItemTemplate>
HeaderStyle HorizontalAlign="Left">
/asp:TemplateField>
/Columns>
SelectedRowStyle BackColor="#008A8C" ForeColor="White" />
HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" BorderStyle="Double" Font-Size="Small"
HorizontalAlign="Left" />
AlternatingRowStyle BackColor="#DCDCDC" />
/asp:GridView>
/div>
/td>
/tr>
tr>
td align="right" >
asp:Button ID="BtnSave" runat="server" onclick="BtnSave_Click" Text="Save" />
/td>
/tr>
/table>
//.Js file
function maintainState(checkall,Grid)
{
var gvTable=document.getElementById(Grid);
var control=document.getElementById(checkall);
for(var i=1;i
{
if(control.checked==true)
{
gvTable.rows[i].cells[0].childNodes[0].checked=true;
}
else
{
gvTable.rows[i].cells[0].childNodes[0].checked=false;
}
}
}
function uncheckHeader(control)
{
document.getElementById(control).rows[0].cells[0].childNodes[0].checked=false;
}
//first table
CREATE TABLE tbluser(
[UId] [int] IDENTITY(1,1) NOT NULL,
[usreId] [varchar](100) CONSTRAINT [pkuser] PRIMARY KEY CLUSTERED ,
[UserName] [varchar](100)
)
//second table
CREATE TABLE tblGroup(
[GroupId] [int] IDENTITY(1,1)CONSTRAINT [pkGroup] PRIMARY KEY CLUSTERED ,
[GroupName] [varchar](100) )
//third table
CREATE TABLE MapUserGroup(
[usreId] [varchar](100) FOREIGN KEY([Groupid]) REFERENCES [dbo].[tblGroup] ([GroupId]),
[Groupid] [int] FOREIGN KEY([usreId]) REFERENCES [dbo].[tbluser] ([usreId]))
//.cs code
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Master"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var sql = "select GroupId,GroupName from tblGroup";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, con);
da.Fill(ds);
if (ds.Tables.Count > 0)
{
gUser.DataSource = ds;
gUser.DataBind();
}
unCheckUserGrid();
}
}
private void unCheckUserGrid()
{
for (int groupcount = 0; groupcount < gUser.Rows.Count; groupcount++) { CheckBox chkGroup = (CheckBox)gUser.Rows[groupcount].FindControl("chkUser"); chkGroup.Checked = false; } } protected void gUser_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { CheckBox chkGHeader = (CheckBox)e.Row.FindControl("chkUHeader"); chkGHeader.Attributes.Add("onclick", "return maintainState('" + chkGHeader.ClientID + "','" + gUser.ClientID + "');"); } if (e.Row.RowIndex >= 0)
{
CheckBox chkGroup = (CheckBox)e.Row.FindControl("chkUser");
chkGroup.Attributes.Add("onclick", "return uncheckHeader('" + gUser.ClientID + "');");
}
}
string sql = string.Empty;
private void saveall()
{
ArrayList arr = new ArrayList();
CheckBox chk;
Label lbl;
int i;
foreach (GridViewRow d in gUser.Rows)
{
chk = (CheckBox)d.FindControl("chkUser");
if (chk.Checked)
{
lbl = (Label)d.FindControl("lblGroupId");
arr.Add(lbl.Text);
}
}
string userId = txtUserName.Text.Trim().Replace("'", "''");
SqlDataAdapter adp = new SqlDataAdapter("Select usreId from tbluser where usreId='" + userId + "'", con);
DataSet ds = new DataSet();
adp.Fill(ds);
SqlCommand cmd ;
if (ds.Tables[0].Rows.Count > 0)
{
string UId = ds.Tables[0].Rows[0]["usreId"].ToString();
if (arr.Count > 0)
{
for (i = 0; i <= arr.Count - 1; i++) { sql += "insert into MapUserGroup(usreId,groupid) values('" + UId + "','" + arr[i] + "')"; } con.Open(); cmd = new SqlCommand(sql, con); cmd.ExecuteNonQuery(); con.Close(); unCheckUserGrid(); } } } protected void BtnSave_Click(object sender, EventArgs e) { con.Open(); SqlCommand cmd = new SqlCommand("insert into tbluser(usreId) values('" + txtUserName.Text.Trim().Replace("'", "''") + "')", con); cmd.ExecuteNonQuery(); con.Close(); //after save in maping value saveall(); } //Design .aspx page table align="center" >
tr>
td>
span>UserName /span>
/td>
td>
asp:TextBox runat="server" ID="txtUserName" > /asp:TextBox>
/td>
/tr>
/table>
table align="center" >
tr>
td>
div style="overflow: auto">
asp:GridView ID="gUser" AutoGenerateColumns="False"
runat="server"
BackColor="White" BorderColor="#999999" BorderStyle="Double"
BorderWidth="1px" CellPadding="0" Width="100%" Font-Overline="False"
Font-Size="Small" HorizontalAlign="Center" onrowdatabound="gUser_RowDataBound" >
FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
RowStyle BackColor="#EEEEEE" ForeColor="Black" />
Columns>
asp:TemplateField>
HeaderTemplate>
asp:CheckBox ID="chkUHeader" runat="server" />
/HeaderTemplate>
ItemTemplate>
asp:CheckBox ID="chkUser" runat="server" />
/ItemTemplate>
ItemStyle Width="10%" />
/asp:TemplateField>
asp:TemplateField HeaderText="Group Id" HeaderStyle-HorizontalAlign="Left">
ItemTemplate>
asp:Label ID="lblGroupId" runat="server" Font-Size="100%" Text='<%# Bind("GroupId") %>' >
/ItemTemplate>
HeaderStyle HorizontalAlign="Left">
/asp:TemplateField>
asp:TemplateField HeaderText="Group Name" HeaderStyle-HorizontalAlign="Left">
ItemTemplate>
asp:Label ID="lblGroupName" Font-Size="100%" runat="server" Text='<%# Bind("GroupName") %>'>
/ItemTemplate>
HeaderStyle HorizontalAlign="Left">
/asp:TemplateField>
/Columns>
SelectedRowStyle BackColor="#008A8C" ForeColor="White" />
HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" BorderStyle="Double" Font-Size="Small"
HorizontalAlign="Left" />
AlternatingRowStyle BackColor="#DCDCDC" />
/asp:GridView>
/div>
/td>
/tr>
tr>
td align="right" >
asp:Button ID="BtnSave" runat="server" onclick="BtnSave_Click" Text="Save" />
/td>
/tr>
/table>
//.Js file
function maintainState(checkall,Grid)
{
var gvTable=document.getElementById(Grid);
var control=document.getElementById(checkall);
for(var i=1;i
if(control.checked==true)
{
gvTable.rows[i].cells[0].childNodes[0].checked=true;
}
else
{
gvTable.rows[i].cells[0].childNodes[0].checked=false;
}
}
}
function uncheckHeader(control)
{
document.getElementById(control).rows[0].cells[0].childNodes[0].checked=false;
}
Wednesday, February 16, 2011
var datatype in C#
Declaring local variables implicitly has some restrictions; the variable must be initialized to some expression that can not be null.
var a= 10;
var z = "Rekha";
The primary reason for its existence is the introduction of anonymous types in C#
Another point to stress is that variable inference does not work for class level fields or method arguments or anywhere else other than for local variables in a method.
Advantages :
Less typing with no loss of functionality
Increases the type safety of your code. A foreach loop using an iteration variable which is typed to var will catch silently casts that are introduced with explicit types
Makes it so you don't have to write the same name twice in a variable declaration.
Some features, such as declaring a strongly typed anonymous type local variable, require the use of var
var a= 10;
var z = "Rekha";
The primary reason for its existence is the introduction of anonymous types in C#
Another point to stress is that variable inference does not work for class level fields or method arguments or anywhere else other than for local variables in a method.
Advantages :
Less typing with no loss of functionality
Increases the type safety of your code. A foreach loop using an iteration variable which is typed to var will catch silently casts that are introduced with explicit types
Makes it so you don't have to write the same name twice in a variable declaration.
Some features, such as declaring a strongly typed anonymous type local variable, require the use of var
Labels:
var datatype in C#
What is difference between Physical path and Virtual path?
Physical path is the path where ur original application has
stored(actual directory path of application).
virtual path is the link between physical path and
IIS..becoz to execute the webapplication in Physical
path,it needs to be mapped on to IIS(webserver) and this
logical link between physical path and IIS is called
virtual path
stored(actual directory path of application).
virtual path is the link between physical path and
IIS..becoz to execute the webapplication in Physical
path,it needs to be mapped on to IIS(webserver) and this
logical link between physical path and IIS is called
virtual path
Recursive method for Find all directory and inside the directory found all files
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
//Logic here in file
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries){
ProcessFile(fileName);
}
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
ProcessDirectory(subdirectory);
}
}
//call the Method
string FilePath = Request.PhysicalApplicationPath + "UploadFile";
if (Directory.Exists(FilePath))
{
ProcessDirectory(FilePath);
}
public static void ProcessFile(string path)
{
//Logic here in file
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries){
ProcessFile(fileName);
}
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
{
ProcessDirectory(subdirectory);
}
}
//call the Method
string FilePath = Request.PhysicalApplicationPath + "UploadFile";
if (Directory.Exists(FilePath))
{
ProcessDirectory(FilePath);
}
Subscribe to:
Posts (Atom)