i have a web form with server controls like textbox, dropdownlist, radio button, label etc.its a user registration form..
now what i want here is..when ever i choose a option from the dropdownlist. i want all the controls like textbox, dropdownlist etc to be
automatically filled up by the data related to that option in the dropdownlist..i want this data to be fetched from the data that is stored in the sql database.
You need to call dropdownlist selected index changed event, in that fetch data from releated table and assign them to the text boxes.
If you are using ajax then you have to put all controls inside the updatepanel except and dropdown list and you can set the async postback trigger
as dropdownlist selected index changed event
otherwise you can use icallback
HTH
With Luv
Dhana
Dont forget to mark as answer if my reply helped you...
Marked as answer by Xun Ye - MSFT on Jan 23, 2008 05:28 AM
I assumed that you have A dropdownlist in your webform and lets say two textboxes.. Here i am using a DataTable as my DataSource
So first populate your DropDownList with a data from your database
// A method that Populates your DropDownList
private void BindDDL()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]) // calling up your connection string that was configured in you Web Config File
{
DataTable dt = new DataTable();
conn.Open();
String sql = "SELECT Category FROM Table1"
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList1.Items.Add("Select");
for (int i = 0; i < dt.Rows.Count; i++)
{
// A method that fetches the data from the dataBase based on the SELECTED ITEM FROM THE DropDownList
private void FetchData()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]) // calling up your connection string that was configured in you Web Config File
{
DataTable dt = new DataTable();
conn.Open();
String sql = "SELECT * FROM Table2 WHERE Category = '" +DropDownList1.SelectedItem.Text+ "'"
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
//If you want to get mutiple data from the database then you need to write a simple looping
TextBox1.Text = dt.Rows[0]["Field1"].ToString();
TextBox2.Text = dt.Rows[0]["Field2"].ToString();
}
cmd.Connection.Close();
here i've given the sample source code with database details
create table testtable1(id int,usrname varchar(50),usrage int)
insert into testtable1 values(1,'user1',20)
insert into testtable1 values(2,'user2',22)
insert into testtable1 values(3,'user3',25)
insert into testtable1 values(4,'user4',27)
insert into testtable1 values(5,'user5',29)
insert into testtable1 values(6,'user6',31)
insert into testtable1 values(7,'user7',33)
insert into testtable1 values(8,'user8',35)
Be sure that you have set a connectionstring in your webconfig file.. Note that I am using appSettings there in setting my connection string.. so you should have something like this below
In you Web. Config file add this
<appSettings>
<add key="ConnString" value="Data Source=<Sql Server Name>;Initial Catalog=<Name of Database>;Integrated Security=SSPI;"/>
</appSettings>
Sorry for the late reply.. Anyway try to have it this way
private void BindDDL()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]) // calling up your connection string that was configured in you Web Config File
{
DataTable dt = new DataTable();
if (conn.State == ConnectionState.Open) // validates if the connection state is open
{ conn.Close(); }
conn.Open();
String sql = "SELECT Category FROM Table1"
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList1.Items.Add("Select");
for (int i = 0; i < dt.Rows.Count; i++)
{
jack007
Member
165 Points
588 Posts
filling the textbox,dropdownlist from the database??
Jan 21, 2008 11:14 AM|LINK
hello everyone..
i have a question..may be you can help.
i have a web form with server controls like textbox, dropdownlist, radio button, label etc.its a user registration form..
now what i want here is..when ever i choose a option from the dropdownlist. i want all the controls like textbox, dropdownlist etc to be
automatically filled up by the data related to that option in the dropdownlist..i want this data to be fetched from the data that is stored in the sql database.
how is it done..
anybody??
thanks..
dhanabalanr
Participant
978 Points
181 Posts
Re: filling the textbox,dropdownlist from the database??
Jan 21, 2008 12:57 PM|LINK
Hi,
You need to call dropdownlist selected index changed event, in that fetch data from releated table and assign them to the text boxes.
If you are using ajax then you have to put all controls inside the updatepanel except and dropdown list and you can set the async postback trigger
as dropdownlist selected index changed event
otherwise you can use icallback
HTH
Dhana
Dont forget to mark as answer if my reply helped you...
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: filling the textbox,dropdownlist from the database??
Jan 21, 2008 01:19 PM|LINK
Hi,
I assumed that you have A dropdownlist in your webform and lets say two textboxes.. Here i am using a DataTable as my DataSource
So first populate your DropDownList with a data from your database
// A method that Populates your DropDownList
private void BindDDL()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]) // calling up your connection string that was configured in you Web Config File
{
DataTable dt = new DataTable();
conn.Open();
String sql = "SELECT Category FROM Table1"
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList1.Items.Add("Select");
for (int i = 0; i < dt.Rows.Count; i++)
{
Cat = dt.Rows[i]["Category"].ToString();
DropDownList1.Items.Add(Cat);
}
}
cmd.Connection.Close();
}
}
// A method that fetches the data from the dataBase based on the SELECTED ITEM FROM THE DropDownList
private void FetchData()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]) // calling up your connection string that was configured in you Web Config File
{
DataTable dt = new DataTable();
conn.Open();
String sql = "SELECT * FROM Table2 WHERE Category = '" +DropDownList1.SelectedItem.Text+ "'"
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
//If you want to get mutiple data from the database then you need to write a simple looping
TextBox1.Text = dt.Rows[0]["Field1"].ToString();
TextBox2.Text = dt.Rows[0]["Field2"].ToString();
}
cmd.Connection.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDDL();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
FetchData();
}
Hope my suggestion helps
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
jack007
Member
165 Points
588 Posts
Re: filling the textbox,dropdownlist from the database??
Jan 23, 2008 06:08 AM|LINK
Hi Vinz,
Thanks for the tips.
well i did exactly what u wrote.
but as soon as i select a item from a dropdownlist..
there displays a messagebox stating
"connectionstring property has not been initialized"
whats the problem here? any solution..
and why did u use datatable here..
thanks..
jack007
Member
165 Points
588 Posts
Re: filling the textbox,dropdownlist from the database??
Jan 23, 2008 06:18 AM|LINK
Hi Dhana
But how do i fetch data from related table and assign them to the text boxes in the dropdownlist selected index changed event.
and how can i accomplish this using Ajax.can u help me with some example or codes to do this..
thanks
dhanabalanr
Participant
978 Points
181 Posts
Re: filling the textbox,dropdownlist from the database??
Jan 23, 2008 08:57 AM|LINK
Hi jack,
here i've given the sample source code with database details
HTH
Dhana
Dont forget to mark as answer if my reply helped you...
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: filling the textbox,dropdownlist from the database??
Jan 23, 2008 11:19 AM|LINK
Be sure that you have set a connectionstring in your webconfig file.. Note that I am using appSettings there in setting my connection string.. so you should have something like this below
In you Web. Config file add this
<appSettings>
<add key="ConnString" value="Data Source=<Sql Server Name>;Initial Catalog=<Name of Database>;Integrated Security=SSPI;"/>
</appSettings>
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
jack007
Member
165 Points
588 Posts
Re: filling the textbox,dropdownlist from the database??
Jan 24, 2008 08:04 AM|LINK
hello vinz..
thanks.it worked till the limit..
but now as soon as i pick a option from dropdownlist there pops up another error message box stating "the connection was not closed.the connection's
current state is open."
any clue..
thanks..
jack007
Member
165 Points
588 Posts
Re: filling the textbox,dropdownlist from the database??
Jan 24, 2008 08:30 AM|LINK
hi dhana..
thanks for the clue..
but i didnt get the meaning of this statement.
string sqlQry = "SELECT ID DISPMEM, DISPMEM FROM TESTTABLE1";
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: filling the textbox,dropdownlist from the database??
Jan 24, 2008 11:14 AM|LINK
Hi Jack,
Sorry for the late reply.. Anyway try to have it this way
private void BindDDL()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]) // calling up your connection string that was configured in you Web Config File
{
DataTable dt = new DataTable();
if (conn.State == ConnectionState.Open) // validates if the connection state is open
{ conn.Close(); }
conn.Open();
String sql = "SELECT Category FROM Table1"
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
DropDownList1.Items.Add("Select");
for (int i = 0; i < dt.Rows.Count; i++)
{
Cat = dt.Rows[i]["Category"].ToString();
DropDownList1.Items.Add(Cat);
}
}
conn.Close();
}
}
Let me know if it wont work..
MessageBox Controls for WebForms | Blog | Twitter | Linkedin