LIKE US ON FACE BOOK

Friday, 22 February 2013

Uploading Single/Multiple Files in ASP.Net without using any Plug-in or Jquery

Here is most simplest way i found while working on .....
U guys no need to google further..I bet it..

create a file called Filename.aspx.cs 

write a this code 


<div>
        <input type="file" id="FileUpload1" multiple="multiple" runat="server"/>

        <asp:Button runat="server" ID="btnupload" Text="Upload"
            onclick="btnupload_Click"/>&nbsp;<br/>

        <asp:Label runat="server" ID="Label1"></asp:Label> 
</div>

Filename.aspx.cs

protected void btnupload_Click(object sender, EventArgs e)
    {
      string tempPath =          System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
       string savepath = Server.MapPath(tempPath);
        for(int i=0;i<Request.Files.Count;i++)
        {            
            HttpPostedFile hpf = Request.Files[i];
            string filename = hpf.FileName;
            hpf.SaveAs(savepath + @"\" + filename);
        }

    } 
Web.config

<appSettings>
    <add key ="FolderPath" value ="uploads"/>
</appSettings > 

Any Doubts regarding this please do ask...

Monday, 4 February 2013

READING AND ACCESSING DATA FROM XML IN ASP.NET

HERE TODAY AM GONNA GIVE U VERY SIMPLE CODE...
I DONT BELIV IT HAS TO BE EXPLAINED BECOZ..ITS VERY MUCH UNDERSTANDABLE..
STILL ILL COMMENT OUT POINTS..WAT'S HAPPENIN IN CODE...


CREATE A FILE WITH NAME AS follows:-

Xmlreadrite.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlinput.aspx.cs" Inherits="xmlinput" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
   
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       Employee Information<br />
       Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <br />
       Emp-Id:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       <br />
       Qualification:<asp:DropDownList ID="DropDownList1" runat="server"
           AutoPostBack="True">
           <asp:ListItem>--SELECT--</asp:ListItem>
           <asp:ListItem>MTEC</asp:ListItem>
           <asp:ListItem>BE</asp:ListItem>
           <asp:ListItem>BTEC</asp:ListItem>
           <asp:ListItem>MBBS</asp:ListItem>
           <asp:ListItem>BDS</asp:ListItem>
       </asp:DropDownList>
   
       <br />
       <asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" />
       <asp:Button ID="Button2" runat="server" Text="SHOW" onclick="Button2_Click" />
       <br />
       <br />
       <asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333">
           <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
           <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
           <ItemTemplate>
               <hr />
               Name:<%#DataBinder.Eval(Container.DataItem,"name")%><br />Emp-ID:<%#DataBinder.Eval(Container.DataItem,"Emp-Id")%>></a><br />
               Qualification:<%#DataBinder.Eval(Container.DataItem,"Qualification")%>><br />
           </ItemTemplate>
           <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
       </asp:DataList>
   
   </div>
   </form>
</body>
</html>

Employee.xml
<?xml version="1.0" encoding="utf-8"?>
<Employeeinformation>

</Employeeinformation>

Xmlreadrite.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;

public partial class xmlinput : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      
   }

here we retrive data from xml file to datalist by using dataset.

   private void BindDatalist()
   {
       XmlTextReader xmlreader=new XmlTextReader(Server.MapPath("Employee.xml"));
       DataSet da = new DataSet();
       da.ReadXml(xmlreader);
       xmlreader.Close();
       if (da.Tables.Count != 0)
       {
           DataList1.DataSource = da;
           DataList1.DataBind();
       }
       else
       {
           DataList1.DataSource = null;
           DataList1.DataBind();
       }
   }


here we insert the data to xml file by creating xml object and assignin it to our xml file.

   protected void Button1_Click(object sender, EventArgs e)
   {
       XmlDocument xmldoc = new XmlDocument();
       xmldoc.Load(Server.MapPath("Employee.xml"));
       XmlElement parentelement = xmldoc.CreateElement("Details");
       XmlElement name = xmldoc.CreateElement("name");
       name.InnerText = TextBox1.Text;
       XmlElement EmpId = xmldoc.CreateElement("Emp-Id");
       EmpId.InnerText = TextBox2.Text;
       XmlElement Qualification = xmldoc.CreateElement("Qualification");
       Qualification.InnerText = DropDownList1.SelectedItem.Text;
       parentelement.AppendChild(name);
       parentelement.AppendChild(EmpId);
       parentelement.AppendChild(Qualification);
       xmldoc.DocumentElement.AppendChild(parentelement);
       xmldoc.Save(Server.MapPath("Employee.xml"));
   }

here in this button click we call display function ,whic is binddatalist()

   protected void Button2_Click(object sender, EventArgs e)
   {
      
       BindDatalist();

   }
}