Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having a 2 drop-down list in my page like 1.Location :[ ]
2.Sublocation : [ ] and

I am having 2 tables in my database like Location_Lookup and Sublocation_Lookup,
in Location_Lookup having columns like 'LocationID' 'Location' and
in Sublocation_Lookup havin columns like 'SublocationID' 'LocationID' and 'Sublocation'.

Then I have created a stored Procedure to fetch the sublocation using the LocationID
like
SQL
create procedure PopSublocation @location int as
begin
Select Sublocation from Sublocation_LookUp where LocationID = @location


now for fetching the location items to the Location dropdown list i have written the code like..
C#
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();
        }


Here i am using stored procedure for getting locationID and Location and in the program for ddlLocation.DataValueField= "LocationID" i am passing.

Now for fetching the related sublocation list in the Sublocation:[ ] dropdown i am writting the code in the "protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)" and the code is

C#
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
      {
          SqlConnection conn = new SqlConnection(connstr);
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = conn;
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.CommandText = "PopSublocation";
          cmd.Parameters.AddWithValue("@location",ddlLocation.SelectedItem.Value);
          ddlSublocation.Items.Insert(0, new ListItem("Select", "-1"));
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          da.Fill(ds);
          ddlSublocation.DataSource = ds;
          ddlSublocation.DataTextField = "Sublocation";
          ddlSublocation.DataValueField = "SublocationID";
          ddlSublocation.DataBind();

      }


Here i am passing the parameter of LocationID,
but here my problem is I am not getting the sublocation details in the Sublocation dropdown list.. please help me out for this... thanks
Posted

Try This:

SQL
create procedure PopSublocation @location int as
begin
Select Sublocation from Sublocation_LookUp where LocationID = @location
end


SQL
protected void LocationBind()
        {
            SqlConnection conn = new SqlConnection(connstr);
 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Location";
 
            conn.Open();
DataSet ds=new DataSet();
SqlDataAdapter adp=new SqlDataAdapter(cmd,conn);
adp.fill=ds; 
            ddlLocation.DataSource = ds;
            ddlLocation.DataTextField = "Location";
            ddlLocation.DataValueField = "LocationID";
            ddlLocation.DataBind();
            ddlLocation.Items.Insert(0, new ListItem("Select", "-1"));
            conn.Close();
            reader.Close();
        }

SQL
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection(connstr);
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = conn;
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "PopSublocation";
           cmd.Parameters.AddWithValue("@location",ddlLocation.SelectedValue);
           ddlSublocation.Items.Insert(0, new ListItem("Select", "-1"));
           SqlDataAdapter da = new SqlDataAdapter(cmd,conn);
           DataSet ds = new DataSet();
           da.Fill(ds);
           ddlSublocation.DataSource = ds;
           ddlSublocation.DataTextField = "Sublocation";
           ddlSublocation.DataValueField = "SublocationID";
           ddlSublocation.DataBind();
           
       }
 
Share this answer
 
v5
Use
C#
ddlSublocation.DataSource = ds.Table[0];
 
Share this answer
 
v2
Rather than this
C#
cmd.Parameters.AddWithValue("@location",ddlLocation.SelectedItem.Value);

Try this
C#
cmd.Parameters.AddWithValue("@location",Convert.ToInt32(ddlLocation.SelectedValue));
 
Share this answer
 
v2
Comments
vinay7.ra 20-May-12 0:59am    
Thanks i got the solution....
bhagirathimfs 21-May-12 1:34am    
WC :)
C#
sqlCommand.Parameters.Add("@location", SqlDbType.Int).Value =  Convert.ToInt32(ddlLocation.SelectedValue);
 
Share this answer
 
v2

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