newbie question

  • Thread starter Thread starter Troy
  • Start date Start date
T

Troy

Hello,

I'm learning asp.net so please excuse the question: I;d list to populate the
listbox control with data from sql and I have no idea on how to do it.
Below is my code that displays it in a datagrid.

TIA

<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.sqlclient" %>
<%@ Page Language="vb" Debug="true" %>

<script language="vb" runat="server">
'Place the following in a server side script in your aspx or in the
codebehind:
Sub Page_Load(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter
MyConnection = new
sqlConnection("server=ServerA;uid=app;pwd=password;database=northwind")
MyCommand = New SqlDataAdapter("select CompanyName from Customers",
MyConnection)
DS = new DataSet()
MyCommand.Fill(ds, "Customers")
MyDataGrid.DataSource=ds.Tables("Customers").DefaultView
MyDataGrid.DataBind()
End Sub

</script>


<html>
<body>
<form runat="server">
<asp:DataGrid id="MyDataGrid" runat="Server" font-size="10" />
</form>
</body>
 
The following source code may be helpful to you

<%@ Page Language="VB" EnableSessionState="False"
EnableViewState="False" debug="False" trace="False"
strict="True" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language=VB runat=server>

Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = New SqlConnection
(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim mySQL as string = "Select au_id, au_lname, au_fname
from Authors"
Dim myCmd as New SQLCommand(mySQL,myConnection)
myConnection.Open()

MyDropDownList.DataSource = myCmd.ExecuteReader()
myDropDownList.DataBind()
myConnection.Close()
End Sub

</script>
<html>
<head><title>DropListBox using
SQLDataReader</title></head>

<body>
<asp:dropdownlist id="MyDropDownList" DataValueField
= "au_lname" DataTextField = "au_id" runat="server"/>
</body>
</html>


-Gopi
 
Thanks - I really appreciate it.


S Gopikrishna said:
The following source code may be helpful to you

<%@ Page Language="VB" EnableSessionState="False"
EnableViewState="False" debug="False" trace="False"
strict="True" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language=VB runat=server>

Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As SqlConnection = New SqlConnection
(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim mySQL as string = "Select au_id, au_lname, au_fname
from Authors"
Dim myCmd as New SQLCommand(mySQL,myConnection)
myConnection.Open()

MyDropDownList.DataSource = myCmd.ExecuteReader()
myDropDownList.DataBind()
myConnection.Close()
End Sub

</script>
<html>
<head><title>DropListBox using
SQLDataReader</title></head>

<body>
<asp:dropdownlist id="MyDropDownList" DataValueField
= "au_lname" DataTextField = "au_id" runat="server"/>
</body>
</html>


-Gopi
 
Back
Top