Saturday, April 7, 2012

Save the data into xml and display in gridview in asp.net



.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Request.MapPath("XMLFile.xml"));
            grvProducts.DataSource = ds.Tables[0];
            grvProducts.DataBind();
            ViewState["dtProducts"] = ds.Tables[0];
        }
    }   
    protected void btnSave_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)ViewState["dtProducts"];       
        dt.Rows.Add(txtPName.Text, txtPrice.Text, txtqty.Text, Convert.ToDateTime(txtSdate.Text).ToShortDateString());
        dt.AcceptChanges();
        dt.WriteXml(Request.MapPath("XMLFile.xml"));       
       
        grvProducts.DataSource = dt;
        grvProducts.DataBind();
    }



.aspx


  <table>
        <tr>
          <td>
            Product Name :
          </td>
           <td>
             <asp:TextBox ID="txtPName" runat="server"></asp:TextBox>
          </td>
        </tr>
         <tr>
           <td>
            Price :
          </td>
           <td>
             <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
          </td>
        </tr>
          <tr>
           <td>
            Quantity :
          </td>
           <td>
             <asp:TextBox ID="txtqty" runat="server"></asp:TextBox>
          </td>
        </tr>
          <tr>
           <td>
            Sale Date :
          </td>
           <td>
             <asp:TextBox ID="txtSdate" runat="server"></asp:TextBox>
          </td>
        </tr>
         <tr>
           <td>
           Total Price :
          </td>
           <td>
             <asp:Label ID="lblPrice" runat="server"></asp:Label>
          </td>
        </tr>
         <tr>          
           <td colspan="2">
             <asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
          </td>
        </tr>
      </table>
      <br />
      <asp:GridView ID="grvProducts" runat="server" AutoGenerateColumns="true">
       
      </asp:GridView>



XMLFile.xml


<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Product id="1">
    <Name>Test</Name>
    <Price>2000</Price>
    <Qty>2</Qty>
    <Date>12/2/2010</Date>
  </Product> 
  <Product>
    <Name>Mobile</Name>
    <Price>6500</Price>
    <Qty>3</Qty>
    <Date>5/9/2010</Date>
  </Product>
</DocumentElement>

Monday, April 2, 2012

Using Datalist in asp.net

 .aspx.

<asp:DataList id="dlpoolers" runat="server"   CellPadding="0" RepeatDirection="Horizontal" RepeatColumns="4"  CellSpacing="0" Width='100%'>
      <ItemTemplate>           
          &nbsp; <asp:Label ID="lblcid" runat="server" Text='<%#Eval("iCId")%>' Visible="false" align="center"></asp:Label>
          <asp:Label ID="lblphotopaths" runat="server" Text='<%#Eval("vPhotoPath")%>' Visible="false"></asp:Label>
          <asp:Label ID="lblphoto1" runat="server"></asp:Label>
          <asp:Label ID="lblname" Text='<%#Eval("vName")%>' runat="server"></asp:Label>
  <hr />
</ItemTemplate>
</asp:DataList>

.aspx.cs
protected void dlview_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem))
        {
            try
            {
                string path1 = "";
                string name = "";
                string links = "";
                string strfname = "";
                string strlname = "";
                string straddr = "";
                Label lbllinks = e.Item.FindControl("lblcpownerid") as Label;
                links = lbllinks.Text;
               
                // fist image
                Label lblpath1 = e.Item.FindControl("lblphotopath") as Label;
                path1 = lblpath1.Text;
                Label lblphoto = e.Item.FindControl("lblphoto") as Label;
               
                if (path1 != "")
                {
                  
                    lblphoto.Text = "<b><a href='#'><img src='" + sAppDomain + "/" + path1 + "' height='120' width='120' border='0' /></a></b>";
                    lblphoto.Attributes.Add("OnClick", "getphotos('" + links + "');");
                }
                else
                {

                    lblphoto.Text = "<b><a href='#'><img src='" + Application["Root"] + "Images/nopic.png' height='120' width='120' border='0' /></a></b>";
                    lblphoto.Attributes.Add("OnClick", "getphotos('" + links + "');");                                  }
                // end of first image              

            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
            }      

        }
    }

Page Index Changing In Gridview


.aspx

    <asp:GridView ID="gvcardetails" AutoGenerateColumns="false" PageSize="20" OnPageIndexChanging="gvcardetails_PageIndexChanging" AllowPaging="True" runat="server" Width="100%" cellpadding="5" cellspacing="1" gridlines="none" bgcolor="#ffffff">
           <Columns>
           <asp:BoundField HeaderText="Id" DataField="iCarPoolOwnerId" Visible="false" ReadOnly="true" />
                        <asp:BoundField HeaderText="CarNo" DataField="CarNo" /> 
                        <asp:BoundField HeaderText="Start Point" DataField="vStartPoint" /> 
                  </Columns>
</asp:GridView>

.aspx.cs
protected void gvcardetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       gvcardetails.PageIndex = e.NewPageIndex;
       bindgrid();  //method for binding Grid
    }

Excel Upload in asp.net

This is in web.config
<connectionStrings>
        <add name="excelo3cnstring" connectionString="Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}';" providerName="OleDb;"/>
        <add name="excelo7cnstring" connectionString="Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}';" providerName="OleDb;"/>
    </connectionStrings>

Microsoft.Jet.OLEDB.4.0 is for MSOffice 2003 and 
Microsoft.ACE.OLEDB.12.0 is for MSOffice 2007

.aspx.cs
protected void btnUpload_Click(object sender, EventArgs e)
        {

            if (FileUpload1.HasFile)
            {

                string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
                string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
                string FilePath = Server.MapPath(FolderPath + FileName);
                FileUpload1.SaveAs(FilePath);

                Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);

            }

        }

        private void Import_To_Grid(string FilePath, string Extension, string isHDR)
        {

            string conStr = "";

            switch (Extension)
            {

                case ".xls": //Excel 97-03

                    conStr = ConfigurationManager.ConnectionStrings["excelo3cnstring"]

                             .ConnectionString;

                    break;

                case ".xlsx": //Excel 07

                    conStr = ConfigurationManager.ConnectionStrings["excelo7cnstring"]

                              .ConnectionString;

                    break;

            }

            conStr = String.Format(conStr, FilePath, isHDR);

            OleDbConnection con
Excel= new OleDbConnection(conStr);

            OleDbCommand cmdExcel = new OleDbCommand();

            OleDbDataAdapter odadp = new OleDbDataAdapter();

            DataTable dt = new DataTable();

            cmdExcel.Connection = connExcel;

            //Get the name of First Sheet

           
conExcel.Open();

            DataTable dtExcelSchema;

            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
           
conExcel.Close();

            //Read Data from First Sheet

           
conExcel.Open();
            cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
           
odadp.SelectCommand = cmdExcel;
           
odadp.Fill(dt);
           
conExcel.Close();
            //Bind Data to GridView

            GridView1.Caption = Path.GetFileName(FilePath);
            GridView1.DataSource = dt;     
            GridView1.DataBind();

        }

Protected by Copyscape Plagiarism Software