newbie question

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>
 
S

S Gopikrishna

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
 
T

Troy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top