I am generating dynamic textbox for that I have 2 tables:
1. dynamic
2. empdetail
Here empdetail is a master table and dynamically generated column is inserted in that table and after that I also want to store data in database. But the problem is when i save the dynamic textbox value in database, the value will be insert null in database i cant see the dynamic textbox value...
Pls help me to solve my problem. My code is below...... My Dynamic TextBox id is="TxtDynamic" and The problem is on Button3 query.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Sql;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=vaio\\sqlexpress;Initial Catalog=emp;User ID=sa;Password=administrator";
con.Open();
string query = "insert into empdetail (id,name) values (@id,@name)";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@id", txtId.Text);
cmd.Parameters.AddWithValue("@name", txtName.Text);
txtId.Text = "";
txtName.Text = "";
cmd.ExecuteNonQuery();
}
con.Close();
}
static int i;
int j;
protected void Button2_Click(object sender, EventArgs e)
{
Button3.Visible = true;
i++;
for (j = 0; j <= i-1; j++)
{
Label lbl = new Label();
lbl.ID = "dlbl";
lbl.Text = TextBox1.Text;
Panel1.Controls.Add(lbl);
TextBox tb = new TextBox();
tb.ID = "TxtDynamic"+i;
Panel1.Controls.Add(tb);
}
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=vaio\\sqlexpress;Initial Catalog=emp;User ID=sa;Password=administrator";
string query = "insert into dynamic (controlname,size,datatype) values (@controlname,@size,@datatype)";
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@controlname", TextBox1.Text);
cmd.Parameters.AddWithValue("@size", TextBox2.Text);
cmd.Parameters.AddWithValue("@datatype", DropDownList1.SelectedValue);
TextBox2.Text = "";
cmd.ExecuteNonQuery();
}
string qry = "alter table empdetail add " +TextBox1.Text+ " " +DropDownList1.SelectedValue+ " null";
SqlCommand cd = new SqlCommand(qry, con);
cd.ExecuteNonQuery();
con.Close();
}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
Label3.Visible = true;
Label4.Visible = true;
Label5.Visible = true;
TextBox1.Visible = true;
TextBox2.Visible = true;
DropDownList1.Visible = true;
Button2.Visible = true;
Button3.Visible = false;
}
else
{
Label3.Visible = false;
Label4.Visible = false;
Label5.Visible = false;
TextBox1.Visible = false;
TextBox2.Visible = false;
DropDownList1.Visible = false;
Button2.Visible = false;
Button3.Visible = false;
}
}
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=vaio\\sqlexpress;Initial Catalog=emp;User ID=sa;Password=administrator";
con.Open();
string query = "update empdetail set " + TextBox1.Text + " = '"+ Panel1.FindControl("TxtDynamic1" + i.ToString()) + "' where id=(select Max(id) from empdetail )";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
}