|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Dynamic List Box
<i><b>Originally posted by : Lee (lee.liddell@hq.hqusareur.army.mil)</b></i><br /><br />Hello, I am trying to use a function or a sub procedure to generate a list box. <br />This one code will be used to create multiple list boxes. <br />The problem is I cannot pass out the arraylist() from a function or cannot assign it to a control in a function. <br /><br />How do I pass in the control name and assign the arraylist to that control????<br /><br />Thanks,<br />~Lee<br /><br /><%@ Page Language="vb" %><br /><%@ import Namespace="System.Data" %><br /><%@ import Namespace="System.Data.sqlclient" %><br /><script runat="server"><br /> <br />Public Sub CreateMySqlDataReader(mySelectQuery As String, _<br /> myConnectionString As String, myControl as DropDownList?????????)<br /> <br /> Dim myConnection As New SqlConnection(myConnectionString)<br /> Dim myCommand As New SqlCommand(mySelectQuery, myConnection)<br /> myCommand.Connection.Open()<br /> Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)<br /> Dim myList As New ArrayList()<br /> While myReader.Read()<br /> myList.Add(myReader.GetString(0))<br /> End While<br /><br /> '!!!!!!These are the lines I want to change depending on what control name I pass<br /> myControl.DataSource = myList<br /> myControl.DataBind()<br /> <br /> myReader.Close()<br /> myConnection.Close()<br /> End Sub<br /> <br /> <br /> sub Page_Load(Source as Object, E as EventArgs)<br /> Dim strConn as string<br /> strConn ="server=server;uid=test;pwd=test"<br /> <br /> If Not IsPostBack Then<br /> Dim SQL as String<br /> SQL = "SELECT Room_number FROM ROOM"<br /> CreateMySqlDataReader(SQL, strConn, "DropDownList1")<br /> SQL = "SELECT POC FROM POC"<br /> CreateMySqlDataReader(SQL, strConn, "DropDownList2")<br /> End If<br /> <br /> end sub <br /></script><br /><html><br /><head><br /> <title>Room Scheduler</title><br /></head><br /><body><br /> <p><br /> Room Scheduler <br /> </p><br /> <form id="NameForm" runat="server"><br /> <p>Room Number: <br /> <asp
ropDownList id="DropDownList1" runat="server"></asp ropDownList></p><br /> <asp ropDownList id="DropDownList2" runat="server"></asp ropDownList></p><br /> <p><input type="submit" value="Submit" /><br /> <input type="reset" value="Clear" /></p><br /> </form><br /></body><br /></html> |
|
#2
|
|||
|
|||
|
<i><b>Originally posted by : Lisa (ldwelch@dcr.net)</b></i><br />Make sure that you import:<br />System.Web.UI.WebControls<br />and you should be able to declare the argument without any problems.<br /><br />Also - you may assign a sqldatareader directly to the DropDownLists datasource - without going through and creating the arraylist.<br /><br />ie.<br /> myControl.DataSource = myReader<br /> myControl.DataTextField = DisplayField<br /> myControl.DataValueField = sValueField<br /> myControl.DataBind()<br /><br />Note - The bind even autoCloses the reader for you.<br /><br /><br /><br /><br />------------<br />Lee at 7/31/2002 9:52:57 AM<br /><br /><br />Hello, I am trying to use a function or a sub procedure to generate a list box. <br />This one code will be used to create multiple list boxes. <br />The problem is I cannot pass out the arraylist() from a function or cannot assign it to a control in a function. <br /><br />How do I pass in the control name and assign the arraylist to that control????<br /><br />Thanks,<br />~Lee<br /><br /><%@ Page Language="vb" %><br /><%@ import Namespace="System.Data" %><br /><%@ import Namespace="System.Data.sqlclient" %><br /><script runat="server"><br /> <br />Public Sub CreateMySqlDataReader(mySelectQuery As String, _<br /> myConnectionString As String, myControl as DropDownList?????????)<br /> <br /> Dim myConnection As New SqlConnection(myConnectionString)<br /> Dim myCommand As New SqlCommand(mySelectQuery, myConnection)<br /> myCommand.Connection.Open()<br /> Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConne ction)<br /> Dim myList As New ArrayList()<br /> While myReader.Read()<br /> myList.Add(myReader.GetString(0))<br /> End While<br /><br /> '!!!!!!These are the lines I want to change depending on what control name I pass<br /> myControl.DataSource = myList<br /> myControl.DataBind()<br /> <br /> myReader.Close()<br /> myConnection.Close()<br /> End Sub<br /> <br /> <br /> sub Page_Load(Source as Object, E as EventArgs)<br /> Dim strConn as string<br /> strConn ="server=server;uid=test;pwd=test"<br /> <br /> If Not IsPostBack Then<br /> Dim SQL as String<br /> SQL = "SELECT Room_number FROM ROOM"<br /> CreateMySqlDataReader(SQL, strConn, "DropDownList1")<br /> SQL = "SELECT POC FROM POC"<br /> CreateMySqlDataReader(SQL, strConn, "DropDownList2")<br /> End If<br /> <br /> end sub <br /></script><br /><html><br /><head><br /> <title>Room Scheduler</title><br /></head><br /><body><br /> <p><br /> Room Scheduler <br /> </p><br /> <form id="NameForm" runat="server"><br /> <p>Room Number: <br /> <asp
ropDownList id="DropDownList1" runat="server"></asp ropDownList></p><br /> <asp ropDownList id="DropDownList2" runat="server"></asp ropDownList></p><br /> <p><input type="submit" value="Submit" /><br /> <input type="reset" value="Clear" /></p><br /> </form><br /></body><br /></html> |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Dynamic List Box |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|