rickb Posted May 29, 2003 Posted May 29, 2003 I have a button whose text property is passed to a textbox, txtpick. The value of the textbox is used as the parameter in a SQL query, which in turn is supposed to fill a listbox. When I click on, for example, the "A" button, the txtpick textbox is filled, but the listbox isn't poplulated. Below is the code I have for the sub (LoadList). Could someone please tell me what I'm missing or doing wrong? I have followed the advice of other posters who have answered similar questions, but I'm having no luck. Public Sub LoadList() oledbconn.Open() Dim dsUserList As New DataSet() Dim daUserList As New OleDbDataAdapter("SELECT UserProfile, trim( LastName)" & ", " & "trim(FirstName) AS FullName,LastName,FirstName,LOC,Building,DeviceID FROM tblAS400Users WHERE LastName LIKE '" & Me.txtPick.Text & "%' ORDER BY LastName,FirstName", oledbconn) Try daUserList.Fill(dsAS400, "tblAS400USers") With Me.lstUsers .DataSource = dsUserList.Tables("tblAS400Users") .DisplayMember = "FullName" .ValueMember = "UserProfile" End With Catch ex As Exception MsgBox(ex.ToString) Exit Sub End Try 'Enable the Delete and Edit Button cmdDelete.Enabled = True cmdEdit.Enabled = True oledbconn.Close() End Sub Here is the code for the "A" button: Private Sub cmdA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdA.Click Me.txtPick.Text = cmdA.Text LoadList() End Sub I do not receive an error of any type from the "Try-Catch" statement; the listbox just doesn't fill. Thanks for any help. Quote
*Experts* jfackler Posted May 29, 2003 *Experts* Posted May 29, 2003 Public Sub LoadList() oledbconn.Open() Dim dsUserList As New DataSet() Dim daUserList As New OleDbDataAdapter("SELECT UserProfile, trim( LastName)" & ", " & "trim(FirstName) AS FullName,LastName,FirstName,LOC,Building,DeviceID FROM tblAS400Users WHERE LastName LIKE '" & Me.txtPick.Text & "%' ORDER BY LastName,FirstName", oledbconn) Try daUserList.Fill(dsAS400, "tblAS400USers") 'should this be dsUserList? With Me.lstUsers .DataSource = dsUserList.Tables("tblAS400Users") .DisplayMember = "FullName" .ValueMember = "UserProfile" End With Catch ex As Exception MsgBox(ex.ToString) Exit Sub End Try 'Enable the Delete and Edit Button cmdDelete.Enabled = True cmdEdit.Enabled = True oledbconn.Close() End Sub Quote
rickb Posted May 29, 2003 Author Posted May 29, 2003 You're right, thanks for the catch. However, I corrected the Dataset name in the "Fill" statement, but that didn't solve the problem; the listbox still will not fill. Quote
Administrators PlausiblyDamp Posted May 29, 2003 Administrators Posted May 29, 2003 Is this a windows or web app? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* jfackler Posted June 2, 2003 *Experts* Posted June 2, 2003 I think your problem lies within your sql statement &/or your txtPick.txt. Dim daUserList As New OleDbDataAdapter("SELECT UserProfile, trim( LastName)" & ", " & "trim(FirstName) AS FullName,LastName,FirstName,LOC,Building,DeviceID FROM tblAS400Users WHERE LastName LIKE '" & Me.txtPick.Text & "%' ORDER BY LastName,FirstName", oledbconn) Your where statement looks for LastName like. Is the textbox actually receiving the full name or just the last name? Also, I'd put the portion in paranthesis (trim( LastName)" & ", " & "trim(FirstName)). Jon Quote
*Experts* jfackler Posted June 2, 2003 *Experts* Posted June 2, 2003 Public Sub LoadList() oledbconn.Open() 'since your using a dataadapter this is 'unnecessary the da will open and close the connection as needed Dim dsUserList As New DataSet() 'simplify your select Dim daUserList As New OleDbDataAdapter("SELECT UserProfile,LastName,FirstName,LOC,Building,DeviceID FROM tblAS400Users WHERE LastName LIKE '" & Me.txtPick.Text & "%' ORDER BY LastName,FirstName", oledbconn) 'then, independent of the db, create your calculated column Try daUserList.Fill(dsUserList, "tblAS400Users") 'create a new column Dim dcName As System.Data.DataColumn dcName = New System.Data.DataColumn("Name") dcName.DataType = System.Type.GetType("System.String") dcName.Expression = "FirstName + ' ' + LastName" 'Add the calculated column me.dsUserList.Tables("tblAS400Users").Columns.Add(dcName) 'Bind to the listbox With Me.lstUsers .DataSource = dsUserList.Tables("tblAS400Users") .DisplayMember = "Name" '.ValueMember = "UserProfile" End With Catch ex As Exception MsgBox(ex.ToString) Exit Sub End Try 'Enable the Delete and Edit Button cmdDelete.Enabled = True cmdEdit.Enabled = True oledbconn.Close() 'as above, unnecessary End Sub Just some thoughts. Jon Quote
rickb Posted June 2, 2003 Author Posted June 2, 2003 Works perfectly, jfackler! Thank you very much for the solution. I'm still learning VB.Net, so this was a valuable lesson. This whole procedure worked perfectly using the data adapter wizard, but I was trying to duplicate it using code only. Once again, thanks for your time and effort; it's greatly appreciated. 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.