Tuesday, January 31

Gridview ToolTip View

Source Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="finalpopUP.aspx.cs" Inherits="finalpopUP" %>

<!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 id="Head1" runat="server">
    <title>Om Namah Shivaya</title>
  
</head>
<body>
    <form id="form1" runat="server">
       
       
      
           <div>
          
                          
                                        <asp:GridView Width="100%" AllowPaging="True" ID="gvCustomers"
                                             runat="server" ShowHeader="true" AutoGenerateColumns="false"
                                            onrowdatabound="gvCustomers_RowDataBound" >
                                            <Columns>
                                                <asp:TemplateField HeaderText="CustomerID:" ItemStyle-Width="20px" >
                                                    <ItemTemplate>
                                                       
                                                      
                                                          <%--  <img src="~/Assets/img/plus.png" id="MyControlImage" runat="server" onmouseover="ShowToolTip(this);" onmouseout="HideToolTip(this);"
                                onmousedown="HideToolTip(this);" />--%>
                                                            <asp:Label ID="MyControlImage" runat="server"  onmouseover="ShowToolTip(this);" onmouseout="HideToolTip(this);"
                                onmousedown="HideToolTip(this);"  Text='<%#Eval("CustomerID")%>'></asp:Label>
                                                               
                                                       
                                                      
                                                        <div id="MyControlDiv" runat="server" style="position:absolute; display:none; background-color:Yellow; border:1px solid black; padding:3px;">
                                                            <asp:GridView  CssClass="grid" ID="gvOrders" AutoGenerateColumns="false"
                                                                runat="server" ShowHeader="true" EnableViewState="false">
                                                                <RowStyle CssClass="row" />
                                                                <AlternatingRowStyle CssClass="altrow" />
                                                                <Columns>
                                                                    <asp:TemplateField ItemStyle-CssClass="rownum">
                                                                        <ItemTemplate>
                                                                            <%# Container.DataItemIndex + 1 %>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
                                                                     <asp:BoundField HeaderText="CustomerID" DataField="CustomerID"
                                                                        ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                                                                    <asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="100px" />
                                                                    <asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="110px" />
                                                                    <asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
                                                                        ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                                                                    <asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="100px" />
                                                                </Columns>
                                                            </asp:GridView>
                                                          
                                                        </div>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:BoundField HeaderText="Company Name" DataField="CompanyName" ItemStyle-Width="20px" />
                                                                    <asp:BoundField HeaderText="TotalOrders" DataField="TotalOrders"
                                                                        ItemStyle-Width="20px" />
                                                                  
                                            </Columns>
                                        </asp:GridView>
                                   
                       
          
        </div>
       
        <script type="text/javascript">
            function ShowToolTip(obj) {
                var controlID = obj.id.replace('Image', 'Div');
                document.getElementById(controlID).style.display = 'block';
            }
            function HideToolTip(obj) {
                var controlID = obj.id.replace('Image', 'Div');
                document.getElementById(controlID).style.display = 'none';
            }
    </script>
    </form>
