Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having 2 webpages 1 patient page 2 Doctor Page

In patient page i am having drop down list like Location : [ ]

in doctors page i am having another drop down list like Area : [ ]

Here i am having 2 tables like location and sub location in the database

in location table i am having columns like 'LocationID' 'Location'

in sub location table i am having columns like 'SublocationID', 'LocationID' and 'SubLocation'

now i have written the code to populate the location dropdown list from the table please check the below code:

C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               LocationBind();
               
           }
       }


protected void LocationBind()
        {
            SqlConnection conn = new SqlConnection(connstr);

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Location";

            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            ddlLocation.DataSource = reader;
            ddlLocation.DataTextField = "Location";
            ddlLocation.DataValueField = "LocationID";
            ddlLocation.DataBind();
            ddlLocation.Items.Insert(0, new ListItem("Select", "-1"));
            conn.Close();
            reader.Close();
        }




now by using the LocationID i have to populate the related items in sublocation Dropdown list that is present in doctor page. Please help me out about this... thnks
Posted

hi to access data of 1 page into another.
there are 3 ways according to me:
1) Creating Cookies
2) Creating Session Variables
3) Sql State Variables

use 1 & 2 they are comparitively easy.

here are examples to use session variables

Manage ASP.NET Session Variables using the Facade Design Pattern[^]

http://msdn.microsoft.com/en-us/library/ms178581.aspx[^]

Thanks
 
Share this answer
 
Comments
vinay7.ra 18-May-12 3:28am    
Thanks i got that but if you send me some other links related to this its very helpful for me.... once again thanks for you answer...
preet88 18-May-12 4:44am    
this a link i found it to be very precise and clear
http://www.w3schools.com/asp/asp_sessions.asp
however it is for asp but it hardly matters, it'll work well in asp.net as well.

this is another link which is gives you a detail on cookies
http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies
hey Try this and make changes as you need in your code
private void BindDropDownList1()

    {

        DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(GetConnectionString());

        try

        {

            connection.Open();

            string sqlStatement = "SELECT ColumnName * FROM TableName";

            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);

            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)

            {

                DropDownList1.DataSource =dt;

                DropDownList1.DataTextField = "ColumnName"; // the items to be displayed in the list items

                DropDownList1.DataValueField = "ColumnName"; // the id of the items displayed

                DropDownList1.DataBind();

            }

        }

        catch (System.Data.SqlClient.SqlException ex)

        {

            string msg = "Fetch Error:";

            msg += ex.Message;

            throw new Exception(msg);

        }

        finally

        {

            connection.Close();

        }

    }



    private void BindDropDownList2(string field)

    {

        DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(GetConnectionString());

        try

        {

            connection.Open();

            string sqlStatement = "SELECT * FROM Table WHERE ColumnName = @Value1";

            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);

            sqlCmd .Parameters.AddWithValue("@Value1", field) 

            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)

            {

                DropDownList2.DataSource =dt;

                DropDownList2.DataTextField = "ColumnName"; // the items to be displayed in the list items

                DropDownList2.DataValueField = "ColumnName; // the id of the items displayed

                DropDownList2.DataBind();

            }

        }

        catch (System.Data.SqlClient.SqlException ex)

        {

            string msg = "Fetch Error:";

            msg += ex.Message;

            throw new Exception(msg);

        }

        finally

        {

            connection.Close();

        }

    }



   protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindDropDownList1();

        }

    }



   protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)

   {

   

      BindDropDownList2(DropDownList1.SelectedItem.Text);

   

   }
 
Share this answer
 
Hi Try this
it will defiantly work.

Your First page code is fine.
send location id of selected item of location dropdown to second page by state management concepts.

then in second page do same code as your first page code.

just replce dropdown name with your another dropdown list name and procdure must contain query like "SELECT * FROM Table WHERE locationid= @Value1"

paas that location id while binding dropdown on second page as parameter @Value1
 
Share this answer
 
The same question is asked in CP.. kindly go to the below URL.. you will definitely get your answer..

Connectivity of Dropdown lists with two tables of SQL[^]


make it answer..if you got your answer..dont forget to rate ..:)
 
Share this answer
 
If user must (or will) navigate to Doctor page directly after selecting a Location
you can send LocationID as parameter in QueryString.
C#
Response.Redirect("DoctorPageUrl.aspx?LocationID=" + ddlLocation.SelectedValue);

And in Doctor page you will use it to populate your other drop down list.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack)
    { 
        if(Request.QueryString["LocationID"]!= null)
        {
            //I advise you to use SqlDataSource instead of doing it "by hand".
            //It has properties to directly connect its parameters to various
            //elements including controls' properties and QueryString 
            FillDoctorsByLocation(int.Parse(Request.QueryString["LocationID"]);
        }
    }
}


If that is not the case I strongly recommend preet88's solutions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900