473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

2FieldsInListBox

Hi,
I'm trying to insert 2 fields of same ROW in a ListBox.
Following is the code in VB I had done previously: It worked
pretty straight forward.
------------------------In VB-------------------
If (DataContact.EOF = True) Then Combo2.Text = "No Item"
Do While Not DataContact.EOF
Combo2.AddItem DataContact.Fields("Contact_Name_VC")
Combo2.AddItem
DataContact.Fields("Sub_Contact_Name_VC")
Combo2.ItemData(Combo2.NewIndex) =
DataContact.Fields("Contact_ID")
DataContact.MoveNext
Loop
'End If
'close the recordsrt
DataContact.Close
Set DataContact = Nothing
========================In VB.NET=========
Try

ContactMgrConnection.Open()

' Response.Write(strFeeMgrQuery)

ContactMgrDatareader =
ContactMgrCommand.ExecuteReader(CommandBehavior.Cl oseConnection)

ListBox1.DataSource = ContactMgrDatareader

ListBox1.DataValueField = "Contact_ID"

ListBox1.DataTextField = "Contact_Name_VC"

ListBox1.DataTextField = "Sub_Contact_Name_VC"

ListBox1.DataBind()

Catch myException As Exception
'I'm able to trap only Sub_contact_Elements not Contact_elements
========================================
But I'm unable to execute the same in vb.net.
Any feed backe will be a real help.
Thanks.

Nov 21 '05 #1
2 979
Hi,

Try something like this.

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

'Put user code to initialize the page here

If Not Me.IsPostBack Then

BindDataToGrid()

End If

End Sub

Private Sub BindDataToGrid()

Dim strConn As String

Dim conn As OleDb.OleDbConnection

Dim cmdOrders As OleDb.OleDbCommand

Dim dr As OleDb.OleDbDataReader

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = C:\Northwind.mdb;"

conn = New OleDb.OleDbConnection(strConn)

cmdOrders = New OleDb.OleDbCommand("Select (FirstName + ' ' + LastName) as
name from Employees", conn)

conn.Open()

dr = cmdOrders.ExecuteReader(CommandBehavior.CloseConne ction)

ListBox1.DataSource = dr

ListBox1.DataTextField = "name"

ListBox1.DataBind()

End Sub

Ken

--------------------

"gurvar" <gu****@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
Hi,
I'm trying to insert 2 fields of same ROW in a ListBox.
Following is the code in VB I had done previously: It worked
pretty straight forward.
------------------------In VB-------------------
If (DataContact.EOF = True) Then Combo2.Text = "No Item"
Do While Not DataContact.EOF
Combo2.AddItem DataContact.Fields("Contact_Name_VC")
Combo2.AddItem
DataContact.Fields("Sub_Contact_Name_VC")
Combo2.ItemData(Combo2.NewIndex) =
DataContact.Fields("Contact_ID")
DataContact.MoveNext
Loop
'End If
'close the recordsrt
DataContact.Close
Set DataContact = Nothing
========================In VB.NET=========
Try

ContactMgrConnection.Open()

' Response.Write(strFeeMgrQuery)

ContactMgrDatareader =
ContactMgrCommand.ExecuteReader(CommandBehavior.Cl oseConnection)

ListBox1.DataSource = ContactMgrDatareader

ListBox1.DataValueField = "Contact_ID"

ListBox1.DataTextField = "Contact_Name_VC"

ListBox1.DataTextField = "Sub_Contact_Name_VC"

ListBox1.DataBind()

Catch myException As Exception
'I'm able to trap only Sub_contact_Elements not Contact_elements
========================================
But I'm unable to execute the same in vb.net.
Any feed backe will be a real help.
Thanks.
Nov 21 '05 #2
Hi,
I'm trying to get one below the other say Main Contact listed after sub
contact.
Not Main Contact+Sub Contact.
Do let me knwo how to go about extracting one field below another.

Thanks

"Ken Tucker [MVP]" wrote:
Hi,

Try something like this.

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

'Put user code to initialize the page here

If Not Me.IsPostBack Then

BindDataToGrid()

End If

End Sub

Private Sub BindDataToGrid()

Dim strConn As String

Dim conn As OleDb.OleDbConnection

Dim cmdOrders As OleDb.OleDbCommand

Dim dr As OleDb.OleDbDataReader

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = C:\Northwind.mdb;"

conn = New OleDb.OleDbConnection(strConn)

cmdOrders = New OleDb.OleDbCommand("Select (FirstName + ' ' + LastName) as
name from Employees", conn)

conn.Open()

dr = cmdOrders.ExecuteReader(CommandBehavior.CloseConne ction)

ListBox1.DataSource = dr

ListBox1.DataTextField = "name"

ListBox1.DataBind()

End Sub

Ken

--------------------

"gurvar" <gu****@discussions.microsoft.com> wrote in message
news:1A**********************************@microsof t.com...
Hi,
I'm trying to insert 2 fields of same ROW in a ListBox.
Following is the code in VB I had done previously: It worked
pretty straight forward.
------------------------In VB-------------------
If (DataContact.EOF = True) Then Combo2.Text = "No Item"
Do While Not DataContact.EOF
Combo2.AddItem DataContact.Fields("Contact_Name_VC")
Combo2.AddItem
DataContact.Fields("Sub_Contact_Name_VC")
Combo2.ItemData(Combo2.NewIndex) =
DataContact.Fields("Contact_ID")
DataContact.MoveNext
Loop
'End If
'close the recordsrt
DataContact.Close
Set DataContact = Nothing
========================In VB.NET=========
Try

ContactMgrConnection.Open()

' Response.Write(strFeeMgrQuery)

ContactMgrDatareader =
ContactMgrCommand.ExecuteReader(CommandBehavior.Cl oseConnection)

ListBox1.DataSource = ContactMgrDatareader

ListBox1.DataValueField = "Contact_ID"

ListBox1.DataTextField = "Contact_Name_VC"

ListBox1.DataTextField = "Sub_Contact_Name_VC"

ListBox1.DataBind()

Catch myException As Exception
'I'm able to trap only Sub_contact_Elements not Contact_elements
========================================
But I'm unable to execute the same in vb.net.
Any feed backe will be a real help.
Thanks.

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.