A newbie here trying to bind a datatable to a dropdownlist. My page loads with no errors but dropdownlist is blank. On aspx page dropdownlist is named lotnumberlist. This is the code behind.
Imports System Imports System.Data.SqlClient ' Imports System.Configuration Imports System.Data Imports System.Linq Public Class AddItem Inherits System.Web.UI.Page Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim Constring As String = ConfigurationManager.ConnectionStrings("DBConn").ConnectionString Dim objCommand As New SqlCommand objCommand.CommandText = "Select Max(LotNumber) As MaxLotNumber From Items" objCommand.Connection = New SqlConnection(Constring) objCommand.Connection.Open() Dim objDataReader As SqlDataReader = objCommand.ExecuteReader() Dim CurrentLotNumber As Int32 Dim TopLotNumber As Int32 If objDataReader.HasRows Then Do While objDataReader.Read() CurrentLotNumber = Convert.ToInt32(objDataReader(0)) TopLotNumber = CurrentLotNumber + 5 Response.Write("CurrentLotNumber " & CurrentLotNumber & " Top Lot Number " & TopLotNumber) Loop 'Dim Lotnumbers() As Integer = Enumerable.Range(CurrentLotNumber, TopLotNumber).ToArray() Dim LotTable As New DataTable() 'LotTable.Columns.Add("LotNumber") LotTable.Columns.Add(New DataColumn("LotNumber", GetType(Int32))) For i As Integer = CurrentLotNumber To TopLotNumber 'Response.Write(i) LotTable.Rows.Add(i) Next Dim LotNumberView As DataView = New DataView(LotTable) If LotTable.Rows.Count > 2 Then Dim Count As Int32 = LotTable.Rows.Count Response.Write(" Records Found" & Count) End If Dim LotNumberList As DropDownList = New DropDownList() ' Set the properties for the DropDownList control. LotNumberList.DataSource = LotTable LotNumberList.DataTextField = "LotNumber" LotNumberList.DataValueField = "LotNumber" LotNumberList.DataBind() Else Response.Write("No rows returned.") End If End Sub End Class
Last edited by a moderator: