Wednesday, July 16, 2014

Binding Dropdowns Using Single Method.. and At A time inserting Dataset to Database... Xml tag Creation... temp table creation


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        XmlDocument XmlDocObj = new XmlDocument();
        XmlDocObj.Load(Server.MapPath("BookStore.xml"));
        XmlNode RootNode = XmlDocObj.SelectSingleNode("bookstore");
        XmlNode bookNode = RootNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "book", ""));
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Title", "")).InnerText = tbTitle.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Author", "")).InnerText = tbAuthor.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Year", "")).InnerText = tbYear.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Price", "")).InnerText = tbPrice.Text;

        XmlDocObj.Save(Server.MapPath("BookStore.xml"));

        gridDataBind();
        //bind();

    }

    //private void bind()
    //{


    //}

    public void sessionusername(DataSet ds)
    {
        Session["username"] = ds;

        DataSet ds1 = (DataSet)Session["username"];
    }



    public void gridDataBind()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("BookStore.xml"));
        gvBookStoreRecords.DataSource = ds;
        gvBookStoreRecords.DataBind();
        SqlConnection con = new SqlConnection("Data Source=KCLINK-45-PC\\SQLEXPRESS;Initial Catalog=naresh;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        con.Open();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Sp_title1";
      
        //SqlDataAdapter dap = new SqlDataAdapter(cmd);
        //DataSet ds = new DataSet();
        //dap.Fill(ds);
        cmd.Parameters.AddWithValue("@datacontainer", ds.Tables[0]);
        cmd.ExecuteNonQuery();
        filldropdown(drp, ds,"Title","Price");
        filldropdown(DropDownList1, ds,"Author","Price");
        filldropdown(DropDownList2, ds,"Year","Price");


    }

    public void filldropdown(DropDownList drp, DataSet ds,string text,string value)
    {
        drp.DataSource = ds;
        drp.DataTextField = text;
        drp.DataValueField = value;
        drp.DataBind();
    }



}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
        <br />
        <table style="width: 100%;">
            <tr>
                <td class="auto-style1">
                    Title:
                </td>
                <td>
                    <asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Author:
                </td>
                <td>
                    <asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Year:
                </td>
                <td>
                    <asp:TextBox ID="tbYear" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Price:
                </td>
                <td>
                    <asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                </td>
            </tr>

            <tr>
             <td>
               <asp:DropDownList ID="drp" runat="server"></asp:DropDownList>
                <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                 <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>

 <asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList>
                  <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>
             </td>
            </tr>
        </table>
        <asp:GridView ID="gvBookStoreRecords" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />


          
        </asp:GridView>
    </div>
    </form>
</body>
</html>

<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book>
    <Title>adsd</Title>
    <Author>dsfffg</Author>
    <Year>2323</Year>
    <Price>2323</Price>
  </book>
  <book>
    <Title>jhgj</Title>
    <Author>ersfser</Author>
    <Year>56547</Year>
    <Price>5466768</Price>
  </book>
  <book>
    <Title>jhgj</Title>
    <Author>ersfser</Author>
    <Year>56547</Year>
    <Price>2323</Price>
  </book>
  <book>
    <Title>nbhb</Title>
    <Author>gh</Author>
    <Year>6767</Year>
    <Price>766</Price>
  </book>
  <book>
    <Title>.net</Title>
    <Author>naresh</Author>
    <Year>2014</Year>
    <Price>1000</Price>
  </book>
  <book>
    <Title>.net</Title>
    <Author>naresh</Author>
    <Year>2014</Year>
    <Price>1000</Price>
  </book>
  <book>
    <Title>jkd</Title>
    <Author>fgs</Author>
    <Year>dfgfd</Year>
    <Price>dfgdfg</Price>
  </book>
  <book>
    <Title>naresh</Title>
    <Author>sdfa</Author>
    <Year>asdfasd</Year>
    <Price>sadfasdf</Price>
  </book>
  <book>
    <Title>cghbn</Title>
    <Author>vbnc</Author>
    <Year>vbncv</Year>
    <Price>vbncv</Price>
  </book>
  <book>
    <Title>gfhd</Title>
    <Author>fghd</Author>
    <Year>fdghs</Year>
    <Price>dfsg</Price>
  </book>
  <book>
    <Title>gbdcnc</Title>
    <Author>fbnf</Author>
    <Year>vbndf</Year>
    <Price>vbnv</Price>
  </book>
  <book>
    <Title>bncvb</Title>
    <Author>bvnc</Author>
    <Year>vbncbn</Year>
    <Price>vcbnc</Price>
  </book>
  <book>
    <Title>nnn</Title>
    <Author>nnn</Author>
    <Year>nnn</Year>
    <Price>nnnn</Price>
  </book>
  <book>
    <Title>hh</Title>
    <Author>hh</Author>
    <Year>hh</Year>
    <Price>hh</Price>
  </book>
  <book>
    <Title>fgbfg</Title>
    <Author>cvbcv</Author>
    <Year>cvbc</Year>
    <Price>cvbc</Price>
  </book>
  <book>
    <Title>.net</Title>
    <Author>naresh</Author>
    <Year>2014</Year>
    <Price>1000</Price>
  </book>
  <book>
    <Title>esdfs</Title>
    <Author>sdfsd</Author>
    <Year>sdfsd</Year>
    <Price>sdfsdf</Price>
  </book>
  <book>
    <Title>dfgdsfg</Title>
    <Author>sdfgdsfg</Author>
    <Year>sdfgdsfg</Year>
    <Price>345435</Price>
  </book>
  <book>
    <Title>asdasd</Title>
    <Author>asdasd</Author>
    <Year>asdasd</Year>
    <Price>23423423</Price>
  </book>
  <book>
    <Title>dfghdfgh</Title>
    <Author>hjkhjk</Author>
    <Year>gvbnvc</Year>
    <Price>456456</Price>
  </book>
  <book>
    <Title>r</Title>
    <Author>sdfasdfg</Author>
    <Year>sdfgdsfg</Year>
    <Price>34545</Price>
  </book>
  <book>
    <Title>dfgdfsg</Title>
    <Author>sdfgsdfg</Author>
    <Year>dsfgdsfg</Year>
    <Price>345435</Price>
  </book>
  <book>
    <Title>asdfasdf</Title>
    <Author>asdfsadf</Author>
    <Year>asdfsadf</Year>
    <Price>23232</Price>
  </book>