</body>
</html>
Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class finalpopUP : System.Web.UI.Page
{
    class Product
    {
        public string Name { get; set; }
        public int CategoryID { get; set; }
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string TotalOrders { get; set; }


    }

    class Category
    {
        public string CustomerID { get; set; }
        public string Name { get; set; }
        public int ID { get; set; }
        public string OrderID { get; set; }
        public string OrderDate { get; set; }
        public string RequiredDate { get; set; }
        public string Freight { get; set; }
        public string ShippedDate { get; set; }

    }

    // Specify the first data source.
    List<Category> categories = new List<Category>()
        {
            new Category(){Name="Beverages", ID=001,OrderID="s1",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish",CustomerID="shiba1",ShippedDate="11/12/2011"},
            new Category(){ Name="Condiments", ID=002,OrderID="s2",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish2",CustomerID="shiba2",ShippedDate="11/12/2011"},
            new Category(){ Name="Vegetables", ID=003,OrderID="s3",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish3",CustomerID="shiba3",ShippedDate="11/12/2011"},
            new Category() {  Name="Grains", ID=004,OrderID="s4",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish4",CustomerID="shiba4",ShippedDate="11/12/2011"},
            new Category() {  Name="Fruit", ID=005,OrderID="s5",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish5",CustomerID="shiba5",ShippedDate="11/12/2011"}           
        };

    // Specify the second data source.
    List<Product> products = new List<Product>()
       {
          new Product{Name="Cola",  CategoryID=001,CustomerID="shiba1",CompanyName="swash convergence",TotalOrders="210"},
          new Product{Name="Tea",  CategoryID=001,CustomerID="shiba2",CompanyName="swash convergence",TotalOrders="212"},
          new Product{Name="Mustard", CategoryID=002,CustomerID="shiba3",CompanyName="swash convergence",TotalOrders="213"},
          new Product{Name="Pickles", CategoryID=002,CustomerID="shiba4",CompanyName="swash convergence",TotalOrders="214"},
          new Product{Name="Carrots", CategoryID=003,CustomerID="shiba5",CompanyName="swash convergence",TotalOrders="215"},
          new Product{Name="Bok Choy", CategoryID=003,CustomerID="shiba6",CompanyName="swash convergence",TotalOrders="216"},
          new Product{Name="Peaches", CategoryID=005,CustomerID="shiba7",CompanyName="swash convergence",TotalOrders="217"},
          new Product{Name="Melons", CategoryID=005,CustomerID="shiba8",CompanyName="swash convergence",TotalOrders="218"},
        };
    protected void Page_Load(object sender, EventArgs e)
    {
        gvCustomers.DataSource = products;
        gvCustomers.DataBind();
    }
    protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView gv = (GridView)e.Row.FindControl("gvOrders");
            gv.DataSource = from n in categories
                            where n.CustomerID == DataBinder.Eval(e.Row.DataItem, "CustomerID")
                            select n;
            gv.DataBind();

        }
    }
}


Thanks Shibashish mohanty

Div Like A Ajax TabPanel

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

<!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>
    <script language="JavaScript">
        function setVisibility(id, visibility) {
            document.getElementById(id).style.display = visibility;
        }
</script>

    <script type="text/javascript">
        function ShowToolTip(obj) {
            var controlID = obj.id.replace('Image', 'Div');
            document.getElementById(controlID).style.display = 'block';
        }
        function HideToolTip(obj) {
            var controlID = obj.id.replace('Image', 'Div');
            document.getElementById(controlID).style.display = 'none';
        }
    </script>
    <script type="text/javascript">
        function ShowToolTip1(obj) {
            var controlID = obj.id.replace('Image1', 'Div1');
            document.getElementById(controlID).style.display = 'block';

        }
        function HideToolTip1(obj) {
            var controlID = obj.id.replace('Image1', 'Div1');
            document.getElementById(controlID).style.display = 'none';
        }
    </script>
    <script type="text/javascript">
        function ShowToolTip2(obj) {
            var controlID2 = obj.id.replace('Image2', 'Div2');
            document.getElementById(controlID2).style.display = 'block';
        }
        function HideToolTip2(obj) {
            var controlID2 = obj.id.replace('Image2', 'Div2');
            document.getElementById(controlID2).style.display = 'none';
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
  
    <div>
    <table><tr><td><div style="width: 31px">
        <asp:Image ID="MyControlImage"
            onclick="ShowToolTip(this);setVisibility('MyControlDiv1', 'none');setVisibility('MyControlDiv2', 'none');"  
            ImageUrl="~/Image/BrowserImage.png" Height="28px" Width="25px" runat="server" />
    </div></td><td><div style="width: 31px">
        <asp:Image ImageUrl="~/Image/shibashish.jpg" Height="28px" Width="25px"
                ID="MyControlImage1" runat="server"
                onclick="ShowToolTip1(this);setVisibility('MyControlDiv', 'none');setVisibility('MyControlDiv2', 'none');" />
   
    </div></td><td><div style="width: 31px">
        <asp:Image ID="MyControlImage2"
                onclick="ShowToolTip2(this);setVisibility('MyControlDiv1', 'none');setVisibility('MyControlDiv', 'none');"
                ImageUrl="~/Image/BrowserImage.png" Height="28px" Width="25px" runat="server" />
   
    </div></td></tr></table>
    



   
    
    </div>
    <div><table><tr>
    
   
   
    <td>
    <div id="MyControlDiv" runat="server" style="position:absolute; background-color:Gray;display:none; border:1px solid black; padding:3px;margin-left:10px;margin-top:-6px;">
    <table><tr>
   
    <td>
    <asp:GridView AllowPaging="True" ID="gvCustomers"
                                             runat="server" ShowHeader="true" AutoGenerateColumns="false">
                                            <Columns>
                                              
                                                              
                                                                    <asp:TemplateField ItemStyle-CssClass="rownum">
                                                                        <ItemTemplate>
                                                                            <%# Container.DataItemIndex + 1 %>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
                                                                     <asp:BoundField HeaderText="CustomerID" DataField="CustomerID"
                                                                        ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                                                                    <asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="100px" />
                                                                    <asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="110px" />
                                                                    <asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
                                                                        ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                                                                    <asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="100px" />
                                                              
                                                 
                                              
                                                                  
                                            </Columns>
                                        </asp:GridView>
    </td>
    <td valign="top">

        <asp:Image ID="Image1" runat="server" onclick="setVisibility('MyControlDiv', 'none');" Height="21px"
            ImageUrl="~/Image/CloseImage.jpg" Width="16px" />
    </td>
    </tr></table>
      
       
    </div>
    </td>
    <td>
    <div id="MyControlDiv1" runat="server" style="position:absolute;display:none; background-color:Fuchsia; border:1px solid black;padding:3px; margin-left:40px;margin-top:-6px;">
     <table><tr>
   
    <td>
       <asp:GridView AllowPaging="True" ID="GridView1"
                                             runat="server" ShowHeader="false" AutoGenerateColumns="false">
                                            <Columns>
                                              
                                                              
                                                                    <asp:TemplateField ItemStyle-CssClass="rownum">
                                                                        <ItemTemplate>
                                                                            <%# Container.DataItemIndex + 1 %>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
                                                                     <asp:BoundField HeaderText="CustomerID" DataField="CustomerID"
                                                                        ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                                                                    <asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="100px" />
                                                                    <asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="110px" />
                                                                 
                                                              
                                                 
                                              
                                                                  
                                            </Columns>
                                        </asp:GridView>
        </td>
    <td valign="top">

        <asp:Image ID="Image2" runat="server" onclick="setVisibility('MyControlDiv1', 'none');" Height="21px"
            ImageUrl="~/Image/CloseImage.jpg" Width="16px" />
    </td>
    </tr></table>
    </div>
    </td>
   
    <td>
    <div id="MyControlDiv2" runat="server" style="position:absolute;display:none; background-color:Lime; border:1px solid black; padding:3px;margin-left:70px;margin-top:-6px;">
        <table><tr>
   
    <td>
       <asp:GridView AllowPaging="True" ID="GridView2"
                                             runat="server" ShowHeader="true" AutoGenerateColumns="false">
                                            <Columns>
                                              
                                                              
                                                                    <asp:TemplateField ItemStyle-CssClass="rownum">
                                                                        <ItemTemplate>
                                                                            <%# Container.DataItemIndex + 1 %>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                   
                                                                    <asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="110px" />
                                                                    <asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
                                                                        ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
                                                                    <asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
                                                                        ItemStyle-Width="100px" />
                                                              
                                                 
                                              
                                                                  
                                            </Columns>
                                        </asp:GridView>
        </td>
    <td valign="top">

        <asp:Image ID="Image3" runat="server" onclick="setVisibility('MyControlDiv2', 'none');" Height="21px"
            ImageUrl="~/Image/CloseImage.jpg" Width="16px" />
    </td>
    </tr></table> 
    </div>
    </td></tr></table></div>
   
    </form>
</body>
</html>
CodeBehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Facebook : System.Web.UI.Page
{
    class Product
    {
        public string Name { get; set; }
        public int CategoryID { get; set; }
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string TotalOrders { get; set; }


    }

    class Category
    {
        public string CustomerID { get; set; }
        public string Name { get; set; }
        public int ID { get; set; }
        public string OrderID { get; set; }
        public string OrderDate { get; set; }
        public string RequiredDate { get; set; }
        public string Freight { get; set; }
        public string ShippedDate { get; set; }

    }

    // Specify the first data source.
    List<Category> categories = new List<Category>()
        {
            new Category(){Name="Beverages", ID=001,OrderID="s1",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish",CustomerID="shiba1",ShippedDate="11/12/2011"},
            new Category(){ Name="Condiments", ID=002,OrderID="s2",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish2",CustomerID="shiba2",ShippedDate="11/12/2011"},
            new Category(){ Name="Vegetables", ID=003,OrderID="s3",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish3",CustomerID="shiba3",ShippedDate="11/12/2011"},
            new Category() {  Name="Grains", ID=004,OrderID="s4",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish4",CustomerID="shiba4",ShippedDate="11/12/2011"},
            new Category() {  Name="Fruit", ID=005,OrderID="s5",OrderDate="22/2/1988",RequiredDate="2/11/2012",Freight="shibashish5",CustomerID="shiba5",ShippedDate="11/12/2011"}           
        };

    // Specify the second data source.
    List<Product> products = new List<Product>()
       {
          new Product{Name="Cola",  CategoryID=001,CustomerID="shiba1",CompanyName="swash convergence",TotalOrders="210"},
          new Product{Name="Tea",  CategoryID=001,CustomerID="shiba2",CompanyName="swash convergence",TotalOrders="212"},
          new Product{Name="Mustard", CategoryID=002,CustomerID="shiba3",CompanyName="swash convergence",TotalOrders="213"},
          new Product{Name="Pickles", CategoryID=002,CustomerID="shiba4",CompanyName="swash convergence",TotalOrders="214"},
          new Product{Name="Carrots", CategoryID=003,CustomerID="shiba5",CompanyName="swash convergence",TotalOrders="215"},
          new Product{Name="Bok Choy", CategoryID=003,CustomerID="shiba6",CompanyName="swash convergence",TotalOrders="216"},
          new Product{Name="Peaches", CategoryID=005,CustomerID="shiba7",CompanyName="swash convergence",TotalOrders="217"},
          new Product{Name="Melons", CategoryID=005,CustomerID="shiba8",CompanyName="swash convergence",TotalOrders="218"},
        };
    protected void Page_Load(object sender, EventArgs e)
    {
        gvCustomers.DataSource = categories;
        gvCustomers.DataBind();
        GridView1.DataSource = categories;
        GridView1.DataBind();
        GridView2.DataSource = categories;
        GridView2.DataBind();
    }
}


Thanks shibashish mohanty 

Saturday, January 28

Dynamically Create tables and columns in sqlserver database by using asp.net c# code

Page source:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AnotherTest.aspx.cs" Inherits="AnotherTest" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 26%;
            border: 10px solid #800000;
            background-color: #666666;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <table class="style1"
            style="font-size: medium; font-weight: bold; color: #FFFFFF; margin-left: 0px;">
            <tr>
                <td>
                    Table Name:</td>
                <td>
                    <asp:TextBox ID="TxtTableName" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Column Name:</td>
                <td>
                    <asp:TextBox ID="TxtColumnName" runat="server" Width="200px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td align="left">
                    <asp:ImageButton ID="ImageButton1" runat="server"
                        ImageUrl="~/Image/mc_create_button.gif" onclick="ImageButton1_Click" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>


CodeBehind:-

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;

public partial class AnotherTest : System.Web.UI.Page
{ string connc = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString();

      

    protected void Page_Load(object sender, EventArgs e)
        {
          
         }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection(connc);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + TxtTableName.Text + "'", sqlcon);
        DataSet ds = new DataSet();
        da.Fill(ds);

        foreach (DataTable dt in ds.Tables)
        {
            // checking whether the table selected from the dataset exists in the database or not

            string exists = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '"+TxtTableName.Text+"'", sqlcon);
                sqlcon.Open();
                exists = cmd.ExecuteScalar().ToString();
            }
            catch (Exception exce)
            {
                exists = null;
            }

            // if does not exist

            if (exists == null)
            {
                // selecting each column of the datatable to create a table in the database

                foreach (DataColumn dc in dt.Columns)
                {
                    if (exists == null)
                    {
                        SqlCommand createtable = new SqlCommand("CREATE TABLE " + TxtTableName.Text + " (" + TxtTableName.Text.Substring(0, 1) + "ID" + " varchar(MAX))", sqlcon);
                        createtable.ExecuteNonQuery();
                        exists = dt.TableName;
                    }
                    else
                    {
                        SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD "+TxtColumnName.Text+" varchar(MAX)", sqlcon);
                        addcolumn.ExecuteNonQuery();
                        break;
                    }
                }

                // copying the data from datatable to database table

                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = "rama";
                //    bulkcopy.WriteToServer(dt);
                //}
            }
            // if table exists, just copy the data to the destination table in the database

            else
            {
                SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " varchar(MAX)", sqlcon);
                addcolumn.ExecuteNonQuery();
                break;
                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = dt.TableName;
                //    bulkcopy.WriteToServer(dt);
                //}
            }
        } 
    }
}


Web.config

<connectionStrings>
    <add name="CONN" connectionString="Server=SWASH-DBS\SWASHSQLINT;Database=Test;Uid=ken;pwd=kc@2011" providerName="System.Data.SqlClient"/>
        <!--<add name="CONN" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Shibashish\Desktop\SANJAY\UsesofWebNotification\App_Data\Database.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>-->
        <!--<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>-->
    </connectionStrings>

Page View:-


Another Type:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AnotherTest.aspx.cs" Inherits="AnotherTest" %>

<!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>
    <h1 align="center" style="font-weight: bold; color: #800000">Shibashish mohanty</h1>
    <style type="text/css">
        .style1
        {
            width: 36%;
            border: 10px solid #800000;
            background-color: #666666;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <table class="style1"
            style="font-size: medium; font-weight: bold; color: #FFFFFF; margin-left: 0px;">
            <tr>
                <td>
                    Table Name:</td>
                <td>
                    <asp:TextBox ID="TxtTableName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Column Name:</td>
                <td>
                    <asp:TextBox ID="TxtColumnName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TxtDatatype" runat="server" Width="103px">DataType</asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td align="left">
                    <asp:ImageButton ID="ImageButton1" runat="server"
                        ImageUrl="~/Image/mc_create_button.gif" onclick="ImageButton1_Click" />
                </td>
                <td align="left">
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
Page Behind:
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;

public partial class AnotherTest : System.Web.UI.Page
{ string connc = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString();

      

    protected void Page_Load(object sender, EventArgs e)
        {
          
         }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection(connc);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + TxtTableName.Text + "'", sqlcon);
        DataSet ds = new DataSet();
        da.Fill(ds);

        foreach (DataTable dt in ds.Tables)
        {
            // checking whether the table selected from the dataset exists in the database or not

            string exists = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '"+TxtTableName.Text+"'", sqlcon);
                sqlcon.Open();
                exists = cmd.ExecuteScalar().ToString();
            }
            catch (Exception exce)
            {
                exists = null;
            }

            // if does not exist

            if (exists == null)
            {
                // selecting each column of the datatable to create a table in the database

                foreach (DataColumn dc in dt.Columns)
                {
                    try
                    {
                        if (exists == null)
                        {
                            SqlCommand createtable = new SqlCommand("CREATE TABLE " + TxtTableName.Text + " (" + TxtTableName.Text.Substring(0, 1) + "ID" + " " + TxtDatatype.Text + ")", sqlcon);
                            createtable.ExecuteNonQuery();
                            exists = dt.TableName;
                        }
                        else
                        {
                            SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                            addcolumn.ExecuteNonQuery();
                            break;
                        }
                    }
                    catch
                    {
                        Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes');</script>");
                    }
                }

                // copying the data from datatable to database table

                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = "rama";
                //    bulkcopy.WriteToServer(dt);
                //}
            }
            // if table exists, just copy the data to the destination table in the database

            else
            {
                try
                {
                    SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                    addcolumn.ExecuteNonQuery();
                    break;
                }
                catch
                {
                    Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes');</script>");
                }
                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = dt.TableName;
                //    bulkcopy.WriteToServer(dt);
                //}
            }
        } 
    }
}



 Try Another:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AnotherTest.aspx.cs" Inherits="AnotherTest" %>

<!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>
    <h1 align="center" style="font-weight: bold; color: #800000">Shibashish mohanty</h1>
    <style type="text/css">
        .style1
        {
            width: 43%;
            border: 10px solid #800000;
            background-color: #666666;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <table class="style1"
            style="font-size: medium; font-weight: bold; color: #FFFFFF; margin-left: 0px;">
            <tr>
                <td>
                    Table Name:</td>
                <td>
                    <asp:TextBox ID="TxtTableName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Column Name:</td>
                <td>
                    <asp:TextBox ID="TxtColumnName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TxtDatatype" runat="server" Width="103px">DataType</asp:TextBox>
                </td>
                <td>
                    <asp:CheckBox ID="ChkPrimaryKey" runat="server" AutoPostBack="True"
                        Text="Primary Key" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td align="left">
                    <asp:ImageButton ID="ImageButton1" runat="server"
                        ImageUrl="~/Image/mc_create_button.gif" onclick="ImageButton1_Click" />
                </td>
                <td align="left">
                    &nbsp;</td>
                <td align="left">
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>



Code Behind:-

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;

public partial class AnotherTest : System.Web.UI.Page
{ string connc = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString();

      

    protected void Page_Load(object sender, EventArgs e)
        {
          
         }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection(connc);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + TxtTableName.Text + "'", sqlcon);
        DataSet ds = new DataSet();
        da.Fill(ds);

        foreach (DataTable dt in ds.Tables)
        {
            // checking whether the table selected from the dataset exists in the database or not

            string exists = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '"+TxtTableName.Text+"'", sqlcon);
                sqlcon.Open();
                exists = cmd.ExecuteScalar().ToString();
            }
            catch (Exception exce)
            {
                exists = null;
            }

            // if does not exist

            if (exists == null)
            {
                // selecting each column of the datatable to create a table in the database

                foreach (DataColumn dc in dt.Columns)
                {
                    try
                    {
                        if (exists == null)
                        {
                            SqlCommand createtable = new SqlCommand("CREATE TABLE " + TxtTableName.Text + " (" + TxtTableName.Text.Substring(0, 1) + "ID" + " " +"int"+ " IDENTITY(1,1) NOT NULL" + ")", sqlcon);
                            createtable.ExecuteNonQuery();
                            exists = dt.TableName;
                        }
                        else
                        {
                            if (ChkPrimaryKey.Checked)
                            {
                                SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " " + "PRIMARY KEY ", sqlcon);
                                addcolumn.ExecuteNonQuery();
                                break;
                            }
                            else
                            {
                                SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                                addcolumn.ExecuteNonQuery();
                                break;
                            }
                        }
                    }
                    catch
                    {
                        Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes or Already you have a primary key constraint');</script>");
                    }
                }

                // copying the data from datatable to database table

                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = "rama";
                //    bulkcopy.WriteToServer(dt);
                //}
            }
            // if table exists, just copy the data to the destination table in the database

            else
            {
                try
                {
                    if (ChkPrimaryKey.Checked)
                    {
                        SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " " + "PRIMARY KEY ", sqlcon);
                        addcolumn.ExecuteNonQuery();
                        break;
                    }
                    else
                    {
                        SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                        addcolumn.ExecuteNonQuery();
                        break;
                    }
                }
                catch
                {
                    Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes or Already you have a primary key constraint');</script>");
                }
                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = dt.TableName;
                //    bulkcopy.WriteToServer(dt);
                //}
            }
        } 
    }
}



