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