473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dropdownlist depending another Dropdownlist

3
hello all!
i really need a help of a friend...
i am working on a self project.
and i am realy lost here...
i have to Dropdown list ,the first one gives me the name of the hospital
and the other one give me the name of the doctors that are in the same hospital.
iam using SP and they really work i tested them
but i have a problem with the doctors dropdownlist...
if i choose a doctor...and after i chose another doctor i see them both...
it always remembers the last doctor and keep remembering them all
PLEASE could some one help me code that.....

here is my code:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillHospitalCombo();
}
FillDoctorCombo();

}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{

//Got my id from Dropdownlist

zmn.Doctor_ID =Convert.ToInt32( DropDownList2.SelectedValue);
DropDownList2.Items.Clear();
string date;
//Get This format of date dd,mm,yy
date = DateTime.Today.Day.ToString();
date += "/" + DateTime.Today.Month.ToString();
date += "/" + DateTime.Today.Year.ToString();

zmn.Date = date;



#region My Old Way-->Now it"s just remarks
/*
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(StrCon);
//Execute GetHoleByDate Procedure
SqlCommand cmd = new SqlCommand("GiveDateFrom", con);
//telling him that we use SP
cmd.CommandType = CommandType.StoredProcedure;
//Create Parameter
SqlParameter MyParmeter = new SqlParameter();
//Scan Parameter Name,Type,Value
MyParmeter.ParameterName = "@ID";
MyParmeter.SqlDbType = SqlDbType.Int;
MyParmeter.Value = Doc.Doc_ID;
//AddParameter to cmd
cmd.Parameters.Add(MyParmeter);

SqlParameter MyParmeter2 = new SqlParameter();
MyParmeter2.ParameterName = "@Date";
MyParmeter2.SqlDbType = SqlDbType.NVarChar;
MyParmeter2.Value =zmn.Date;
//AddParameter to cmd
cmd.Parameters.Add(MyParmeter2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
//Fill DT
con.Open();
da.Fill(dt);
con.Close();
*/
#endregion

DataTable dt = new DataTable();
dt = zmn.GetDateFromTodayAndOn();
//Show Grid
GridView1.DataSource = dt;
GridView1.RowStyle.HorizontalAlign = HorizontalAlign.Center;
GridView1.DataBind();

}

public void FillHospitalCombo()
{
SqlConnection conn = new SqlConnection(StrCon);
SqlCommand cmd = new SqlCommand("GetAllHospital", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr;
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(new ListItem(dr["Hospital_Name"].ToString(), dr["Hospital_ID"].ToString()));
}
conn.Close();
}

public void FillDoctorCombo()
{
string strsql = @"SELECT Doctors.Doctor_ID, Doctors.First_Name
FROM Doctors INNER JOIN
Hospital ON Doctors.Hospital_ID = Hospital.Hospital_ID
WHERE Hospital.Hospital_ID = {0}";
strsql = string.Format(strsql, DropDownList1.SelectedIndex.ToString());
SqlConnection conn = new SqlConnection(StrCon);
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataReader dr;
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList2.Items.Add(new ListItem(dr["First_Name"].ToString(), dr["Doctor_ID"].ToString()));
}
conn.Close();
}


}
May 16 '07 #1
1 1816
Hi Nir,

I seen something wrong here in your code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillHospitalCombo();
}
FillDoctorCombo();

}
in this part you are filling Doctor dropdown everytime when page is postback.

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{

//Got my id from Dropdownlist

* zmn.Doctor_ID =Convert.ToInt32( DropDownList2.SelectedValue);
DropDownList2.Items.Clear();
string date;

so in above part whenever dropdownlist2 index is changes page will get postback right? so again dropdownlist2 will be filled up as per page load code and on line marked as * you will get first value of dropdownlist2 only.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
FillDoctorCombo();
}
right one more subrouting like this and remove FillDoctorCombo(); from page load event. I think this should work properly now


Bye,
Kunal

----------------------------------------------------------------------

hello all!
i really need a help of a friend...
i am working on a self project.
and i am realy lost here...
i have to Dropdown list ,the first one gives me the name of the hospital
and the other one give me the name of the doctors that are in the same hospital.
iam using SP and they really work i tested them
but i have a problem with the doctors dropdownlist...
if i choose a doctor...and after i chose another doctor i see them both...
it always remembers the last doctor and keep remembering them all
PLEASE could some one help me code that.....

