...

View Full Version : Seleted value from dynamic dropdownlist can't acess



goltu
10-29-2011, 08:55 AM
Main error are coming in button 2 while retrving the value .Button is ok. create the dynamically dropdownlist as entered in textbox .
Error is
" Object reference not set to an instance of an object."
" value = dr.SelectedValue.ToString();"


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();


}

}

}
}

jassi.singh
11-09-2011, 07:20 AM
Main error are coming in button 2 while retrving the value .Button is ok. create the dynamically dropdownlist as entered in textbox .
Error is
" Object reference not set to an instance of an object."
" value = dr.SelectedValue.ToString();"


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();


}

}

}
}

Hello,

Can you please be more specific of your requirements. Where is the code to bind the dropdown list?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum