hey

im doing a project and am having problem getting this code correct could you please have a look at it and help me understand were i am making the mistake

the code

Private Sub m_buttonAddUserRole_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_buttonAddUserRole.Click
        Dim roleIndex As Integer = ListBox1.SelectedIndex
        If (roleIndex = -1) Then
            Return
        End If
        ' Get the current user index.
        Dim userIndex As Integer = ListBox2.SelectedIndex
        ' Should we ignore the the event?
        If (userIndex = -1) Then
            Return
        End If
        ' Get the identifiers.
        Dim userID As Integer = CType(ListBox1.Rows(userIndex)("user_id"), Int32)
        Dim roleID As Integer = CType(m_roleTable.Rows(roleIndex)("role_id"), Int32)
end sub

the errors says


'Rows' is not a member of 'System.Windows.Forms.ListBox'.

why is this

Are you just trying to get the value from the .SelectedItem in the ListBox?
.reason asking: you already have the .SelectedIndex.

yes and ni have managed to fix that problem by using the code

Dim userID As Integer = CType(ListBox1.SelectedItem, Int32)
        Dim rightID As Integer = CType(ListBox2.SelectedItem, Int32)

but now i have this problem could you tell me were i am going wrong

' Search for an existing row.
        Dim rows As DataRow() = m_userRightTable.[Select]("right_id = " & rightID & " AND user_id = " & userID)

is your listbox databound? do you use DisplayMember and Valuemember properties?
If you DO NOT, you can NOT get SelectedValue property.

Dim userID As Integer = CType(ListBox1.SelectedItem, Int32) will error since you cannot convert a String to an Integer.
.To get the value, use something as: Dim userID As String = ListBox1.SelectedItem .

i have managed to solve this issue but i cant get this errors to go away :(

' Search for an existing row.
        Dim rows As DataRow = "Select right_id = " & rightID & " AND user_id = " & userID & 

        ' Is there already an association?
        If (rows.Length > 0) Then
            Return
        End If

Is the error on line.3?Then .Remove the last "&" since it does not add anything else to the value of your rows .

the error i am getting is

Error 1 Value of type 'String' cannot be converted to '1-dimensional array of System.Data.DataRow'.

i dont no wat that means i neva come across it befroe

rows() is an Array, as: dim rows() as string={"row 1","row 2","row etc."} and if you want a value from it, use msgbox(rows(2)) .
Since you are trying to only set 1 String to a String Array, you will need to set it as I did, not as setting it to a String only as = "blah" & "blah".

Also, not a db.coder here, though it looks like your error line is a connection.string for a database, not to get the rows from a DataTable.

Edited 4 Years Ago by codeorder: n/a

Sorry, this way:

Dim rows As DataRow() = table.[Select]("right_id = '" & rightID & "' AND user_id = '" & userID & "'")
Dim rows As DataRow() = "cg_security_role_right.[Select]right_id = '" & rightID & "' AND user_id = '" & userID & "'")

        ' Is there already an association
        If (rows.Length > 0) Then
            Return
        End If

the same errors here still i dont get why

This article has been dead for over six months. Start a new discussion instead.