|
-
Dec 4th, 2005, 11:26 PM
#1
Thread Starter
Hyperactive Member
Code Throwing errors
VB Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItems.Count = 0 Then
Else
Try
Dim strConn_list As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=DATABASE;Integrated Security=True;User Instance=True"
Dim objConn_list As New SqlClient.SqlConnection(strConn_list)
Dim ds As New DataSet
Dim da As SqlClient.SqlDataAdapter, dt As DataTable
Dim dr As DataRow
da = New SqlClient.SqlDataAdapter("SELECT license_id, product_name, company_name, po_number, date, user_licenses FROM licenses WHERE division_name=@Division", objConn_list)
da.SelectCommand.Parameters.Add("@Division", ListBox1.SelectedItem)
da.Fill(ds)
ListView1.Items.Clear()
'Repeat for each table in the DataSet collection.
For Each dt In ds.Tables
'Repeat for each row in the table.
For Each dr In dt.Rows
ListView1.Items.Add(dr("product_name"))
ListView1.Items(0).SubItems.Add(dr("company_name"))
ListView1.Items(0).SubItems.Add(dr("po_number"))
ListView1.Items(0).SubItems.Add(dr("date"))
ListView1.Items(0).SubItems.Add(dr("users_licenses"))
Next
Next
Catch ex_list As Exception
MsgBox(ex_list.Message & ". Conact RattleSoft for support.")
End Try
End If
End Sub
End Class
For some reason it keeps throwing this error:
No mapping exists from object type System.Data.DataRowView to a known managed provider native type.
On form load it gathers details from the database properly, but it also throws an error before the SelectedIndex is changed.
-
Dec 5th, 2005, 12:14 AM
#2
Re: Code Throwing errors
What line is throwing the error??? Step line by line in your prog to see where its occurring.. set a breakpoint in form load to see where the first error is ocurring...
-
Dec 5th, 2005, 12:17 AM
#3
Re: Code Throwing errors
Are you aware that, because you are referring to Items(0) every time, you are adding every subitem to the first item, no matter how many items you add? You need to add the subitems to the newly created item, not the first itme.
As for your other errors, where exactly do they occur? Please give us as much useful information as possible when asking for help. If an exception is thrown it's thrown by a specific line, so why make us guess which line it is?
-
Dec 5th, 2005, 02:14 AM
#4
Thread Starter
Hyperactive Member
Re: Code Throwing errors
Im seeing it occur at da.Fill(ds)
I changed it to da.Fill(ds, "licenses") but its still saying the same error there.
I also changed the Items(0) to:
VB Code:
For Each dr In dt.Rows
ListView1.Items.Add(dr("license_id"), dr("product_name"))
ListView1.Items(dr("license_id")).SubItems.Add(dr("company_name"))
ListView1.Items(dr("license_id")).SubItems.Add(dr("po_number"))
ListView1.Items(dr("license_id")).SubItems.Add(dr("date"))
ListView1.Items(dr("license_id")).SubItems.Add(dr("users_licenses"))
Next
Now each Item has an id matching to the database table and can be tied to subitems
Last edited by tylerm; Dec 5th, 2005 at 02:17 AM.
-
Dec 5th, 2005, 03:02 AM
#5
Re: Code Throwing errors
If you've data-bound your ListBox then each item is a DataRowView, not a String. This means that you're seting the Value property of you parameter to a DataRowView. If you want the actual string displayed in the ListBox that corresponds to the SelectedItem you would need to use this:
VB Code:
da.SelectCommand.Parameters.Add("@Division", ListBox1.GetItemText(ListBox1.SelectedItem))
If you aren't using the ValueMember for any other purpose, you could set the ValueMember to the same column as the DisplayMember and just use the SelectedValue instead:
VB Code:
da.SelectCommand.Parameters.Add("@Division", ListBox1.SelectedValue)
Also, the ListViewItemCollection.Add method returns a ListViewItem object, so you should use that to add your subitems:
VB Code:
For Each dr In dt.Rows
Dim newItem As ListViewItem = ListView1.Items.Add(dr("license_id"), dr("product_name"))
newItem.SubItems.Add(dr("company_name"))
newItem.SubItems.Add(dr("po_number"))
newItem.SubItems.Add(dr("date"))
newItem.SubItems.Add(dr("users_licenses"))
Next
-
Dec 5th, 2005, 03:16 AM
#6
Thread Starter
Hyperactive Member
Re: Code Throwing errors
Well its working now except its throwing a new error:
-
Dec 5th, 2005, 03:51 PM
#7
Thread Starter
Hyperactive Member
-
Dec 5th, 2005, 04:25 PM
#8
Re: Code Throwing errors
Your post #6 says it's throwing a new error but doesn't give any details.
-
Dec 5th, 2005, 04:49 PM
#9
Thread Starter
Hyperactive Member
Re: Code Throwing errors
Its an image, i dont know why it isn't appearing on your screen here is what it says:
Overload resolution failed because no Public 'Add' can be called without a narrowing conversion:
'Public Overrides Function Add(text As String, imageKey As String) As System.Windows.Forms.ListViewItem':
Argument matching parameter'text' narrows from 'Integer' to 'String'.
-
Dec 5th, 2005, 04:57 PM
#10
Re: Code Throwing errors
Hmmm... It shows up in IE but not in Firefox. Anyway, the message is telling you, fairly clearly I would have thought, that you are trying to pass an Integer where the method is expecting a String. With Option Strict turned Off this conversion would be handled implicitly at run time, but Option Strict On forces you to make this type of conversion explicitly to ensure that you are aware that you are changing types and that you take responsibility for making sure it is a valid conversion. All you have to do is add a ".ToString()" to make the conversion explicit. What this also does is speed up execution of your code because there is less type-checking at run time.
-
Dec 5th, 2005, 05:02 PM
#11
Thread Starter
Hyperactive Member
Re: Code Throwing errors
"Value of type 'String' cannot be converted to 'System.Windows.Forms.ListViewItem"
That is what I get when I add the .ToString to Dim newItem As ListViewItem = ListView1....
Im probably having a brain freeze but I can't seem to find a way.
-
Dec 5th, 2005, 05:06 PM
#12
Thread Starter
Hyperactive Member
Re: Code Throwing errors
Nevermind, Im an idiot. I added ToString to: dr("license_id")
-
Dec 5th, 2005, 05:07 PM
#13
Re: Code Throwing errors
You are calling Add with an Integer and a String. There are only two overloads of that method, as pointed out by the error message. The first overload takes a String and a String, while the second takes a String and an Integer. You need to pass the correct type of arguments to the Add method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|