Weird happenings with DropDownLists and DataTables

heversin

New Member
Hi,<BR><BR>How is it possible for two DropDownLists to be databound to identical DataTables yet display them differently?<BR><BR>I used VB.NET to create a DataTable (containing a list of names) from an Access DB, and display this DataTable via a DropDownList. Then I inserted a new row at position 0 into the DataTable, so that the initially displayed value would be "SELECT A NAME".<BR><BR>So far, so good.<BR><BR>Then, having learning something about creating custom objects, I created my own "Database" object, with two methods, one to create DataTables, and another to insert rows into those DataTables at the specified position. I used this custom object to pull the data out of Access, put it in a DataTable, insert a new row at position 0, and display the DataTable via a DropDownList.<BR><BR>Now, the problem.<BR><BR>The trouble is, when I did everything using my custom object, although EXACTLY THE SAME DataTable is created as first time round, it does NOT display the same way! The added row "SELECT A NAME" is displayed at the BOTTOM of the DropDownList, instead of being the first item (which is what I want).<BR><BR>I can't understand why this is happening. <BR><BR>I even went to the trouble of iterating through both DataTables (one created with the custom object, one normally) and displaying the contents on the screen so I could visually check that the content of both DataTables is identical.<BR><BR>Can anyone tell me what's going on here? Maybe it's something really simple....<BR><BR>Thanks,<BR><BR>JON<BR><BR>__________________________________________________ _____________________________<BR><BR><BR><BR>+++++++++++++++++++++<BR>MainPage.aspx<BR>+++++++++++++++++++++<BR><BR><BR><%@ Page Language="VB" Debug="true" Trace="false" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.OleDb" %><BR><BR><script runat="server"><BR>sub Page_Load(obj as object, e as eventargs)<BR><BR>'*** Set variables for my custom object (code in file Database.vb) <BR>dim objDatabase as new BusinessObjects.Database<BR>objDatabase.ConnectionString = ConfigurationSettings.AppSettings("ConnectionString")<BR>Dim ds as new DataSet()<BR>objDataBase.DataSet = ds<BR><BR>'*** Create DataTable via custom object method<BR>DDL1.DataSource = objDatabase.CreateDataTableDefaultView("ExampleDataTable", "SELECT tblPeople.PersonID, tblPeople.LastName FROM tblPeople;")<BR><BR>'*** insert row at position 0 into DataTable via custom object method<BR>objDatabase.InsertIntoDataTable("ExampleDataTable", 0, "PersonID", -1, "LastName", "SELECT A NAME")<BR><BR>'*** Bind DDL1 to this DataTable<BR>DDL1.DataTextField = "LastName"<BR>DDL1.DataValueField = "PersonID"<BR>DDL1.DataBind()<BR><BR><BR><BR>'*** Create a DataTable via normal VB.NET (no custom object)<BR>Dim objConn as New OleDbConnection ( ConfigurationSettings.AppSettings("ConnectionString") )<BR>Dim objAdapter as New OleDbDataAdapter ( "SELECT tblPeople.PersonID, tblPeople.LastName FROM tblPeople;", objConn )<BR>objAdapter.Fill(ds, "ExampleDataTable2")<BR><BR>'*** insert row at position 0 into DataTable via normal VB.NET<BR>Dim DefaultItemParentDataRow as DataRow = ds.Tables("ExampleDataTable2").NewRow()<BR>DefaultItemParentDataRow("PersonID") = -1<BR>DefaultItemParentDataRow("LastName") = "SELECT A NAME"<BR>ds.Tables("ExampleDataTable2").Rows.InsertAt(DefaultItemParentDataRow, 0)<BR><BR>'*** Bind DDL2 to this second DataTable<BR>DDL2.DataSource = ds.Tables("ExampleDataTable2")<BR>DDL2.DataTextField = "LastName"<BR>DDL2.DataValueField = "PersonID"<BR>DDL2.DataBind()<BR><BR>'*** Iterate through and display content of both DataTables to prove they are identical <BR>Dim myRow as DataRow<BR>For each myRow in ds.Tables("ExampleDataTable").Rows<BR> Response.Write("Person ID: " & myRow.Item("PersonID") & " and LastName is " & myRow.Item("LastName") & "<BR>")<BR>next myRow<BR><BR>Response.Write("<hr>")<BR><BR>For each myRow in ds.Tables("ExampleDataTable2").Rows<BR> Response.Write("Person ID: " & myRow.Item("PersonID") & " and LastName is " & myRow.Item("LastName") & "<BR>")<BR>next myRow<BR><BR><BR>end sub <BR><BR></script><BR><BR><html><body><BR><BR><form runat="server"><BR>DDL1 - populated via custom object objDatabase ("SELECT A NAME" displays at END of DDL):<BR><BR><BR><asp:DropDownList runat="server" id="DDL1" /><BR><hr><BR>DDL2 - populated via normal vb.net code ("SELECT A NAME" displays at BEGINNING of DDL):<BR><BR><BR><asp:DropDownList runat="server" id="DDL2" /><BR></form><BR><BR></body></html><BR><BR><BR><BR><BR>+++++++++++++++++++++++++++++++++<BR>Database.vb<BR>+++++++++++++++++++++++++++++++++<BR><BR><BR>Imports System<BR>Imports System.Data<BR>Imports System.Data.OleDb<BR>Imports System.Collections<BR><BR>Namespace BusinessObjects<BR><BR> Public Class Database<BR> public ConnectionString as String<BR> public DataSet as DataSet<BR> private objConn as OleDbConnection<BR> private objCmd as OleDbCommand<BR> private objAdapter as OleDbDataAdapter<BR> <BR> <BR> public function CreateDataTableDefaultView(DataTableName as String, strQuery as string) as DataView<BR> try<BR> objConn = new OleDbConnection(ConnectionString)<BR> objAdapter = New OleDbDataAdapter(strQuery, objConn)<BR> objAdapter.Fill(DataSet, DataTableName)<BR> return DataSet.Tables(DataTableName).DefaultView<BR> catch ex as OleDbException<BR> return nothing<BR> end try<BR> end function<BR> <BR> <BR> <BR> public function InsertIntoDataTable(DataTableName as String, insertAt as Integer, FirstColumnName as String, FirstColumnValue as Integer, SecondColumnName as String, SecondColumnValue as String)<BR> Dim myDataRow as DataRow = DataSet.Tables(DataTableName).NewRow()<BR> myDataRow(FirstColumnName) = FirstColumnValue<BR> myDataRow(SecondColumnName) = SecondColumnValue<BR> DataSet.Tables(DataTableName).Rows.InsertAt(myData Row, insertAt) <BR> End function<BR> <BR> <BR> End Class<BR><BR>End Namespace<BR><BR><BR><BR>
 
Back
Top