Ver Mensaje Individual
  #23 (permalink)  
Antiguo 18/10/2011, 12:25
Avatar de stuart_david3
stuart_david3
 
Fecha de Ingreso: agosto-2011
Mensajes: 215
Antigüedad: 13 años, 2 meses
Puntos: 1
Respuesta: Generar listbox en base a otro listbox en una base de datos sql, asp.net

Gracias por tu respuesta Aquaventus, mira lo intente sin el botón así:

Código ASP:
Ver original
  1. <%@ Import Namespace="System.Data" %>
  2.  
  3. <%@ Import Namespace="System.Data.SQLClient" %>
  4. <script language="vbscript" runat="server">
  5. Sub Page_Load(Sender As Object, E As EventArgs)
  6. If Not Page.IsPostBack Then
  7.         Dim values As ArrayList = New ArrayList()  
  8.         Dim values2 As ArrayList = New ArrayList()
  9.         Dim conn As SqlConnection =
  10.         New SqlConnection("server=myhost;database=mydb;User ID=myuser;Password=mypass;Trusted_Connection=no")
  11.         Dim reader As SqlDataReader
  12.         Dim sql As String =
  13.          "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES GROUP BY TABLE_NAME"
  14.         Dim dr As New SqlCommand(sql, conn)
  15.         conn.Open()
  16.  reader = dr.ExecuteReader()
  17.         While reader.Read()
  18.             values.Add(reader.Item("TABLE_NAME").ToString())
  19.         End While
  20.         reader.Close()
  21.         DIM CADENA AS STRING = Convert.ToString(lb1.SELECTEDITEM)
  22.   Dim sql2 As String =
  23.          "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =' & CADENA & ' GROUP BY COLUMN_NAME"
  24.         Dim dr2 As New SqlCommand(sql2, conn)
  25.                  
  26.         reader = dr2.ExecuteReader()
  27.         While reader.Read()
  28.             values2.Add(reader.Item("COLUMN_NAME").ToString())
  29.         End While
  30.         lb1.DataSource = values
  31.         lb1.DataBind()
  32.         lb2.DataSource = values2
  33.         lb2.DataBind()
  34.         reader.Close()
  35.         conn.Close()
  36.     End If
  37.     End Sub
  38. </script>
  39. <html>
  40. <head>
  41. <title>Tablas y Columnas</title>
  42. </head>
  43. <body>
  44.     <asp:Label ID="Label1" runat="server" Text="RPE del Empleado"></asp:Label>
  45. <form id="Form1" method="post" runat="server" name="ListEnlaza">
  46. <asp:ListBox id="lb1" SelectionMode="Multiple" runat="server" Height="217px"
  47.     Width="196px" AutoPostBack="True"/>
  48.     <asp:ListBox id="lb2" SelectionMode="Multiple" runat="server" Height="147px"
  49.     Width="196px" AutoPostBack="True"/>
  50. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  51. <asp:Button ID="Button1" runat="server" Text="Button" />
  52. </form>
  53. </body>
  54. </html>

Y cuando le daba clic al listbox2 me aparecía el dibujo del puntero del mouse cuando carga pero menos de un segundo y no me llenaba nada, intente habilitando el AutoPostBack para el list2, para el list1, para ambos y nada.... También lo intente con el botón, y nada:

Código ASP:
Ver original
  1. <%@ Import Namespace="System.Data" %>
  2.  
  3. <%@ Import Namespace="System.Data.SQLClient" %>
  4. <script language="vbscript" runat="server">
  5. Sub Page_Load(Sender As Object, E As EventArgs)
  6.         If Not Page.IsPostBack Then
  7.         Dim reader As SqlDataReader
  8.         Dim values As ArrayList = New ArrayList()
  9.         Dim conn As SqlConnection =
  10.          New SqlConnection("server=myhost;database=mydb;User ID=myuser;Password=mypass;Trusted_Connection=no")
  11.         Dim sql As String =
  12.          "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES GROUP BY TABLE_NAME"
  13.         Dim dr As New SqlCommand(sql, conn)
  14.         conn.Open()
  15.  reader = dr.ExecuteReader()
  16.         While reader.Read()
  17.             values.Add(reader.Item("TABLE_NAME").ToString())
  18.         End While
  19.         lb1.DataSource = values
  20.         lb1.DataBind()
  21.         reader.Close()
  22.         End If
  23.     End Sub
  24. </script>
  25. <html>
  26. <head>
  27. <title>Tablas y Columnas</title>
  28. </head>
  29. <body>
  30.     <asp:Label ID="Label1" runat="server" Text="RPE del Empleado"></asp:Label>
  31. <form id="Form1" method="post" runat="server" name="ListEnlaza">
  32. <asp:ListBox id="lb1" SelectionMode="Multiple" runat="server" Height="217px"
  33.     Width="196px" AutoPostBack="True"/>
  34.     <asp:ListBox id="lb2" SelectionMode="Multiple" runat="server" Height="147px"
  35.     Width="196px" AutoPostBack="True"/>
  36. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  37. <asp:Button ID="Button1" runat="server" Text="Button" OnClick="list2"/>
  38. <script language="vbscript" runat="server">
  39. sub list2 (ByVal sender As Object, ByVal e As EventArgs)
  40.        Dim reader As SqlDataReader
  41.         Dim values2 As ArrayList = New ArrayList()
  42.         Dim conn As SqlConnection =
  43.         New SqlConnection("server=myhost;database=mydb;User ID=myuser;Password=mypass;Trusted_Connection=no")
  44.         conn.Open()
  45.         DIM CADENA AS STRING = Convert.ToString(lb1.SELECTEDITEM)
  46.   Dim sql2 As String =
  47.          "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =' & CADENA & ' GROUP BY COLUMN_NAME"
  48.         Dim dr2 As New SqlCommand(sql2, conn)
  49.                  
  50.         reader = dr2.ExecuteReader()
  51.         While reader.Read()
  52.             values2.Add(reader.Item("COLUMN_NAME").ToString())
  53.         End While
  54.        
  55.         lb2.DataSource = values2
  56.         lb2.DataBind()
  57.         reader.Close()
  58.         conn.Close()
  59. End Sub
  60. </script>
  61. </form>
  62. </body>
  63. </html>

De nuevo gracias!!!...