</bookstore>
-----------------------------------------------------------------------------------
create table title(Title varchar(50),Author varchar(50),Year varchar(50),Price varchar(50))



create type title1 as table(Title varchar(50),Author varchar(50),Year varchar(50),Price varchar(50))

create procedure Sp_title
@datacontainer as title1 readonly
as
begin
insert into title select * from @datacontainer
end
go

select * from title


Tuesday, July 15, 2014

Temporary Table -SQL Server

This example Shows you how to create temporary table in SqlServer, Insert values in them, Check their existence and finally Drop them.

Create Temp Table

This one is declared as Global Temporary Table
create table ##YourTempTable (YourTableField1 bigint,YourTableField2 varchar(50))

This one is declared as Local Temporary Table
create table #YourTempTable (YourTableField1 bigint,YourTableField2 varchar(50))


Insert Values in Your Temporary Table

insert into ##YourTempTable 
SELECT * FROM  YourDesiredTable

Check Existence of Temporary table


if exists(select * from tempdb.dbo.sysobjects where ID=object_ID(N'tempdb..##YourTempTable  '))


Check Existence n Then Drop Temporary Table


if exists(select * from tempdb.dbo.sysobjects where ID=object_ID(N'tempdb..##YourTempTable  '))
BEGIN
DROP TABLE ##YourTempTable 

END

Show Total in GridView Footer


Design:

<asp:TemplateField HeaderText="Total Pcs">
 <ItemTemplate>
          <asp:Label ID="lbltotpcs" runat="server"  Text='<%# Bind("totpcs") %>'></asp:Label>
 </ItemTemplate>
 <FooterTemplate>
        Total : <asp:Label ID="lbltotqty" runat="server" Text="Label"></asp:Label>
</FooterTemplate>
</asp:TemplateField>


Populate the RowDataBound event of Gridview where you have to do the main calculation.
int totqty=0;
protected void gvCrate_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label totpcs = (Label)e.Row.FindControl("lbltotpcs");
            totqty = totqty + (Convert.ToInt32(totpcs.Text));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            ((Label)e.Row.FindControl("lbltotqty")).Text = totqty.ToString();
            TxtTotalpcs.Value = totqty.ToString();
        }
    }


NOTE:

Enable  "ShowFooter" as True in Gridview's property.


Add Month To any Given Date

using System.Globalization;

protected void DropEMISchedule_SelectedIndexChanged(object sender, EventArgs e)

{

DateTime dt;

int calcy = Convert.ToInt32(DropEMISchedule.SelectedValue) * Convert.ToInt32(txttenure.Text);

if (DateTime.TryParseExact(txtEMIStartDate.Text, "dd/MM/yyyy",new CultureInfo("en-US"),  System.Globalization.DateTimeStyles.None, out dt))

   {

     TextBoxend.Text = "";

     DateTime dtshow = dt.Date.AddMonths(calcy);
  
     TextBoxend.Text = (dtshow).ToString("dd/MM/yyyy");

    }
 }