C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Populating a Combobox in ASP from the Database


Posted Date: 27 Apr 2008    Resource Type: Code Snippets    Category: Winforms DataGrid

Posted By: Rekha        Member Level: Gold
Rating:     Points: 8



This code shows how to populate a ComboBox from the database easily. The LoadData function is where the data is selected from the database and then in the Page_Load the DataReader returned from the LoadData function is assigned to the ComboBox's DataSource

Source Code

Imports System.Data
Imports System.Data.OleDb

Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents cboLocations As System.Web.UI.WebControls.DropDownList
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load

cboLocations.DataSource = LoadData()
cboLocations.DataTextField = "Customer"
cboLocations.DataBind()
End Sub

'This function is used to populate the combobox
Private Function LoadData() As OleDbDataReader
Dim strSQL As String

strSQL = "SELECT Customer from Customers"

Dim myConnection As OleDbConnection = New _
OleDbConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)

myConnection.Open()
Dim result As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

Return result
End Function

End Class






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Handle the Datagridview Combox SelectionChangeCommitted event in Windows Form
Previous Resource: How to add the values in several columns and give the result in another column of same row
Return to Discussion Resource Index
Post New Resource
Category: Winforms DataGrid


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

it help desk

Contact Us    Privacy Policy    Terms Of Use