C# .NET - Dropdownlist problem

Asked By sonali gupta on 07-Dec-10 06:28 AM
Hi All,
 i am developing a website with gridview control.
i have three dropdownlists for city, state and pincode respectively in editItemTemplate and FooterTemplate.
i want to fill state and pincode dropdown on the basis of city selected.
but i am not able to do it.
pointer in the right direction will be highly appreciated.
thanks in advance!!
reply soon!!!
Balram Chavan replied to sonali gupta on 07-Dec-10 06:33 AM
On selectedIndexchanged event of city dropdown list you can write your code for filling state and pincode for selected city.
Venkat K replied to sonali gupta on 07-Dec-10 06:44 AM
Use the Dropdown2 selectedindexchanged event and you
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
      DropDownList ddl1 = (DropDownList)sender;
      GridViewRow row = (GridViewRow)ddl1.NamingContainer;
      if (row != null)
      {
        DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
        {
          //call the method for binding the second DDL based on the selected item on the first DDL
          DataTable dt = BindDropDownList(ddl1.SelectedItem.Text);
          ddl2.DataTextField = "Field1";
          ddl2.DataValueField = "Field2";
          ddl2.DataBind();
        }
      }
    }
private DataTable BindDropDownList(string field)
    {
      DataTable dt = new DataTable();
      SqlConnection connection = new SqlConnection(GetConnectionString());
      try
      {
        connection.Open();
        string sqlStatement = "SELECT * FROM TableName WHERE Field1 = @Value";
        SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlCmd.Parameters.AddWithValue("@Value", field);
        sqlDa.Fill(dt);
      }

      catch (System.Data.SqlClient.SqlException ex)
      {
        string msg = "Fetch Error:";
        msg += ex.Message;
        throw new Exception(msg);
      }
      finally
      {
        connection.Close();
      }

      return dt;

    }
Nowshad M replied to sonali gupta on 07-Dec-10 06:51 AM
Hi,
Try like this...

 protected void GrdTabRow_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (((DropDownList)e.Row.FindControl("ddlCOuntry")).SelectedItem.Value != "Select")
                BindDropdownWWC(((DropDownList)e.Row.FindControl("ddlState")), "select * from tblState where Category=" + ((DropDownList)e.Row.FindControl("ddlCountry")).SelectedItem.Value + "", "StateName", "StateID", "Select");

        }
    }
//Here BindDropdownWWC is the funtion to bind state
public void BindDropdownWWC(DropDownList ddl, string qryInput, string textField, string valueField, string zerothIndex)
    {
        try
        {
            SqlDataAdapter da = new SqlDataAdapter(qryInput, objDA.AccessConnection());
            DataTable dt = new DataTable();
            da.Fill(dt);
            ddl.DataSource = dt;
            ddl.DataTextField = textField;
            ddl.DataValueField = valueField;

            ddl.DataBind();
            ddl.Items.Insert(0, zerothIndex);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
Rohan Dave replied to sonali gupta on 07-Dec-10 07:00 AM

you need to handle the SelectionIndexChanged event of your city dropdownlist. Then you need to use NamingContainer to get the reference of your GridViewRow. Once you will get the reference of your Gridiview, you can easily get other two dropdownlist and bind it accordingly...

See the below code :

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

{
  GridViewRow objGVR = (GridViewRow)(((Control)Sender).NamingContainer);
  DropDownList objDrpState = (DropDownList)objGVR.FindControl("drpstate");
  DropDownList objDrpPinCode = (DropDownList)objGVR.FindControl("drppincode");

  // code to bind State dropdown based on City
  objAdpt = new SqlDataAdapter("Select StateId, StateName From States Where CityId = ' " + drpCity.SelectedItem.Value.ToString( ) + " ' ", objCon);
  objDt = new DataTable( );
  objDrpState.DataSource = objDt;
  objDrpState.DataTextField = "StateName";
  objDrpState.DataValueField = "StateId";
  objDrpState.DataBind();

  //code to bind Pincode dropdown based on city.. code will be same

}

sonali gupta replied to Rohan Dave on 08-Dec-10 01:13 AM

hi...
my selectedindexchanged code is like this
protected
void cmbCity_SelectedIndexChanged(object sender, EventArgs e)

{

DropDownList cmbState = new DropDownList();

DropDownList cmbPincode = new DropDownList();

DropDownList city = (DropDownList)sender;

DataSet ds1 = new DataSet();

if (city.SelectedItem.Value != null)

{

ds1 = std.FillCombo(city.SelectedItem.Value);

cmbState = (DropDownList)studentGrid.FindControl("cmbState");

cmbPincode = (DropDownList)studentGrid.FindControl("cmbPincode");

cmbState.DataSource = ds1.Tables["State"];

cmbState.DataBind();

cmbPincode.DataSource = ds1.Tables["Pincode"];

cmbPincode.DataBind();

ds1.Dispose();

}

}
but it is throwing exception at

cmbState.DataSource = ds1.Tables["State"];  line...ds1 has table in it, but still showing exception. how to handle it?
 

Rohan Dave replied to sonali gupta on 08-Dec-10 06:01 AM
What the error is coming up ? anotherthing is no need to create the object of cmbState and cmbPincode.
sonali gupta replied to Rohan Dave on 08-Dec-10 11:21 PM
hi rohan...
i have sovled this problem ,,
actualy the problem was that the FindControl method of gridview was not able to find the dropdownlists for state and pincode.
now i have made the following changes in my code and its working well-
   DropDownList city = (DropDownList)sender;
      GridViewRow row = (GridViewRow)city.NamingContainer;
      if (row.RowType == DataControlRowType.Footer)
      {
        DropDownList cmbStatenew = (DropDownList)row.FindControl("cmbStatenew");
        //cmbState = (DropDownList)studentGrid.FindControl("cmbState");
        DropDownList cmbPincodenew = (DropDownList)row.FindControl("cmbPincodenew");
        DataSet ds = new DataSet();
        if (city.SelectedItem.Value != null)
        {
          ds = std.FillCombo(city.SelectedItem.Value);
          //cmbState.DataSource = ds1.Tables["State"];
          cmbStatenew.DataSource = ds.Tables[0];
          cmbStatenew.DataBind();
          cmbPincodenew.DataSource = ds.Tables[1];
          cmbPincodenew.DataBind();
          ds.Dispose();
        }
      }
      else
      {
        DropDownList cmbState = (DropDownList)row.FindControl("cmbState");
        DropDownList cmbPincode = (DropDownList)row.FindControl("cmbPincode");
        DataSet ds = new DataSet();
        if (city.SelectedItem.Value != null)
        {
          ds = std.FillCombo(city.SelectedItem.Value);
          //cmbState.DataSource = ds1.Tables["State"];
          cmbState.DataSource = ds.Tables[0];
          cmbState.DataBind();
          cmbPincode.DataSource = ds.Tables[1];
          cmbPincode.DataBind();
          ds.Dispose();
        }
      }


thanks...