Has anyone an example of filling a combo box with a data reader?
Thanks
Vayse 18 20026
Here's one; you'll need at the top of your class code:
Imports System.Data.OleDb
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
Try
Dim dbpath As String = "c:\t\db1.mdb"
Dim dbConn As New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbpath)
dbConn.Open()
Dim dbCommand As New OleDbCommand( _
"SELECT * FROM Customers", dbConn)
Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
Do While dbDR.Read
ComboBox1.Items.Add(dbDR("CUSCODE"))
Loop
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error")
End Try
End Sub
Just to add to KitWest's excellent code, I would create a "ListBoxItem"
class so that you can store both a value and displayitem.
Greg
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim dbpath As String = "c:\t\db1.mdb"
Dim dbConn As New OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & dbpath)
dbConn.Open()
Dim dbCommand As New OleDbCommand( _
"SELECT * FROM Customers", dbConn)
Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
ComboBox1.BeginUpdate()
Do While dbDR.Read
ComboBox1.Items.Add(New ListBoxItem(Cint(dbDR("CUSCODE")),
dbDR("CUSNAME").ToString))
Loop
ComboBox1.EndUpdate()
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error")
End Try
End Sub
End Class
Public Class ListBoxItem
Private listItemData As Object
Private listItemText As String
' This is what is displayed in the ComboBox drop-down
Public Overrides Function ToString() As String
Return listItemText
End Function
Public Sub New(ByVal itemData As Object, ByVal itemText As String)
listItemData = itemData
listItemText = itemText
End Sub
Public ReadOnly Property Data() As Object
Get
Data = listItemData
End Get
End Property
Public ReadOnly Property Text() As String
Get
Text = listItemText
End Get
End Property
End Class
Thanks guys.
Another question on this.
Theres several combo boxes on this form, so for speed reasons I'd like to
remove the table adapters and binding sources, and just use Data readers.
Me.CostCentresTableAdapter.Fill(Me.AssetsDataSet.C ostCentres)
So I have a databound comCostCentres with these properties
DateSource = CostCentresBindingSource
Display Member = CostName
Display Value = CostCode
Selectecd Value = AssetsBindingSource - CostCentre
I have the combo box filling correctly, thanks to your code. But it doesn't
display the select CostCentre correctly, and its not updating the Asset.
What step am I missing?
Thanks
Vayse
Try
' Fill the combo boxes
Me.comCostCentre.DisplayMember = "CostName"
Me.comCostCentre.ValueMember = "CostCode"
Dim dbConn As New OleDbConnection(conCONNECT)
dbConn.Open()
Dim dbCommand As New OleDbCommand("SELECT * FROM CostCentres",
dbConn)
Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
comCostCentre.BeginUpdate()
Do While dbDR.Read
comCostCentre.Items.Add(New ListBoxItem(dbDR("CostCode"),
dbDR("CostName")))
Loop
comCostCentre.EndUpdate()
dbDR.Close()
dbConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error")
End Try
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:e6**************@TK2MSFTNGP09.phx.gbl... Just to add to KitWest's excellent code, I would create a "ListBoxItem" class so that you can store both a value and displayitem.
Greg
Just to make it clear - I've removed the CostCentresTableAdapter and those
databound properties. Thats as they were before I used the Datareader.
"Vayse" <Va***@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Thanks guys. Another question on this. Theres several combo boxes on this form, so for speed reasons I'd like to remove the table adapters and binding sources, and just use Data readers.
Me.CostCentresTableAdapter.Fill(Me.AssetsDataSet.C ostCentres)
So I have a databound comCostCentres with these properties DateSource = CostCentresBindingSource Display Member = CostName Display Value = CostCode Selectecd Value = AssetsBindingSource - CostCentre
I have the combo box filling correctly, thanks to your code. But it doesn't display the select CostCentre correctly, and its not updating the Asset. What step am I missing? Thanks Vayse
Try
' Fill the combo boxes Me.comCostCentre.DisplayMember = "CostName" Me.comCostCentre.ValueMember = "CostCode"
Dim dbConn As New OleDbConnection(conCONNECT) dbConn.Open()
Dim dbCommand As New OleDbCommand("SELECT * FROM CostCentres", dbConn) Dim dbDR As OleDb.OleDbDataReader = dbCommand.ExecuteReader
comCostCentre.BeginUpdate()
Do While dbDR.Read comCostCentre.Items.Add(New ListBoxItem(dbDR("CostCode"), dbDR("CostName"))) Loop
comCostCentre.EndUpdate()
dbDR.Close() dbConn.Close()
Catch ex As Exception MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error") End Try
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message news:e6**************@TK2MSFTNGP09.phx.gbl... Just to add to KitWest's excellent code, I would create a "ListBoxItem" class so that you can store both a value and displayitem.
Greg
"Vayse" <Va***@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Selectecd Value = AssetsBindingSource - CostCentre
I have the combo box filling correctly, thanks to your code. But it doesn't display the select CostCentre correctly, and its not updating the Asset. What step am I missing?
I am not sure I follow, but here is some more code I use:
' set the combobox to a default display text
' this seems to automagically set the selected value as well
comCostCentre.Text = "a CostName is the list"
' how to retrieve the value
Dim value as string = CType(comCostCentre.SelectedItem,
ListBoxItem).Data.ToString
HTH,
Greg
"Vayse" <Va***@nospam.nospam> wrote in message
news:e1*************@TK2MSFTNGP11.phx.gbl... Just to make it clear - I've removed the CostCentresTableAdapter and those databound properties. Thats as they were before I used the Datareader.
"Vayse" <Va***@nospam.nospam> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl... Thanks guys. Another question on this. Theres several combo boxes on this form, so for speed reasons I'd like to remove the table adapters and binding sources, and just use Data readers.
Me.CostCentresTableAdapter.Fill(Me.AssetsDataSet.C ostCentres)
So I have a databound comCostCentres with these properties DateSource = CostCentresBindingSource Display Member = CostName Display Value = CostCode Selectecd Value = AssetsBindingSource - CostCentre
I have the combo box filling correctly, thanks to your code. But it doesn't display the select CostCentre correctly, and its not updating the Asset. What step am I missing?
I guess I don't know your problem domain well enough. I see you have a
combobox name comConstCentres (I assume this contain costcenters, with
Name/Value pairs; CostName/CostCode). What do you mean when you say "its
not updating the Asset". I would take it to mean, setting the selected
value on your combobox is not sticking.?
I know you are not using the above code anymore, but what in the world did
the below line do? :)
Selectecd Value = AssetsBindingSource - CostCentre
What does it mean to be using a subtractation between objects here?
Greg
Selected Value = AssetsBindingSource - CostCentre
If you look at the property, thats the way its displayed! Not a calculation
or anything, just means Assets.CostCentre
Also displayed that way if you click the smart tag. Kind of strange really.
And yes, the selected value in the combo box is not sticking. Also, its not
been filled correctly.
So when I navigate to a new record, the combo box is blank.
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... "Vayse" <Va***@nospam.nospam> wrote in message news:e1*************@TK2MSFTNGP11.phx.gbl... Just to make it clear - I've removed the CostCentresTableAdapter and those databound properties. Thats as they were before I used the Datareader.
"Vayse" <Va***@nospam.nospam> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl... Thanks guys. Another question on this. Theres several combo boxes on this form, so for speed reasons I'd like to remove the table adapters and binding sources, and just use Data readers.
Me.CostCentresTableAdapter.Fill(Me.AssetsDataSet.C ostCentres)
So I have a databound comCostCentres with these properties DateSource = CostCentresBindingSource Display Member = CostName Display Value = CostCode Selectecd Value = AssetsBindingSource - CostCentre
I have the combo box filling correctly, thanks to your code. But it doesn't display the select CostCentre correctly, and its not updating the Asset. What step am I missing?
I guess I don't know your problem domain well enough. I see you have a combobox name comConstCentres (I assume this contain costcenters, with Name/Value pairs; CostName/CostCode). What do you mean when you say "its not updating the Asset". I would take it to mean, setting the selected value on your combobox is not sticking.?
I know you are not using the above code anymore, but what in the world did the below line do? :)
Selectecd Value = AssetsBindingSource - CostCentre
What does it mean to be using a subtractation between objects here?
Greg
"Vayse" <Va***@nospam.nospam> wrote in message
news:OP*************@TK2MSFTNGP10.phx.gbl... Selected Value = AssetsBindingSource - CostCentre
If you look at the property, thats the way its displayed! Not a calculation or anything, just means Assets.CostCentre Also displayed that way if you click the smart tag. Kind of strange really.
And yes, the selected value in the combo box is not sticking. Also, its not been filled correctly. So when I navigate to a new record, the combo box is blank.
That sounds like you are still using DataBinding. I am not sure you can
manually populate a combobox like that and have databinding still function.
Hopefully somebody else has some ideas.
Greg
Yeah, I'm still using databinding on the form. Just wanted to "kind of"
unbind the combo boxes. I thought it would be possible.
Thanks for your help though - very useful for all my other forms, which
aren't data bound. And I'm beginning to regret ever making this form data
bound!
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:eY**************@TK2MSFTNGP12.phx.gbl... "Vayse" <Va***@nospam.nospam> wrote in message news:OP*************@TK2MSFTNGP10.phx.gbl... Selected Value = AssetsBindingSource - CostCentre
If you look at the property, thats the way its displayed! Not a calculation or anything, just means Assets.CostCentre Also displayed that way if you click the smart tag. Kind of strange really.
And yes, the selected value in the combo box is not sticking. Also, its not been filled correctly. So when I navigate to a new record, the combo box is blank.
That sounds like you are still using DataBinding. I am not sure you can manually populate a combobox like that and have databinding still function.
Hopefully somebody else has some ideas.
Greg
Hi Vayse,
I am not sure I understand your problem very well. Based on my knowledge,
we can not use SqlDataReader to do databinding in Winform, can you show me
how do you do this? If you just only use SqlDataReader to populate the
DataSet, then the databinding should have nothing to do with the
SqlDataReader, databinding only related with the datasource(DataSet) and
the control(ComboBox), once these 2 are OK, the databinding should be OK.
Can you provide a little sample project to demonstrate your problem, then I
can understand the context better. Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Hi Jeffrey
I can't provide a working project, because I can't get it working! :)
But I've put what I have here http://h1.ripway.com/vayse/ProductCombo.zip
In the example, I'd like to remove
CategoriesTableAdapter
CategoriesBindingSource
comCatgIDBinding is based on these.
Instead I'd like to use comCatgDataReader, based on a Data Reader.
thanks for your help
Vayse
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:i3**************@TK2MSFTNGXA02.phx.gbl... Hi Vayse,
I am not sure I understand your problem very well. Based on my knowledge, we can not use SqlDataReader to do databinding in Winform, can you show me how do you do this? If you just only use SqlDataReader to populate the DataSet, then the databinding should have nothing to do with the SqlDataReader, databinding only related with the datasource(DataSet) and the control(ComboBox), once these 2 are OK, the databinding should be OK.
Can you provide a little sample project to demonstrate your problem, then I can understand the context better. Thanks
Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Vayse,
You know that if you are reading in this way information, that is already in
your datatable, than it will probably consume a lot of time doing it this
way.
By the way changing the reading from a datataadapter for datareading will
probably result in not more than 10 milliseconds. Be aware that for reading
the dataadapter uses just a datareader object.
Just to help you to overthink what you are doing
Cor
Hi Vayse,
Thanks for your feedback.
However, only a project is not helpful for us to track this problem, can
you give some problem context description to this issue? This will save
community a lot of time and give the community members a good
understanding.
Also, can you tell us what is your current problem? The current information
I can get really makes me confused. Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:tu**************@TK2MSFTNGXA02.phx.gbl...
Also, can you tell us what is your current problem? The current information I can get really makes me confused. Thanks
Heh, I confused myself at this stage! ;)
My questions are these:
1) On a databound form, must the combo boxes be databound?
2) If they don't have to be databound, how do I get the combo box to update
the field.
Hi Vayse,
Thanks for your feedback, see inline. 1) On a databound form, must the combo boxes be databound?
Of cause, we can use combobox without databinding. We can just use
ComboBox.Items.Add method to add the display items programmatically.
2) If they don't have to be databound, how do I get the combo box to
update the field.
This depends on what does "field" pointed to, certain field in underlying
DataTable/DataSet or certain field in some table in DataBase.
Normally, after we selected an item, click a update button to update the
underlying datasource, we should use some primary key information(this
primary key information should store with other information in the item of
the combobox) to look the related row in DataTable/DataSet, then change the
corresponding column value in that row to your selected value.
To update the database table, you can use ADO.net with SQL statement to
update the change data into the database.
Hope this helps
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Ah, that changes things. Thanks Cor.
"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl... Vayse,
You know that if you are reading in this way information, that is already in your datatable, than it will probably consume a lot of time doing it this way.
By the way changing the reading from a datataadapter for datareading will probably result in not more than 10 milliseconds. Be aware that for reading the dataadapter uses just a datareader object.
Just to help you to overthink what you are doing
Cor
Thanks for your help on this.
Its a lot clearer now.
I have no more questions on this!
""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:$0**************@TK2MSFTNGXA02.phx.gbl... Hi Vayse,
Thanks for your feedback, see inline.
1) On a databound form, must the combo boxes be databound? Of cause, we can use combobox without databinding. We can just use ComboBox.Items.Add method to add the display items programmatically. 2) If they don't have to be databound, how do I get the combo box to
update the field. This depends on what does "field" pointed to, certain field in underlying DataTable/DataSet or certain field in some table in DataBase. Normally, after we selected an item, click a update button to update the underlying datasource, we should use some primary key information(this primary key information should store with other information in the item of the combobox) to look the related row in DataTable/DataSet, then change the corresponding column value in that row to your selected value. To update the database table, you can use ADO.net with SQL statement to update the change data into the database.
Hope this helps
Best regards, Jeffrey Tan Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights.
Hi Vayse,
Ok, if you have any further question, please feel free to post, we will
help you. Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mike P |
last post by:
I'm using 1 connection to open 3 separate data readers to read data into
3 data grids. However, when I try to use the 2nd reader it says that
the first reader must be closed. So I use Close() to...
|
by: mhnazly |
last post by:
i'm trying to read data from SQL Server database using
data reader and assigned it to a label in my asp.net web
application. but when the button is clicked, nothing
appears. please help, thanks.
...
|
by: atif |
last post by:
hi i hav a prob with data reader. i hav a 2 column in a data table n i
hav 2 drop down lists on my page. Now i want to connect my first drop
down with the first column of the data table. Though i...
|
by: Ram |
last post by:
I am using ADO.Net data reader to retrieve data from main
frame. I am getting timestamp which is 26 (yyyy-mm-dd hh-
mm-ss.123456) bytes as sqltimestamp data type. I am using
data reader to...
|
by: Jim Heavey |
last post by:
I am new to SqlServer, have been using Oracle in a prior life.
I have written a very simple stored procedure in SQL Server and it (the
query) looks like the following...
Select AD_ID,...
|
by: simonZ |
last post by:
I have data reader(I can't create data adapter, because I'm using
command.BeginExecuteReader and EndExecuteReader)
What is the best way to save data reader to table inside the data set?
Any...
|
by: =?Utf-8?B?cm9kY2hhcg==?= |
last post by:
hey all,
how do you resolve this problem?
i have a public procedure in my DataAccessLayer that gets a SqlDataReader
how do i close the reader from inside the DataAccessLayer if I'm returning...
|
by: charvi |
last post by:
i hae a ole data reader which contains records extracted from Query.how can i check for end of file in data reader.and i want to know the number of records present in data reader
thanks in advance
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| |