Finally i got the better solution:-




Page source:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AnotherTest.aspx.cs" Inherits="AnotherTest" %>

<!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>
    <h1 align="center" style="font-weight: bold; color: #800000">Shibashish mohanty</h1>
    <style type="text/css">
        .style1
        {
            width: 43%;
            border: 10px solid #800000;
            background-color: #666666;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <table class="style1"
            style="font-size: medium; font-weight: bold; color: #FFFFFF; margin-left: 0px;">
            <tr>
                <td>
                    Table Name:</td>
                <td>
                    <asp:TextBox ID="TxtTableName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Column Name:</td>
                <td>
                    <asp:TextBox ID="TxtColumnName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TxtDatatype" runat="server" Width="103px">DataType</asp:TextBox>
                </td>
                <td>
                    <asp:CheckBox ID="ChkPrimaryKey" runat="server" AutoPostBack="True"
                        oncheckedchanged="ChkPrimaryKey_CheckedChanged" Text="Primary Key" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td align="left">
                    <asp:ImageButton ID="ImageButton1" runat="server"
                        ImageUrl="~/Image/mc_create_button.gif" onclick="ImageButton1_Click" />
                </td>
                <td align="left">
                    &nbsp;</td>
                <td align="left">
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
Code Behind:-



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;

public partial class AnotherTest : System.Web.UI.Page
{ string connc = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString();

      

    protected void Page_Load(object sender, EventArgs e)
        {
          
         }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection(connc);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + TxtTableName.Text + "'", sqlcon);
        DataSet ds = new DataSet();
        da.Fill(ds);

        foreach (DataTable dt in ds.Tables)
        {
            // checking whether the table selected from the dataset exists in the database or not

            string exists = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '"+TxtTableName.Text+"'", sqlcon);
                sqlcon.Open();
                exists = cmd.ExecuteScalar().ToString();
            }
            catch (Exception exce)
            {
                exists = null;
            }

            // if does not exist

            if (exists == null)
            {
                // selecting each column of the datatable to create a table in the database

                foreach (DataColumn dc in dt.Columns)
                {
                    try
                    {
                        if (exists == null)
                        {
                            SqlCommand createtable = new SqlCommand("CREATE TABLE " + TxtTableName.Text + " (" + TxtTableName.Text.Substring(0, 1) + "ID" + " " +"int"+ " IDENTITY(1,1) NOT NULL" + ")", sqlcon);
                            createtable.ExecuteNonQuery();
                            exists = dt.TableName;
                        }
                        else
                        {
                            if (ChkPrimaryKey.Checked)
                            {
                                SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " " + "PRIMARY KEY ", sqlcon);
                                addcolumn.ExecuteNonQuery();
                                break;
                            }
                            else
                            {
                                SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                                addcolumn.ExecuteNonQuery();
                                break;
                            }
                        }
                    }
                    catch
                    {
                        Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes');</script>");
                    }
                }

                // copying the data from datatable to database table

                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = "rama";
                //    bulkcopy.WriteToServer(dt);
                //}
            }
            // if table exists, just copy the data to the destination table in the database

            else
            {
                try
                {
                    if (ChkPrimaryKey.Checked)
                    {
                        SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " " + "PRIMARY KEY ", sqlcon);
                        addcolumn.ExecuteNonQuery();
                        break;
                    }
                    else
                    {
                        SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                        addcolumn.ExecuteNonQuery();
                        break;
                    }
                }
                catch
                {
                    Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes');</script>");
                }
                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = dt.TableName;
                //    bulkcopy.WriteToServer(dt);
                //}
            }
        } 
    }
    protected void ChkPrimaryKey_CheckedChanged(object sender, EventArgs e)
    {
        if (ChkPrimaryKey.Checked)
        {
            string PrimaryKey = null;

            PrimaryKey = GetprimaryKey(TxtTableName.Text, connc);
            if (PrimaryKey != null && PrimaryKey!="")
            {
                Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert(' Already you have a primary key constraint');</script>");

            }
        }  //Response.Write("This is your tablePrimary Key Column" + PrimaryKey);
    }
    public string GetprimaryKey(string tableName, string cnnString)
    {
        string names,
         ID = "";
        SqlDataReader mReader;
        SqlConnection mSqlConnection = new SqlConnection();
        SqlCommand mSqlCommand = new SqlCommand();

        mSqlConnection = new SqlConnection(connc);
        mSqlConnection.Open();
        // sp_pkeys is SQL Server default stored procedure
        // you pass it only table Name, it will return
        // primary key column
        mSqlCommand = new SqlCommand("sp_pkeys", mSqlConnection);
        mSqlCommand.CommandType = CommandType.StoredProcedure; mSqlCommand.Parameters.Add
                 ("@table_name", SqlDbType.NVarChar).Value = tableName;
        mReader = mSqlCommand.ExecuteReader();
        while (mReader.Read())
        {
            //the primary key column resides at index 4
            ID = mReader[3].ToString();
        }
        return ID;
    }
}



