Results 1 to 13 of 13

Thread: Code Throwing errors

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Code Throwing errors

    VB Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.         If ListBox1.SelectedItems.Count = 0 Then
    3.         Else
    4.             Try
    5.                 Dim strConn_list As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=DATABASE;Integrated Security=True;User Instance=True"
    6.  
    7.                 Dim objConn_list As New SqlClient.SqlConnection(strConn_list)
    8.                 Dim ds As New DataSet
    9.                 Dim da As SqlClient.SqlDataAdapter, dt As DataTable
    10.                 Dim dr As DataRow
    11.                 da = New SqlClient.SqlDataAdapter("SELECT license_id, product_name, company_name, po_number, date, user_licenses FROM licenses WHERE division_name=@Division", objConn_list)
    12.                 da.SelectCommand.Parameters.Add("@Division", ListBox1.SelectedItem)
    13.  
    14.                 da.Fill(ds)
    15.                 ListView1.Items.Clear()
    16.  
    17.                 'Repeat for each table in the DataSet collection.
    18.                 For Each dt In ds.Tables
    19.                     'Repeat for each row in the table.
    20.                     For Each dr In dt.Rows
    21.                         ListView1.Items.Add(dr("product_name"))
    22.                         ListView1.Items(0).SubItems.Add(dr("company_name"))
    23.                         ListView1.Items(0).SubItems.Add(dr("po_number"))
    24.                         ListView1.Items(0).SubItems.Add(dr("date"))
    25.                         ListView1.Items(0).SubItems.Add(dr("users_licenses"))
    26.                     Next
    27.                 Next
    28.             Catch ex_list As Exception
    29.                 MsgBox(ex_list.Message & ". Conact RattleSoft for support.")
    30.             End Try
    31.         End If
    32.        
    33.        
    34.     End Sub
    35. 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.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    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:
    1. For Each dr In dt.Rows
    2.                     ListView1.Items.Add(dr("license_id"), dr("product_name"))
    3.                     ListView1.Items(dr("license_id")).SubItems.Add(dr("company_name"))
    4.                     ListView1.Items(dr("license_id")).SubItems.Add(dr("po_number"))
    5.                     ListView1.Items(dr("license_id")).SubItems.Add(dr("date"))
    6.                     ListView1.Items(dr("license_id")).SubItems.Add(dr("users_licenses"))
    7.                 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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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:
    1. 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:
    1. For Each dr In dt.Rows
    2.                     Dim newItem As ListViewItem = ListView1.Items.Add(dr("license_id"), dr("product_name"))
    3.                     newItem.SubItems.Add(dr("company_name"))
    4.                     newItem.SubItems.Add(dr("po_number"))
    5.                     newItem.SubItems.Add(dr("date"))
    6.                     newItem.SubItems.Add(dr("users_licenses"))
    7.                 Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: Code Throwing errors

    Well its working now except its throwing a new error:


  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: Code Throwing errors

    Time for a bump

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code Throwing errors

    Your post #6 says it's throwing a new error but doesn't give any details.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    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'.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    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.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Re: Code Throwing errors

    Nevermind, Im an idiot. I added ToString to: dr("license_id")

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width