Drop Down List Help

  • Thread starter Thread starter Dharmen
  • Start date Start date
D

Dharmen

Hey guys!
I wrote a script with 2 dropdown lists. When the value on one is
selected it should automatically load the corresponding value from the table
on the second list. This for some apparent reason is not working.

Label1.Text=this.ddList1.SelectedItem.ToString();
SqlCommand sqlCmd2 =new SqlCommand("SELECT Name FROM pricesTB1 WHERE
Location="+Label1.Text,conn);
conn.Open();
SqlDataReader read2=sqlCmd2.ExecuteReader();
while (read2.Read())
{
//ListItem NewItem2 = new ListItem();
//NewItem2.Text=read2.GetString(0);
//NewItem2.Value = read2.GetValue(0).ToString();
//ddList2.Items.Add(NewItem2);
Label2.Text=this.ddList1.SelectedItem.ToString();
}
conn.Close();

It works fine when i explicitely define the SQL Command but when i read it
using this.ddList1.SelectedItem.ToString();
it messes up...Please help me.
Thanks
 
I am trying to represent master-detail records from a database
The general logic will be populating the first dropdownlist with the master records and on change of the first dropdownlis
getting the selectedindex and querying the database and populating the childrecords in the second dropdownlist

But my question is how the same can be acheieved by using "Relations"

The following code fragment i tried, but i have no idea how to proceed

Thanks in advanc
Ravichandra

Dim myconn As Strin
Dim oledataadapt As New OleDbDataAdapte
Dim oledataadapt1 As New OleDbDataAdapte
Dim ds As New DataSe
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
'Put user code to initialize the page her

myconn = "Provider=MSDAORA;User ID=x;Password=x;Data Source=xxx;
oledataadapt = New OleDbDataAdapter("Select Organization_id from bengine.cb_organization", myconn
oledataadapt.Fill(ds, "Sector"

oledataadapt1 = New OleDbDataAdapter("Select Superbu_id, organization_id from bengine.cb_super_businessunit", myconn
oledataadapt1.Fill(ds, "SBU"
ds.Relations.Add("Sec_sbu", ds.Tables("Sector").Columns("Organization_id"),
ds.Tables("SBU").Columns("Organization_id")
If Not IsPostBack The
DropDownList1.DataSource = ds.Tables(0).DefaultVie
DropDownList2.DataSource = ds.Tables(1).DefaultVie
DropDownList1.DataMember = "Organization_id
DropDownList2.DataMember = "superbu_id
DropDownList1.DataTextField = "Organization_id
DropDownList1.DataValueField = "Organization_id
DropDownList2.DataTextField = "superbu_id
DropDownList2.DataValueField = "superbu_id
DropDownList1.DataBind(
DropDownList2.DataBind(
End I
End Su

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChange
Dim i As Intege
i = DropDownList1.SelectedInde
DropDownList2.DataSource = ds.Tables(0).Rows(i).GetChildRows("Sec_sbu"
DropDownList2.DataTextField = "superbu_id
DropDownList2.DataValueField = "superbu_id
DropDownList1.DataBind(
DropDownList2.DataBind(
End Su
 
Dharmen,
I think what ravi has mentioned is correct.
That SelectedIndex_Change will work as u mentioned on change of
DropDownlist if that control has AutoPostBack = true.

ARvind
Ravi said:
I am trying to represent master-detail records from a database.
The general logic will be populating the first dropdownlist with the
master records and on change of the first dropdownlist
getting the selectedindex and querying the database and populating the
childrecords in the second dropdownlist.
But my question is how the same can be acheieved by using "Relations"?

The following code fragment i tried, but i have no idea how to proceed.

Thanks in advance
Ravichandran

Dim myconn As String
Dim oledataadapt As New OleDbDataAdapter
Dim oledataadapt1 As New OleDbDataAdapter
Dim ds As New DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

myconn = "Provider=MSDAORA;User ID=x;Password=x;Data Source=xxx;"
oledataadapt = New OleDbDataAdapter("Select Organization_id from
bengine.cb_organization", myconn)
oledataadapt.Fill(ds, "Sector")

oledataadapt1 = New OleDbDataAdapter("Select Superbu_id,
organization_id from bengine.cb_super_businessunit", myconn)
oledataadapt1.Fill(ds, "SBU")
ds.Relations.Add("Sec_sbu",
ds.Tables("Sector").Columns("Organization_id"), _
ds.Tables("SBU").Columns("Organization_id"))
If Not IsPostBack Then
DropDownList1.DataSource = ds.Tables(0).DefaultView
DropDownList2.DataSource = ds.Tables(1).DefaultView
DropDownList1.DataMember = "Organization_id"
DropDownList2.DataMember = "superbu_id"
DropDownList1.DataTextField = "Organization_id"
DropDownList1.DataValueField = "Organization_id"
DropDownList2.DataTextField = "superbu_id"
DropDownList2.DataValueField = "superbu_id"
DropDownList1.DataBind()
DropDownList2.DataBind()
End If
End Sub

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
Hey Guys...Thanks for the replies!
I still have problems but this time atleast it doesnt gimme any errors...It
jist doesnt enter the while (read2.Read())
loop! I think this is because Im usign sqlCmd.Execute Reader in the first
function and the sqlCmd2.ExecuteReader again.

Is this wrong? or do i have to do anything to close it? I need help in C#
please...i dont know much of VB...If u have MSN ID it would be helpful!
Thanks!
 
Back
Top