How to create dynamic textboxes and stored textboxes values save in SQL database in C#

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
        <asp:Table id="Table1" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" Font-Bold="True" >
   
        </asp:Table>
<asp:PlaceHolder id="PlaceHolder1" runat="server">
</asp:PlaceHolder><BR><BR>
        <br />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    <asp:Button ID="btn" runat="server" Text="Button" onclick="btn_Click1" />
    </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;
public partial class Default2 : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(@"Data Source=COMTECH-PC\SQLEXPRESS;database=kabir;integrated security=true;");

     protected void Page_Load(object sender, EventArgs e)
    {
     
        if (ViewState["Count"] != null)
        {
         
          
            //AddTextBoxs();
            //AddButton();
            AddTextBoxs_Session();
         
        }
    }
     private void AddTextBoxs()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
           

         }


     }

     private void AddTextBoxs_Session()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
             cn.Open();
             SqlCommand cmd = new SqlCommand("alter table pratima add " + (t.ID).ToString() + " varchar(50)", cn);
             cmd.ExecuteNonQuery();
             cn.Close();

         }

     }

    //private void AddButton()
    //{
    //    Button b = new Button();
    //    b.ID = "btn";
    //    b.Text = "Button";
    //    b.Click += new System.EventHandler(btn_Click);
    //    PlaceHolder1.Controls.Add(b);
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        AddTextBoxs();
       // if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;

    }
  
    protected void Button2_Click(object sender, EventArgs e)
    {
      
       // AddButton();

        clear();
        AddTextBoxs();
        //if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
    }
  
    public void bindgrid()
    {
        //SqlConnection cn = new SqlConnection("Data Source=SQLDB;User ID=Demoh;Password=Demo1@");
        //SqlCommand cmd = new SqlCommand("select * from poll", cn);
        //DataTable dt = new DataTable();
        //SqlDataAdapter sa = new SqlDataAdapter();
        //cn.Open();
        //sa.SelectCommand = cmd;
        //cmd.ExecuteNonQuery();
        //sa.Fill(dt);
        //GridView1.DataSource = dt;
        //GridView1.DataBind();
    }
    public void clear()
    {
        Table1.Rows.Clear();
        for (int i = 0; i < Table1.Rows.Count; i++)
            Table1.Rows.RemoveAt(i);
    }

    protected void btn_Click1(object sender, EventArgs e)
    {
        //AddTextBoxs_Session();
        
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        try
        {
            for (int i = 1; i < Table1.Rows.Count; i++)
            {
                //string dp = ((TextBox)Table1.Rows[i+1].FindControl("txtnm" + i+1)).Text;
                TextBox tnm = (TextBox)Table1.Rows[i].FindControl("CN" + i);

                //TextBox tsal = (TextBox)Table1.Rows[i].FindControl("txtsal" + i);
                //DropDownList tyear = (DropDownList)Table1.Rows[i].FindControl("dpl" + i);
                string colname = "CN" + i;
                string str="select * from pratima";
                SqlDataAdapter da=new SqlDataAdapter(str,cn);
                DataSet ds=new DataSet();
                da.Fill(ds);
                int k=Convert.ToInt32(ds.Tables[0].Rows.Count);
                if(k<=0)
                {
                SqlCommand cmd1 = new SqlCommand("insert into  pratima (" + colname + ") values('" + tnm.Text + "')",cn);
                cmd1.ExecuteNonQuery();
                }
                else
                {

                    SqlCommand cmd1 = new SqlCommand("update  pratima set " + colname + "='" + tnm.Text + "' where id="+k+"", cn);
                    cmd1.ExecuteNonQuery();
                }
                

                // bindgrid();
            }
        }
            catch(Exception ex)
            {
               
            }
        finally
            {
            cn.Close();
             clear();
            }

        }

        
    
}


Share:

No comments:

Post a Comment

Monday, 1 June 2015

How to create dynamic textboxes and stored textboxes values save in SQL database in C#

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
        <asp:Table id="Table1" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="3px" Font-Bold="True" >
   
        </asp:Table>
<asp:PlaceHolder id="PlaceHolder1" runat="server">
</asp:PlaceHolder><BR><BR>
        <br />
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    <asp:Button ID="btn" runat="server" Text="Button" onclick="btn_Click1" />
    </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;
