Thursday, February 3, 2011

how to store data in sqlserver from dynamically created controls

Hi,In my previous article i am already explained how to create a dynamic controls Now i would like to explain how to store the data in sqlserver from this dynamic controls and how to kept validations for this dynamic controls..

First,Open the MIcrosoft visual studio 2008


Next,Create one Asp.Net Web application



Next,Open the Design page of the Default.aspx


Next,Drag and Drop one Textbox and Two Buttons from the ToolBox and change the names of the buttons as AddTextBoxes and Submit


Next,come to the code page that is Default.aspx.cs and copy the following code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Dynamic_controls : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand com;
  protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            CreateDynamicTextBoxes();                      
        }
       
    }
  protected void CreateDynamicTextBoxes()
    {
        string val = txt_value.Text;
        int c = Convert.ToInt32(val);
        TextBox[] textBoxArr = new TextBox[c];//array of textboxes
        for (int i = 0; i < c; i++)
        {
           
            textBoxArr[i] = new TextBox();
            textBoxArr[i].ID = "txtBox" + i.ToString();
            PlaceHolder1.Controls.Add(textBoxArr[i]);
            RequiredFieldValidator reqfldVal = new      RequiredFieldValidator();
            reqfldVal.ID = "RequiredValidator" + i;
            reqfldVal.ControlToValidate = "txtBox" + i;
            reqfldVal.ErrorMessage = "Not Empty";
            reqfldVal.SetFocusOnError = true;
            PlaceHolder1.Controls.Add(reqfldVal);
        }
    }

protected void btn_submit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Convert.ToInt32( txt_value.Text); i++)
        {
            string txtvalue = "txtBox" + i.ToString();          
            TextBox txt = (TextBox)PlaceHolder1.FindControl(txtvalue);
           
            if (txt != null )
            {               
                string strText = txt.Text;               
                // store your textbox value in database
                con = new SqlConnection(ConfigurationManager.ConnectionStrings["SAMPLE_2"].ToString());
                con.Open();
                com = new SqlCommand("insert into Dynamic_Form(TextboxValue)values('" + strText + "')", con);
                com.ExecuteNonQuery();
                con.Close();
            }
        }
    }
Finally, Execute the page


Thats it...:D


If You Have a Doubts feel free to ask me.....Enjoy coding

developercode
About the Author
Sayyad is a Software Engineer, Blogger and Founder of Developers Code from India.His articles mainly focus on .Net (Web and Windows based applications), Mobile Technologies (Android,IPhone,BlackBerry) and SEO.

Labels: , , , , ,

3 Comments:

At March 14, 2011 at 1:11 PM , Blogger Unknown said...

Hi Tanisha,
I tried with your code, but i am getting a null exception.
can you plz explain me
string txtvalue = "txtBox" + i.ToString();
what do u mean by this statement
because i am getting an error due to this statement

 
At March 15, 2011 at 8:48 AM , Blogger Developers Code said...

Hi Minakshi

Thanks for sharing..i will explain you clearly..

txtBox means we are dynamically generating the ID's for dynamically created textboxes

By using this code textBoxArr[i].ID = "txtBox" + i.ToString();//means

if you declare value 4 in the txt_value.Text

then it dynamically generates ID's like

txtBox[0];
txtBox[1];
txtBox[2];
txtBox[3];

i am already checked this code ..its working fine..will you pls send the your code i will try to rectify the problem...Happy coding

 
At June 28, 2011 at 10:17 AM , Anonymous Minakshi said...

Hi Tanisha,
I tried with your code, but i am getting a null exception.
can you plz explain me
string txtvalue = "txtBox" + i.ToString();
what do u mean by this statement
because i am getting an error due to this statement

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home