tlhorses Posted January 6, 2005 Posted January 6, 2005 This is driving me nuts and is probably simple. I have an employees form. When first loaded the combobox is loaded with a list of employees, names concatenated from the firstname and lastname columns of the table during the select and the uid from the database is set for the primary key of the datatable. The combo also has a blank line added so the admin will see that first and has to select a name from the combo. What I want is when a name is selected in the combo it then populates the other textboxes on that form with the info for that particular employee name. From there they will be able to edit items then update the db. So here are parts of my code: Populates the combobox: Private Sub PopulateCombo() 'called from formload 'sets primary key of datatable Dim dc(1) As DataColumn dc(0) = dtUpEmp.Columns("uid") dtUpEmp.PrimaryKey = dc 'binds data to combobox and fills box cboSelectEmp.DataSource = dtUpEmp cboSelectEmp.DisplayMember = "name" cboSelectEmp.ValueMember = "name" 'adds empty row to combox and sets box to that row Dim dr As DataRow dr = dsUpEmp.Tables(0).NewRow dr(1) = "" dsUpEmp.Tables(0).Rows.Add(dr) cboSelectEmp.SelectedIndex = cboSelectEmp.FindStringExact("") End Sub User selects a name in combobox and should display data in textboxes: Private Sub CboSelectEmpSelectedIndexChanged(sender As System.Object, e As System.EventArgs) Dim drCurrent As DataRow 'load the uid and find values Dim upempFindValue(0) As object upempFindValue(0) = cboSelectEmp.SelectedItem(0) drCurrent = dtUpEmp.Rows.Find(upempFindValue) 'populate the fields txtUser.Text = drCurrent("username").ToString txtFirstName.Text = drCurrent("firstname").ToString txtMi.Text = drCurrent("mi").ToString txtLastName.Text = drCurrent("lastname").ToString txtMaskDob.Text = drCurrent("dob").ToString txtMaskSsn.Text = drCurrent("ssn").ToString txtStreet.Text = drCurrent("street").ToString txtCity.Text = drCurrent("city").ToString txtState.Text = drCurrent("state").ToString txtMaskPhone.Text = drCurrent("phone").ToString txtMaskZip.Text = drCurrent("zip").ToString 'selects correct radio button Dim empstat As String empstat = drCurrent("emptype").ToString If empstat = "a" Then rdoAdmin.PerformClick() Else If empstat = "i" Then rdoinactive.PerformClick() Else rdoEmp.PerformClick() End If End Sub I get the following error on compile: error BC30574: Option Strict On disallows late binding. upempFindValue(0) = cboSelectEmp.SelectedItem(0) I assume that my error is because I Dim upempFindValue as just an object and not something specific. If that is the problem, I have no clue how to fix it. Any Help would be appreciated. Thanks tk Quote
Richard Crist Posted January 7, 2005 Posted January 7, 2005 Possible answer 'load the uid and find values Dim upempFindValue(0) As object upempFindValue(0) = cboSelectEmp.SelectedItem(0) drCurrent = dtUpEmp.Rows.Find(upempFindValue) I think you need to do something like this (I'm C# trying to do VB here): 'load the uid and find values Dim upempFindValue(0) As string upempFindValue(0) = cboSelectEmp.Text drCurrent = dtUpEmp.Rows.Find(upempFindValue) I think the Strict On is a good setting to teach discipline. The problem is that you must define the upempFindValue as a type and then use it. I would imagine the Find method looks up a string, hence my suggestion to use the Text value. I hope this is marginally helpful. :) Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
RobEmDee Posted January 7, 2005 Posted January 7, 2005 I believe your problem is that cboSelectEmp.SelectedItem(0) is returning a DataRowView......it may be easiest just to use the DataRowView. Try this: (sorry if my VB is shaky) Dim DataRowView rowSel As DataRowView rowSel = CType(cboSelectEmp.SelectedItem(0), DataRowView) 'You now have the underlying row txtUser.Text = rowSel("username").ToString txtFirstName.Text = rowSel("firstname").ToString txtMi.Text = rowSel("mi").ToString txtLastName.Text = rowSel("lastname").ToString ' etc Quote
tlhorses Posted January 7, 2005 Author Posted January 7, 2005 Thanks for the answers, but neither worked. Richard, on yours it would compile but when run I get an error of Input string is not in correct format displays when that form is loaded. Rob, on yours I get rowSel is not declared and still get the disallow late binding error. Any other thoughts? tk Quote
tlhorses Posted January 7, 2005 Author Posted January 7, 2005 Well a bit more on this. On Richard's suggestion if in the PopulateCombo sub if I change the ValueMember to "uid" then this works. Well except for my blank line I inserted which has no uid. But of course it displays the uid in the combobox which is not what is needed. Need that name there. The uid in the database is an integer. The name is actually created in the select statement since I didn't want it in the db. The select is in another sub for the update: cmdUpEmp = "Select *, Concat(lastname, ', ' , firstname) as name From emp Order By name" I can't find a way to get it to work with name because of the format error. tk Quote
RobEmDee Posted January 7, 2005 Posted January 7, 2005 I'd try the code I posted again after you made the ValueMember and DisplayMember different fields....I have never set these properties to the same field. Also, for you blank row, make the uid an integer....maybe -1. Quote
tlhorses Posted January 7, 2005 Author Posted January 7, 2005 Rob Well, the dim statement as you have it doesn't work. That way I get an end of statement expected. So I did one for DataRowView and one for rowSel thinking maybe both were supposed to be datarowviews? Not sure where you were headed with it. Anyway, either way I still get the late binding error on compile. tk Quote
Administrators PlausiblyDamp Posted January 7, 2005 Administrators Posted January 7, 2005 In the line Dim upempFindValue(0) As object what kind of objects are you expecting to store in the array? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
tlhorses Posted January 7, 2005 Author Posted January 7, 2005 (edited) In the line Dim upempFindValue(0) As object what kind of objects are you expecting to store in the array? This is longer than what you asked for but wanted to cover the process. In the dataset I selected * to get all data from the employee's table and created a name col which concatenates the lastname , firstname cols during the select. I then wanted to populate the combobox with the new name col. So I figured bind the control to the datatable since nothing in the combox will be edited. That I did in the code. When the user selects a name in the combo then it fills the textboxes on the form with that employee's information. That info could then be edited and saved. So for the combo, my thought was to get the SelectedItem and use that to determine which row and retrieve the info to populate the textboxes. SelectedItem is an object so figured I needed that to determine where I was in the datatable. EDIT: Forgot to mention, uid is the primary key of the database and I also made it the primary key of the ds. That is an integer. EDIT again: Sorry saw Richard's reply and got in a hurry. Anyway, my initial try at this was bind the combo with displaymember as name and valuemember as uid. tk Edited January 7, 2005 by tlhorses Quote
Richard Crist Posted January 7, 2005 Posted January 7, 2005 The Nitty Gritty tlhorses, In my signature at the bottom click on the link .NET Framework Homepage. Once there navigate to the DataRowCollections.Find method documentation. There it tells how to find stuff in a data table. Basically, you have to define a primary or other key (which I reckon you have done) and then use that key to find rows. Based on what I have studied since your question was posted (because I haven't studied data tables in depth until now) you must have a primary key object of type column(s) (or something like that) and then assign a text lookup value for each of the columns in your primary key lookup thingy. You then use the lookup thingy (which now has text values) as the parameter to the Rows.Find method. Bottom Line: You will get combobox.text, use that text to set the key object, use that object to find rows. This may be overkill or underkill, but I hope that it helps. :) Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
tlhorses Posted January 7, 2005 Author Posted January 7, 2005 tlhorses, In my signature at the bottom click on the link .NET Framework Homepage. Once there navigate to the DataRowCollections.Find method documentation. There it tells how to find stuff in a data table. Basically, you have to define a primary or other key (which I reckon you have done) and then use that key to find rows. :) Richard, If you are talking about this page: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowcollectionclassfindtopic.asp That is initially were I was going with my code but I get the late binding error where they dim the object. tk Quote
Richard Crist Posted January 7, 2005 Posted January 7, 2005 Like if I could read and stuff Richard, If you are talking about this page: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatarowcollectionclassfindtopic.asp That is initially were I was going with my code but I get the late binding error where they dim the object. tk Sorry! :( I should have noticed they were Dim'ing objects. The link below is more what I was thinking about. In this link they Dim the objects as strings ahead of time. That's what I was thinking of when I suggested getting the text value of the combobox and then using it in the find. Strong Typing Example in find usage I'm really trying to help....really! :rolleyes: Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
tlhorses Posted January 7, 2005 Author Posted January 7, 2005 Sorry! :( I should have noticed they were Dim'ing objects. The link below is more what I was thinking about. In this link they Dim the objects as strings ahead of time. That's what I was thinking of when I suggested getting the text value of the combobox and then using it in the find. Strong Typing Example in find usage I'm really trying to help....really! :rolleyes: Thanks Richard, I do appreciate it. Well, I hesitate to use the text in the combo because it is possible it may not be unique, such as two Jones, Mike. Tried following that example and using the valuemember which is the primary key uid but that didn't work. Couldn't cast it to string or integer, get the message Cast from type UInt32 to Integer is not valid. Not sure where the UInt is coming from, the value is stored as an integer in the db, maybe the dt stores it that way. So I tried dim as UInt32 and the cast as that. Well, that worked for the first load of the form but didn't work when selecting an different employee from the combo. Got the error, no default UInt32. You know you wouldn't think it would be this hard just to select the current row and fill the boxes. tk Quote
RobEmDee Posted January 8, 2005 Posted January 8, 2005 (edited) tl - I apologize for my VB retardation in my earlier post...I use C#. I simulated your code using Northwind and was able to get working what I tried to describe earlier Private Sub PopulateCombo() 'called from formload 'sets primary key of datatable Dim dc(1) As DataColumn dc(0) = dtTest.Columns("CustomerID") dtTest.PrimaryKey = dc 'binds data to combobox and fills box cboSelectEmp.DataSource = dtTest cboSelectEmp.DisplayMember = "ContactName" cboSelectEmp.ValueMember = "CustomerID" 'adds empty row to combox and sets box to that row Dim dr As DataRow dr = DataSet11.Tables(0).NewRow dr("CustomerID") = -1 dr("ContactName") = "" DataSet11.Tables(0).Rows.Add(dr) cboSelectEmp.SelectedIndex = cboSelectEmp.FindStringExact("") End Sub Private Sub cboSelectEmp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSelectEmp.SelectedIndexChanged Dim rowSel As DataRowView rowSel = CType(cboSelectEmp.SelectedItem, DataRowView) Me.txtContactName.Text = CType(rowSel("ContactName"), String) Me.txtCity.Text = CType(rowSel("City"), String) Me.txtContactTitle.Text = CType(rowSel("ContactTitle"), String) 'You could alternatively use the DataRowView.Row property to access the underlying row and cast it into a typed row from your DataSet (if your DataSet is typed) Dim row As DataSet1.CustomersRow = CType(rowSel.Row, DataSet1.CustomersRow) Me.txtContactName.Text = row.ContactName Me.txtCity.Text = row.City Me.txtContactTitle.Text = row.ContactTitle 'etc End Sub I think the problem you were having in your original code is that you were using an indexer on the SelectedItem property (SelectedItem(0)). SelectedItem returns a DataRowView for the whole selected DataRow. SelectedValue returns what you have set for the ComboBox.ValueMember property of the selected DataRow. SelectedText returns what you have set for the ComboBox.DisplayMember property of the selected DataRow. If you are not completely attached to VB, I would suggest moving to C#. You will end up typing a lot less code. :p Edited January 8, 2005 by RobEmDee Quote
tlhorses Posted January 8, 2005 Author Posted January 8, 2005 tl - I apologize for my VB retardation in my earlier post...I use C#. I simulated your code using Northwind and was able to get working what I tried to describe earlier I think the problem you were having in your original code is that you were using an indexer on the SelectedItem property (SelectedItem(0)). It may be a stretch for you, but I would suggest moving to C#. You will end up typing a lot less code. :p Rob, That is exactly it. The indexer was the problem. Wasn't retardation on your part, more on my part I think. Thank you very much for the help. I was beginning to think I was going to have to go back to vb6. Went this afternoon looking for Programming ADO.Net by O'Reilly but Barnes & Noble was out. Have the Programming .Net Window Applications from them and it shows both C# and VB.Net so I will probably end up giving the C# a shot. Doesn't look so bad and the book is pretty good. Richard, Thanks for plugging away at it also. Good to have folks to help out. Knowing my somtimes thick skull, I may be back. Thanks again guys. tk Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.