Wednesday, July 16, 2014

Dynamically Creating Registration forms Based On Tables ...

<%@ 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>
        <asp:Button ID="btndpm" Text="SPM" runat="server" OnClick="btndpm_Click" />
        <asp:Button ID="btnpm" Text="PM" runat="server" />
        <asp:Button ID="btnsv" Text="SV" runat="server" />
        <asp:Button ID="btndv" Text="Dev" runat="server" />
        <%--<asp:Panel ID="panel" runat="server" Height="446px"></asp:Panel>--%>
    </div>
    <p>
    </p>
    </form>
</body>
</html>
 =========================================================================

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.Data.SqlClient;
using System.Configuration;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    public string con = ConfigurationManager.ConnectionStrings["con"].ToString();
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new SqlConnection(con);
        cmd = new SqlCommand("select * from sub", cn);
        cn.Open();
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
    }
    protected void btndpm_Click(object sender, EventArgs e)
    {
        //int i;
        //i = ds.Tables[0].Rows[0].Columns[.ToString();
        if(ds.Tables[0].Rows[0][0].ToString()=="1")
        {
            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                if (ds.Tables[0].Rows[0][i].ToString() != "")
                {
                    TextBox t = new TextBox();
                    t.ID = "tt" + i;
                    form1.Controls.Add(new LiteralControl("<br/>"));
                    form1.Controls.Add(t);
                  
                 
                    Label l = new Label();
                     l.ID = "ll" + i;
                  //   l.Attributes.Add("style", "font-size:12px;padding:10px;");
                     //l.Style["Position"] = "Absolute";
                     l.Style["Top"] = "2px";
                     l.Style["Left"] = "1000px";
                    l.Text = ds.Tables[0].Columns[i].ColumnName;
                    form1.Controls.Add(l);

                    
                }
             
            }
        }
        Button btnSubmit = new Button();
        btnSubmit.ID = "btnSubmit";
        btnSubmit.Text = "Submit";
        btnSubmit.Click += new System.EventHandler(btnSubmit_click);
        form1.Controls.Add(btnSubmit);
    }
    protected void btnSubmit_click(object sender, EventArgs e)
    {
   
   
    }
}
==========================================================================
SQL 

USE Naresh
GO

/****** Object:  Table [dbo].[main]    Script Date: 07/17/2014 11:32:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[main](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [type] [varchar](50) NOT NULL,
 CONSTRAINT [PK_main] PRIMARY KEY CLUSTERED
(
      [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO
---------------------------------2nd table
USE Naresh
GO

/****** Object:  Table [dbo].[sub]    Script Date: 07/17/2014 11:31:30 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[sub](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [firstname] [varchar](50) NULL,
      [lastname] [varchar](50) NULL,
      [username] [varchar](50) NULL,
      [password] [varchar](50) NULL,
      [project] [varchar](50) NULL,
      [spmid] [int] NULL,
      [pmid] [int] NULL,
      [supervisorid] [int] NULL,
      [developerid] [int] NULL,
      [location] [varchar](50) NULL,
 CONSTRAINT [PK_sub] PRIMARY KEY CLUSTERED
(
      [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

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