how to create dynamic controls and how to save that values in database?

Hariinakoti
Posted by Hariinakoti under C# category on | Points: 40 | Views : 3547
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 : System.Web.UI.Page
{
Label lbl = new Label();
TextBox txtename = new TextBox();
Label lblgender = new Label();
DropDownList ddlgender = new DropDownList();
Label lblsal = new Label();
TextBox txtsal = new TextBox();
Button btn = new Button();
protected void Page_Load(object sender, EventArgs e)
{
lbl.Text = "EName";
lblgender.Text = "Gender";
lblsal.Text = "Salary";
this.form1.Controls.Add(lbl);
this.form1.Controls.Add(txtename);
this.form1.Controls.Add(lblgender);
this.form1.Controls.Add(ddlgender);
ddlgender.Items.Add("-Select-");
ddlgender.Items.Add("Male");
ddlgender.Items.Add("Female");
this.form1.Controls.Add(lblsal);
this.form1.Controls.Add(txtsal);
this.form1.Controls.Add(btn);
btn.Text = "Save";
btn.Click += new EventHandler(btn_Click);
}

void btn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Persist Security Info=False;User ID=sa;Password=amma;Initial Catalog=Sample;Data Source=AmmaNaanna-PC");
con.Open();
//SqlCommand cmd = new SqlCommand("insert into Dynamic values('"+arrList[0].ToString() +"','"+arrList[1].ToString()+"','"+arrList[2].ToString()+ "')", con);
SqlCommand cmd = new SqlCommand("insert into Dynamic values('" + txtename.Text.ToString() + "','" + ddlgender.SelectedItem.ToString() + "','" +int.Parse(txtsal.Text)+ "')", con);
cmd.ExecuteNonQuery();
con.Close();
}

}

Comments or Responses

Login to post response