Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net save dynamically created control values in database

Jul 30, 2012
Introduction:

In this article I will explain how to save dynamically created control values in database using asp.net.
Description:

In previous posts I explained
how to create controls dynamically in asp.net and get dynamically created control values in asp.net. Now I will explain how to save dynamically created controls values in database using asp.net. To implement this concept first design table in database and give name as UserDetails as shown below

Column Name
Data Type
Allow Nulls
UserId
int(set identity property=true)
No
UserName
varchar(50)
Yes
Email
varchar(100)
Yes
After completion table design write the following code in your aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Create controls dynamically in asp.net and handle button click events</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Panel ID="pnlInfo" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
After completion of aspx page design add the following namespaces in code behind

C# code


using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
After add namespaces write the following code in code behind like as shown below


protected void Page_Load(object sender, EventArgs e)
{
Table tbldynamic = new Table();
TableCell tc=new TableCell();
TableCell tc1=new TableCell();
TableRow tr = new TableRow();
Label lblName =new Label();
lblName.ID = "lblName";
lblName.Text = "UserName:";
tc.Controls.Add(lblName);
TextBox txtName=new TextBox();
txtName.ID = "txtName";
tc1.Controls.Add(txtName);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
tc=new TableCell();
tc1=new TableCell();
tr=new TableRow();
Label lblEmail = new Label();
lblEmail.ID = "lblEmail";
lblEmail.Text = "Email:";
tc.Controls.Add(lblEmail);
TextBox txtEmail = new TextBox();
txtEmail.ID = "txtEmail";
tc1.Controls.Add(txtEmail);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
tc = new TableCell();
tc1 = new TableCell();
tr = new TableRow();
Button btnSubmit = new Button();
btnSubmit.ID = "btnSubmit";
btnSubmit.Text = "Submit";
btnSubmit.Click += new System.EventHandler(btnSubmit_click);
tc1.Controls.Add(btnSubmit);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tbldynamic.Rows.Add(tr);
pnlInfo.Controls.Add(tbldynamic);
}
// Dynamic button click event which is used to save values in database
protected void btnSubmit_click(object sender, EventArgs e)
{
String str = string.Empty;
TextBox txtUserName = (TextBox)pnlInfo.FindControl("txtName");
// str = txtUserName.Text;
TextBox txtEmail = (TextBox)pnlInfo.FindControl("txtEmail");
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
using (SqlCommand cmd =new SqlCommand("INSERT INTO UserDetails(UserName,Email) VALUES(@name,@mail)",con))
{
con.Open();
cmd.Parameters.AddWithValue("@name", txtUserName.Text);
cmd.Parameters.AddWithValue("@mail", txtEmail.Text);
cmd.ExecuteNonQuery();
txtEmail.Text = string.Empty;
txtUserName.Text = string.Empty;
ClientScript.RegisterClientScriptBlock(this.GetType(), "btn","<script type = 'text/javascript'>alert('UserDetails saved Successfully');</script>");
}
}
}

VB.NET Code

Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

Dim tbldynamic As New Table()
Dim tc As New TableCell()
Dim tc1 As New TableCell()
Dim tr As New TableRow()
Dim lblName As New Label()
lblName.ID = "lblName"
lblName.Text = "UserName:"
tc.Controls.Add(lblName)
Dim txtName As New TextBox()
txtName.ID = "txtName"
tc1.Controls.Add(txtName)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
tc = New TableCell()
tc1 = New TableCell()
tr = New TableRow()
Dim lblEmail As New Label()
lblEmail.ID = "lblEmail"
lblEmail.Text = "Email:"
tc.Controls.Add(lblEmail)
Dim txtEmail As New TextBox()
txtEmail.ID = "txtEmail"
tc1.Controls.Add(txtEmail)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
tc = New TableCell()
tc1 = New TableCell()
tr = New TableRow()
Dim btnSubmit As New Button()
btnSubmit.ID = "btnSubmit"
btnSubmit.Text = "Submit"
AddHandler btnSubmit.Click, AddressOf btnSubmit_click
tc1.Controls.Add(btnSubmit)
tr.Cells.Add(tc)
tr.Cells.Add(tc1)
tbldynamic.Rows.Add(tr)
pnlInfo.Controls.Add(tbldynamic)
End Sub
' Dynamic button click event which is used to save values in database
Protected Sub btnSubmit_click(ByVal sender As Object, ByVal e As EventArgs)
Dim str As [String] = String.Empty
Dim txtUserName As TextBox = DirectCast(pnlInfo.FindControl("txtName"), TextBox)
' str = txtUserName.Text;
Dim txtEmail As TextBox = DirectCast(pnlInfo.FindControl("txtEmail"), TextBox)
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Using cmd As New SqlCommand("INSERT INTO UserDetails(UserName,Email) VALUES(@name,@mail)", con)
con.Open()
cmd.Parameters.AddWithValue("@name", txtUserName.Text)
cmd.Parameters.AddWithValue("@mail", txtEmail.Text)
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
End Class
Demo


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

13 comments :

srinivas said...

Great Explanation

jai said...

1

Anonymous said...

Nice guidelines for beginners!!!!!!!!!!

tinhdamsugia said...

dfasddasfadf

Anonymous said...

good one

Muad'Dib said...

You should put code here. I am trying to implement something similiar to my page, and also to make to read data into pages dynamically like new part in link new data content.

Unknown said...

Thanks,your coding help me lot.

Unknown said...

thanx a lot suresh!

Unknown said...

how to save value in database if we have 100 Dynamic input boxes. How to separate their ID and how to store in Database.

Anonymous said...

Not Working....text box and button vanishes after button click and can't Enter in debug mode

vinnu said...

hi suresh can u please help in developing a usercontrol.
i had a form when click on addnewform(button).. sameform has to display under it.and how to save each form in database.
my emailid: vinith46@gmail.com

Srinivas P said...

Thanks

GoldenPearl said...

can you show the advanced version in which we can click add new button, that will create another two set of textboxes for Username and Email,
then again until total 5 users data, then save into DB.


Thanks

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.