Need Urgent Help i cannot access value dynamic dropdownlist
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 assigned : System.Web.UI.Page
{
DropDownList d;
DropDownList dr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
try
{
for (int i = 0; i < a; i++)
{
d = new DropDownList();
d.ID = "Text" + i;
LiteralControl l1 = new LiteralControl("<br></br>");
d.Items.Add("Rahul");
d.Items.Add("Gautum");
d.Items.Add("anant");
Panel3.Controls.Add(d);
Panel3.Controls.Add(l1);
}
}
catch (Exception e1)
{
Label4.Text = "" + e1.Message.ToString();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
string value = "";
for (int i = 0; i < a; i++)
{
string str ="Text" + i;
dr = new DropDownList();
dr = (DropDownList)(Panel3.FindControl(str));
value = dr.SelectedValue.ToString();
SqlConnection con = new SqlConnection("Data Source=ABC-0D78787890A;Initial Catalog=Amit;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);
cmd1.ExecuteNonQuery();
con.Close();
}
}
}
}