here is my code:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillHospitalCombo();
}
FillDoctorCombo();

}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{

//Got my id from Dropdownlist

zmn.Doctor_ID =Convert.ToInt32( DropDownList2.SelectedValue);
DropDownList2.Items.Clear();
string date;
//Get This format of date dd,mm,yy
date = DateTime.Today.Day.ToString();
date += "/" + DateTime.Today.Month.ToString();
date += "/" + DateTime.Today.Year.ToString();

zmn.Date = date;



#region My Old Way-->Now it"s just remarks
/*
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(StrCon);
//Execute GetHoleByDate Procedure
SqlCommand cmd = new SqlCommand("GiveDateFrom", con);
//telling him that we use SP
cmd.CommandType = CommandType.StoredProcedure;
//Create Parameter
SqlParameter MyParmeter = new SqlParameter();
//Scan Parameter Name,Type,Value
MyParmeter.ParameterName = "@ID";
MyParmeter.SqlDbType = SqlDbType.Int;
MyParmeter.Value = Doc.Doc_ID;
//AddParameter to cmd
cmd.Parameters.Add(MyParmeter);

SqlParameter MyParmeter2 = new SqlParameter();
MyParmeter2.ParameterName = "@Date";
MyParmeter2.SqlDbType = SqlDbType.NVarChar;
MyParmeter2.Value =zmn.Date;
//AddParameter to cmd
cmd.Parameters.Add(MyParmeter2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
//Fill DT
con.Open();
da.Fill(dt);
con.Close();
*/
#endregion

DataTable dt = new DataTable();
dt = zmn.GetDateFromTodayAndOn();
//Show Grid
GridView1.DataSource = dt;
GridView1.RowStyle.HorizontalAlign = HorizontalAlign.Center;
GridView1.DataBind();

}

public void FillHospitalCombo()
{
SqlConnection conn = new SqlConnection(StrCon);
SqlCommand cmd = new SqlCommand("GetAllHospital", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr;
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(new ListItem(dr["Hospital_Name"].ToString(), dr["Hospital_ID"].ToString()));
}
conn.Close();
}

public void FillDoctorCombo()
{
string strsql = @"SELECT Doctors.Doctor_ID, Doctors.First_Name
FROM Doctors INNER JOIN
Hospital ON Doctors.Hospital_ID = Hospital.Hospital_ID
WHERE Hospital.Hospital_ID = {0}";
strsql = string.Format(strsql, DropDownList1.SelectedIndex.ToString());
SqlConnection conn = new SqlConnection(StrCon);
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataReader dr;
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList2.Items.Add(new ListItem(dr["First_Name"].ToString(), dr["Doctor_ID"].ToString()));
}
conn.Close();
}


}
Jun 15 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jonathan | last post by:
Hi everyone, i have made two dropdownlist controls. On my first one, i have the country and on the second one, the state (for example) if i selected the United States country. So depending on wich...
7
by: Piotr Strycharz | last post by:
Hi all. I have problem with ASP:DropDownList. I do not know how to get its value. The DropDownList is populated in Page_Load method (from SQL Server). When this list is embeded in ASPX page...
4
by: Yahya Saad | last post by:
Dear All, I have an ASP.Net webform that includes a DropDownList (bound to a dataview including 6000 records) and a textbox that is updated according to the selection of an event at the...
2
by: Jennyfer J Barco | last post by:
Hello is there a way to create a dropdownlist inside a <input type='dropdownlist'> or there is an equivalent to create other than <asp:DropDownList id="drop1" runat="server"> . I need this because...
3
by: Olivier Verdin | last post by:
Hi, I have a page with several Textboxes and several DropDownList. When I click on a 'save' button, it creates a record in a database. The page works fine under Internet Explorer. It does...
0
by: Tand35006 | last post by:
Hi, I hope some one can help with this. I have a basic webform with 2 DropDownLists and a single DataGrid. What I am trying to do is populate the first DDList from a dataset on Form_Load. I then...
0
by: brian.newman | last post by:
I've created a dropdownlist whose values are drawn from an Access database. This list of items represents a user account. In the same record is a boolean field which records whether the user is...
3
by: =?Utf-8?B?RGVlcGE=?= | last post by:
Hi All, I have a webform in asp.net framework 1.1 where the dropdownlist is populated with list of locations from the database. The depending on the list selected, the user will be able to see...
5
by: =?Utf-8?B?Q2hyaXMgRy4=?= | last post by:
I'm trying to put conditional code in a dropdown that would check the value of the current ListItem and set Selected=true depending on that value, and Selected=false if it's any other value. Due...
11
by: harold.gimenez | last post by:
Hi group, I am trying to change the selection of an ASP Dropdownlist just like "Orange" is selected here: http://www.w3schools.com/js/tryit.asp?filename=try_dom_option_selected The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.