absent Posted December 7, 2003 Posted December 7, 2003 I'm having a problem with the Listbox's databinding... I'm using Private Sub frmSetupDay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstSelect.DataSource = SqlDataSet1 lstSelect.DisplayMember = "tblEmployee.EmployeeName" lstSelect.ValueMember = "tblEmployee.ID_Number" SqlDataAdapter1.Fill(SqlDataSet1) End Sub to bind the first list box. When I click the button Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeSelect.Click Try lstSelected.Items.Remove(lstSelected.SelectedItem) Catch ex As Exception MsgBox(Err.Number, MsgBoxStyle.Exclamation + MsgBoxStyle.OKOnly, Err.Description) Exit Sub End Try End Sub I get "System.Data.DataRowView" in the second listbox. Any ideas whats wrong? the coding looks right and the tables and fields are present in the database. Thanx in advance:confused: Quote P.L.U.R - Peace, Love, Unity, Respect :P
Mothra Posted December 8, 2003 Posted December 8, 2003 You might want to try filling the dataset before you set it as the datasource for your listbox. I'm not sure but is may be the cause of your problem. Quote Being smarter than you look is always better than looking smarter than you are.
absent Posted December 8, 2003 Author Posted December 8, 2003 Right... I know this is going to sound REALLY dumb - I'm still trying to learn VB.net let alone SQL - How do you fill a dataset? I thought that when you create a connection to the SQL server database it automatically fills when you run the application? Quote P.L.U.R - Peace, Love, Unity, Respect :P
Mothra Posted December 8, 2003 Posted December 8, 2003 The dataadapter has a Fill method, you're calling it but AFTER your assigning the datasource. It may not be the problem, thats just what I would look at first... Private Sub frmSetupDay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'Fill your dataset here... SqlDataAdapter1.Fill(SqlDataSet1) 'And then assign the datasource, etc... lstSelect.DataSource = SqlDataSet1 lstSelect.DisplayMember = "tblEmployee.EmployeeName" lstSelect.ValueMember = "tblEmployee.ID_Number" End Sub And don't worry about sounding dumb. I asked a thousand dumb questions when I learned VB and again for .NET... Quote Being smarter than you look is always better than looking smarter than you are.
absent Posted December 9, 2003 Author Posted December 9, 2003 ahhhhhhhhhh Right I see where my logic went off at a tangent... Slightly off the topic : Do I have to create a connection to the database for each form? or can I use the connection on the parent form? THe application is a SDI using tabbed forms and one or 2 that are SDI on their own for making setting changes etc to the application - Some of these have connections to the database of their own. So I was wondering if it is possible to do something like sqlDataAdapter1.source = form1.sqlDataAdapter Back in VB6 days you could cross link forms by calling the control from a separate SDI form. Or am I praying to the impossible knowing M$:confused: Quote P.L.U.R - Peace, Love, Unity, Respect :P
Mothra Posted December 9, 2003 Posted December 9, 2003 Sure you can but, it's not THAT simple. You just have a few more lines of code to write... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As EventArgs) _ Handles Button1.Click Dim f As New Form2 'When you create an instance of the second form 'Make the form with the db connection (in this case Form1) 'the "Owner" of the new form (Form2) f.Owner = Me f.Show() End Sub Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As EventArgs) _ Handles MyBase.Load 'Then you use the DirectCast to hook up to the "Owner" form 'and get access to it's components. The process is almost the 'same for MDI apps, just instead of using "Owner" you use 'MdiParent. Dim owner As Form1 = DirectCast(Me.Owner, Form1) Dim cn As SqlClient.SqlConnection = owner.SqlConnection1 Dim cmd As New SqlClient.SqlCommand("SELECT * FROM YourTtable", cn) Dim da As New SqlClient.SqlDataAdapter(cmd) Dim ds As New DataSet Try da.Fill(ds) Catch ex As Exception MessageBox.Show(ex.Message & " " & Err.Number) End Try End Sub That should get you going. Quote Being smarter than you look is always better than looking smarter than you are.
absent Posted December 9, 2003 Author Posted December 9, 2003 *scream* ARG!!!! Sorry to come back to this after the SQL - But the System.Data.DataRowView is still giving ... I even tried redesigning the sqldataadapter. I know they are working else I would not be able to see the entries in the list box like I do. I copied the code exactly as you put it and still has the same effect... I even tried giving the sqlDataAdapter the admin user n pass and still same thing. Can't seem to find anything relevant on the net to the problem. - IS THERE NO END! Quote P.L.U.R - Peace, Love, Unity, Respect :P
Mothra Posted December 10, 2003 Posted December 10, 2003 (edited) The end?? I sjust went back to your origional post and I have an idea...instead of this: Private Sub frmSetupDay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'Fill your dataset here... SqlDataAdapter1.Fill(SqlDataSet1) 'And then assign the datasource, etc... lstSelect.DataSource = SqlDataSet1 lstSelect.DisplayMember = "tblEmployee.EmployeeName" lstSelect.ValueMember = "tblEmployee.ID_Number" End Sub Try this: Private Sub frmSetupDay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load 'Fill your dataset here... SqlDataAdapter1.Fill(SqlDataSet1) 'And then assign the datasource, etc... lstSelect.DataSource = SqlDataSet1.Tables(x) 'where x is the index of the table in the dataset '(if you only have one table the index is '0'). 'lstSelect.DisplayMember = SqlDataSet1.Tables(x) 'lstSelect.ValueMember = SqlDataSet1.Tables(x) End Sub To display a speific column in the table, you may have to loop through it to get them added, like this... Dim i As Integer For i = 0 To SqlDataSet.Tables(x).Rows.Count ListBox.Items.Add(SqlDataSet.Tables(x).Row(i).Item(n).ToString Next Here, n is the index of the column you want... Sorry we had to go through all this to get you back to your origional question, I just didn't see it before... You might need to play with these a little to get them working just right, I don't usually use the DisplayMember and ValueMember so I'm sure about them working like this. Edited December 10, 2003 by Mothra Quote Being smarter than you look is always better than looking smarter than you are.
absent Posted December 10, 2003 Author Posted December 10, 2003 VB6 vs VB.net *round one!* ARG!!! things were soooooooooooo much easier back in VB6 days. Yeah the coding might be neater and a lot tighter. But there are some of us who have to relearn VB because of M$'s loverly java look like .net technology :confused: Are there any good tutorials or such for SQL in VB.net and ADO.net? OTHER than the MSDN website please... Yeah manuals do come in handy - But they explain the whole .net data typing in less than 10 pages, and it is written as though we all understand .net just because we are vb6 programmers. *scream!!!!* Quote P.L.U.R - Peace, Love, Unity, Respect :P
absent Posted December 10, 2003 Author Posted December 10, 2003 Erg! SQL table index? Sorry to bother again Mothra, but how do I find the index of my tables? There is no way of setting them or retrieving them in SQL Server 2000 enterprise manager. Quote P.L.U.R - Peace, Love, Unity, Respect :P
Mothra Posted December 11, 2003 Posted December 11, 2003 You have to have a different DataAdapter for each table. If you're only using one dataadapter, you're only going tohave one table. If you ARE using more than one table, the index of each one is in the order in which you use the .Fill... DataAdapter1.Fill(ds) 'Table 1, Index 0 DataAdapter2.Fill(ds) 'Table 2, Index 1 'and so on... You pretty much DO have to learn VB all over again but, in the end, you'll be using a much more powerful and dynamic language. It's way worth it! At least I think so anyhow. After you've been working on things for a while, they'll start to get more fimilar to you. One things that's helped me is trying different kinds of resources. MSDN has a thing called VB TV that, while it covers pretty basic stuff, can often be pretty insightful, especially for folks making the transition from 6.0 to .NET. As for tutorials...there's tons out there (some good and some not as good.) It just takes some digging, here's a few web sites that may help... Got .NET SQL Junkies .NET Junkies Windows Forms There are tons fo others but these should get you started... Feel free to email me if you want (Ken). I learned how to program using web sites like this and, now that I (sort of) know what I'm doing I really like to help other people out as much as I can. Ready for round two yet? ;) Quote Being smarter than you look is always better than looking smarter than you are.
absent Posted December 12, 2003 Author Posted December 12, 2003 *Round two* *ding* " ... And it's VB6 in one corner and VB.net in his opposite ... " " ... This is the titanic strugle of the decade, who will come out tops? ... " Right so I have to admit VB.net is FAR SUPERIOR than VB6, and it might take relearning the language ALL OVER again. But I am greatful I am doing it. Thanx a mill for all your help Mothra Really appreciative. I will mail you soon - Just gotta find some free time during work and the week to do it. Or alternatively you could mail me (Brendon). I might be a beginner or so in VB.net, but 2 heads are always better than one. Maybe there is something you are stuck with I might be able to help with. Thanx again and happy programming " ... O that was a AWSOME BATTLE! Two great titans battling it out and as we said before only one would come out tops... Ladies and gentlemen VB.net is the winner!!!!!!! ... " Quote P.L.U.R - Peace, Love, Unity, Respect :P
Mothra Posted December 12, 2003 Posted December 12, 2003 Any time my man, any time. Quote Being smarter than you look is always better than looking smarter than you are.
absent Posted December 25, 2003 Author Posted December 25, 2003 Problem sloved It was as easy as putting lstSelected.items.add(lstSelected) Something so simple in the end... Quote P.L.U.R - Peace, Love, Unity, Respect :P
absent Posted January 1, 2004 Author Posted January 1, 2004 Right wrong coding there I know I am sorry... To add items to the second list box from the first, do: listbox2.items.add(listbox1.text) Quote P.L.U.R - Peace, Love, Unity, Respect :P
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.