Another one Something Different: 

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

<!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>
    <h1 align="center" style="font-weight: bold; color: #800000">Shibashish mohanty</h1>
    <style type="text/css">
        .style1
        {
            width: 52%;
            border: 10px solid #800000;
            background-color: #666666;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
   
        <table class="style1"
           
            style="font-size: medium; font-weight: bold; color: #FFFFFF; margin-left: 0px; height: 227px;">
            <tr>
                <td>
                    Table Name:</td>
                <td>
                    <asp:TextBox ID="TxtTableName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Column Name:</td>
                <td>
                    <asp:TextBox ID="TxtColumnName" runat="server" Width="200px"></asp:TextBox>
                </td>
                <td>
                    <asp:TextBox ID="TxtDatatype" runat="server" Width="103px">DataType</asp:TextBox>
                </td>
                <td>
                    <asp:CheckBox ID="ChkPrimaryKey" runat="server" AutoPostBack="True"
                        oncheckedchanged="ChkPrimaryKey_CheckedChanged" Text="Primary Key" />
                </td>
                <td>
                    <asp:CheckBox ID="ChkAllowNull" runat="server" AutoPostBack="True"
                        Checked="True" Text="Allow Null" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td align="left">
                    &nbsp;</td>
                <td align="left">
                    <asp:ImageButton ID="ImageButton1" runat="server"
                        ImageUrl="~/Image/mc_create_button.gif" onclick="ImageButton1_Click" />
                </td>
                <td align="left">
                    &nbsp;</td>
                <td align="left">
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
 Code Behind:




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;

public partial class AnotherTest : System.Web.UI.Page
{ string connc = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString();

      

    protected void Page_Load(object sender, EventArgs e)
        {
            if (ChkPrimaryKey.Checked==false)
            {
                ChkAllowNull.Checked = true;
            }
         }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection(connc);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + TxtTableName.Text + "'", sqlcon);
        DataSet ds = new DataSet();
        da.Fill(ds);

        foreach (DataTable dt in ds.Tables)
        {
            // checking whether the table selected from the dataset exists in the database or not

            string exists = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '"+TxtTableName.Text+"'", sqlcon);
                sqlcon.Open();
                exists = cmd.ExecuteScalar().ToString();
            }
            catch (Exception exce)
            {
                exists = null;
            }

            // if does not exist

            if (exists == null)
            {
                // selecting each column of the datatable to create a table in the database

                foreach (DataColumn dc in dt.Columns)
                {
                    try
                    {
                        if (exists == null)
                        {
                            SqlCommand createtable = new SqlCommand("CREATE TABLE " + TxtTableName.Text + " (" + TxtTableName.Text.Substring(0, 1) + "ID" + " " +"int"+ " IDENTITY(1,1) NOT NULL" + ")", sqlcon);
                            createtable.ExecuteNonQuery();
                            exists = dt.TableName;
                        }
                        else
                        {
                            if (ChkPrimaryKey.Checked)
                            {
                                SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " " + "PRIMARY KEY ", sqlcon);
                                addcolumn.ExecuteNonQuery();
                                break;
                            }
                            else
                            {
                                if (ChkAllowNull.Checked)
                                {
                                    SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                                    addcolumn.ExecuteNonQuery();
                                    break;
                                }
                                else
                                {
                                    SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " NOT NULL", sqlcon);
                                    addcolumn.ExecuteNonQuery();
                                    break;
                                }
                            }
                        }
                    }
                    catch
                    {
                        Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes');</script>");
                    }
                }

                // copying the data from datatable to database table

                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = "rama";
                //    bulkcopy.WriteToServer(dt);
                //}
            }
            // if table exists, just copy the data to the destination table in the database

            else
            {
                try
                {
                    if (ChkPrimaryKey.Checked)
                    {
                        SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " " + "PRIMARY KEY ", sqlcon);
                        addcolumn.ExecuteNonQuery();
                        break;
                    }
                    else
                    {
                        if (ChkAllowNull.Checked)
                        {
                            SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text, sqlcon);
                            addcolumn.ExecuteNonQuery();
                            break;
                        }
                        else
                        {
                            SqlCommand addcolumn = new SqlCommand("ALTER TABLE " + TxtTableName.Text + " ADD " + TxtColumnName.Text + " " + TxtDatatype.Text + " NOT NULL", sqlcon);
                            addcolumn.ExecuteNonQuery();
                            break;
                        }
                    }
                }
                catch
                {
                    Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert('Invalid DataTypes');</script>");
                }
                //using (SqlBulkCopy bulkcopy = new SqlBulkCopy(sqlcon))
                //{
                //    bulkcopy.DestinationTableName = dt.TableName;
                //    bulkcopy.WriteToServer(dt);
                //}
            }
        }
        ChkPrimaryKey.Checked = false;
    }
    protected void ChkPrimaryKey_CheckedChanged(object sender, EventArgs e)
    {
        if (ChkPrimaryKey.Checked)
        {
            string PrimaryKey = null;

            PrimaryKey = GetprimaryKey(TxtTableName.Text, connc);
            if (PrimaryKey != null && PrimaryKey!="")
            {
                Page.RegisterStartupScript("ScriptDescription", "<script type=\"text/javascript\">alert(' Already you have a primary key constraint');</script>");
                ChkPrimaryKey.Checked = false;

            }
            ChkAllowNull.Checked = false;
        }  //Response.Write("This is your tablePrimary Key Column" + PrimaryKey);
    }
    public string GetprimaryKey(string tableName, string cnnString)
    {
        string names,
         ID = "";
        SqlDataReader mReader;
        SqlConnection mSqlConnection = new SqlConnection();
        SqlCommand mSqlCommand = new SqlCommand();

        mSqlConnection = new SqlConnection(connc);
        mSqlConnection.Open();
        // sp_pkeys is SQL Server default stored procedure
        // you pass it only table Name, it will return
        // primary key column
        mSqlCommand = new SqlCommand("sp_pkeys", mSqlConnection);
        mSqlCommand.CommandType = CommandType.StoredProcedure; mSqlCommand.Parameters.Add
                 ("@table_name", SqlDbType.NVarChar).Value = tableName;
        mReader = mSqlCommand.ExecuteReader();
        while (mReader.Read())
        {
            //the primary key column resides at index 4
            ID = mReader[3].ToString();
        }
        return ID;
    }
  
}

 

Thanks shibashish mohanty

.

ShibashishMnty
shibashish mohanty