public partial class Default2 : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection(@"Data Source=COMTECH-PC\SQLEXPRESS;database=kabir;integrated security=true;");

     protected void Page_Load(object sender, EventArgs e)
    {
     
        if (ViewState["Count"] != null)
        {
         
          
            //AddTextBoxs();
            //AddButton();
            AddTextBoxs_Session();
         
        }
    }
     private void AddTextBoxs()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
           

         }


     }

     private void AddTextBoxs_Session()
     {
         clear();
         int cnt = Convert.ToInt32(TextBox1.Text);
         TableHeaderRow htr = new TableHeaderRow();
         TableHeaderCell htc1 = new TableHeaderCell();
         htc1.Width = 100;
         htc1.Text = "Name";
         htr.Cells.Add(htc1);
         Table1.Rows.Add(htr);
         for (int i = 0; i < cnt; i++)
         {
             TableRow tr = new TableRow();
             TableCell tc1 = new TableCell();
             tc1.Width = 100;
             TextBox t = new TextBox();
             t.Width = 100;
             t.ID = "CN" + Table1.Rows.Count;
             tc1.Controls.Add(t);
             tr.Cells.Add(tc1);
             Table1.Rows.Add(tr);
             cn.Open();
             SqlCommand cmd = new SqlCommand("alter table pratima add " + (t.ID).ToString() + " varchar(50)", cn);
             cmd.ExecuteNonQuery();
             cn.Close();

         }

     }

    //private void AddButton()
    //{
    //    Button b = new Button();
    //    b.ID = "btn";
    //    b.Text = "Button";
    //    b.Click += new System.EventHandler(btn_Click);
    //    PlaceHolder1.Controls.Add(b);
    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        AddTextBoxs();
       // if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;

    }
  
    protected void Button2_Click(object sender, EventArgs e)
    {
      
       // AddButton();

        clear();
        AddTextBoxs();
        //if (ViewState["Count"] == null) AddButton();
        ViewState["Count"] = Convert.ToInt16(ViewState["Count"]) + 1;
    }
  
    public void bindgrid()
    {
        //SqlConnection cn = new SqlConnection("Data Source=SQLDB;User ID=Demoh;Password=Demo1@");
        //SqlCommand cmd = new SqlCommand("select * from poll", cn);
        //DataTable dt = new DataTable();
        //SqlDataAdapter sa = new SqlDataAdapter();
        //cn.Open();
        //sa.SelectCommand = cmd;
        //cmd.ExecuteNonQuery();
        //sa.Fill(dt);
        //GridView1.DataSource = dt;
        //GridView1.DataBind();
    }
    public void clear()
    {
        Table1.Rows.Clear();
        for (int i = 0; i < Table1.Rows.Count; i++)
            Table1.Rows.RemoveAt(i);
    }

    protected void btn_Click1(object sender, EventArgs e)
    {
        //AddTextBoxs_Session();
        
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        try
        {
            for (int i = 1; i < Table1.Rows.Count; i++)
            {
                //string dp = ((TextBox)Table1.Rows[i+1].FindControl("txtnm" + i+1)).Text;
                TextBox tnm = (TextBox)Table1.Rows[i].FindControl("CN" + i);

                //TextBox tsal = (TextBox)Table1.Rows[i].FindControl("txtsal" + i);
                //DropDownList tyear = (DropDownList)Table1.Rows[i].FindControl("dpl" + i);
                string colname = "CN" + i;
                string str="select * from pratima";
                SqlDataAdapter da=new SqlDataAdapter(str,cn);
                DataSet ds=new DataSet();
                da.Fill(ds);
                int k=Convert.ToInt32(ds.Tables[0].Rows.Count);
                if(k<=0)
                {
                SqlCommand cmd1 = new SqlCommand("insert into  pratima (" + colname + ") values('" + tnm.Text + "')",cn);
                cmd1.ExecuteNonQuery();
                }
                else
                {

                    SqlCommand cmd1 = new SqlCommand("update  pratima set " + colname + "='" + tnm.Text + "' where id="+k+"", cn);
                    cmd1.ExecuteNonQuery();
                }
                

                // bindgrid();
            }
        }
            catch(Exception ex)
            {
               
            }
        finally
            {
            cn.Close();
             clear();
            }

        }

        
    
}


No comments:

Post a Comment

Popular

Total Pageviews

Archive