Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/11/2012, 17:19
Avatar de Darsk
Darsk
 
Fecha de Ingreso: mayo-2011
Mensajes: 39
Antigüedad: 13 años
Puntos: 0
Pregunta Broblemas dropdownlist anidados.

Tengo 3 tablas, region , ciudad y comuna, quiero que todas vayan enlazadas cosa que si seleccionas uno cambie el valor del otro, pero tengo una falla la cual no he podido identificar, puse el metodo en el load si me cargo con una variable local , el asunto es que al moento de abrir la aplicacion tengo 2 errores,unoes que carga normalmente pero el dropdownlist no carga al seleccionar el primero, el segundo es que me dice que la conexion no ce cerro normalmente, adjunto el codigo y de antemano les aghradesco mucho.

Código c#:
Ver original
  1. public partial class frCliente : System.Web.UI.Page
  2. {
  3.     SqlConnection conn = new SqlConnection(@"Data Source=NICOLAS-PC\SQLEXPRESS;Integrated Security=True");
  4.    
  5.    
  6.     protected void Page_Load(object sender, EventArgs e)
  7.     {
  8.         if (!IsPostBack)
  9.         {
  10.             cargarRegiones();
  11.            
  12.         }
  13.        
  14.  
  15.     }
  16.    
  17.     protected void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
  18.     {
  19.  
  20.         string id_region = cbRegion.SelectedValue.ToString();
  21.         SqlCommand cmd = new SqlCommand("select * from ciudad where id_region_fk = '" + id_region + "'", conn);
  22.         SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
  23.         DataSet ds = new DataSet();
  24.         da.Fill(ds);
  25.         this.cbCiudad.DataSource = ds;
  26.         this.cbCiudad.DataValueField = "id_ciudad";
  27.         this.cbCiudad.DataTextField = "nombre";
  28.         this.cbCiudad.DataBind();
  29.         if (cbCiudad.Items.Count != 0)
  30.         {
  31.             string id_ciudad = cbCiudad.SelectedValue.ToString();
  32.             cargarComunas(id_ciudad);
  33.             conn.Close();
  34.  
  35.         }
  36.         else
  37.         {
  38.             cbCiudad.Items.Clear();
  39.         }
  40.  
  41.        
  42.  
  43.         //string id_region = Convert.ToString(cbRegion.SelectedValue);
  44.         //cargarCiudades(id_region);
  45.     }
  46.     protected void cbCiudad_SelectedIndexChanged(object sender, EventArgs e)
  47.     {
  48.         string id_ciudad = Convert.ToString(cbCiudad.SelectedValue);
  49.         cargarComunas(id_ciudad);
  50.     }
  51.  
  52.     public void cargarRegiones()
  53.     {
  54.        
  55.         SqlCommand cmd = new SqlCommand("select * from region",conn);
  56.         SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
  57.         DataSet ds = new DataSet();
  58.         da.Fill(ds);
  59.         this.cbRegion.DataSource = ds;
  60.         this.cbRegion.DataValueField = "id_region";
  61.         this.cbRegion.DataTextField = "nombre";
  62.         this.cbRegion.DataBind();
  63.        
  64.  
  65.         if (cbRegion.Items.Count != 0)
  66.         {
  67.             string id_region = Convert.ToString(cbRegion.SelectedValue);
  68.             cargarCiudades(id_region);
  69.             conn.Close();
  70.  
  71.         }
  72.        
  73.        
  74.     }
  75.    
  76.      
  77.     public void cargarCiudades(string id_region)
  78.     {
  79.        
  80.        
  81.         SqlCommand cmd = new SqlCommand("select * from ciudad where id_region_fk = '"+id_region+"'",conn);
  82.         SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
  83.         DataSet ds = new DataSet();
  84.         da.Fill(ds);
  85.         this.cbCiudad.DataSource = ds;
  86.         this.cbCiudad.DataValueField = "id_ciudad";
  87.         this.cbCiudad.DataTextField = "nombre";
  88.         this.cbCiudad.DataBind();
  89.         if (cbCiudad.Items.Count != 0)
  90.         {
  91.             string id_ciudad = cbCiudad.SelectedValue.ToString();
  92.             cargarComunas(id_ciudad);
  93.             conn.Close();
  94.              
  95.         }
  96.         else
  97.         {
  98.             cbCiudad.Items.Clear();
  99.         }
  100.  
  101.        
  102.  
  103.     }
  104.  
  105.     public void cargarComunas(String id_ciudad)
  106.     {
  107.        
  108.         SqlCommand cmd = new SqlCommand("select * from comunas where id_ciudad_fk ='"+id_ciudad+"'", conn);
  109.         SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, conn);
  110.         DataSet ds = new DataSet();
  111.         da.Fill(ds);
  112.         this.cbComuna.DataSource = ds;
  113.         this.cbComuna.DataValueField = "id_comuna";
  114.         this.cbComuna.DataTextField = "nombre";
  115.         this.cbComuna.DataBind();
  116.         if(cbComuna.Items.Count !=0)
  117.         {
  118.             string id_comuna = Convert.ToString(cbComuna.SelectedValue);
  119.        
  120.         }
  121.         else
  122.         {
  123.             cbComuna.Items.Clear();
  124.         }
  125.        
  126.     }
  127.    
  128.    
  129. }
__________________
El amor mas fiel y duradero es el amor que uno se tiene.