.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Display data from database in dropdownlist

Posted By: Catherine     Posted Date: April 07, 2011    Points:5   Category :VB.Net
i like to display the branch name in dropdownlist from table branch

Responses
Author: shankey             
Posted Date: April 07, 2011     Points: 20   

hi,

following is the sample code which binds data to dropdownlist.Data is fetched from sql server


Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Public Partial Class _Default
Inherits System.Web.UI.Page
Private con As SqlConnection
Private cmd As New SqlCommand()
Private da As SqlDataAdapter
Private ds As New DataSet()

Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
rebind()
End If
End Sub

Public Sub rebind()
Try
con = New SqlConnection("Data Source=MCN0100;Initial Catalog = DropDowndataBase; Uid=sa; Pwd=")
cmd.CommandText = "select * from Record"
cmd.Connection = con
con.Open()
DroplistData.DataSource = cmd.ExecuteReader()
DroplistData.DataTextField = "Name"
DroplistData.DataBind()
Catch ex As Exception
Response.Write(ex.StackTrace)
End Try
End Sub

Public Sub datashow()
con = New SqlConnection("Data Source=MCN0100;Initial Catalog = DropDowndataBase; Uid=sa; Pwd=")
Dim nameshow As String = DroplistData.Text.ToString().Trim()
cmd.CommandText = "select * from Record where Name='" & nameshow & "' "
cmd.Connection = con
da = New SqlDataAdapter(cmd)
da.Fill(ds)
con.Open()
cmd.ExecuteNonQuery()
datagrid.DataSource = ds
datagrid.DataBind()
con.Close()
End Sub

Protected Sub GridShow_Click(sender As Object, e As EventArgs)
datashow()
End Sub
End Class


Shankey
Author: Catherine             
Posted Date: April 07, 2011     Points: 20   

Thank you



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend