Question : System.Data.DataRowView getting Inserted into SQL Server

Hello Experts,

I'm having an issue with getting the Value from a Multi-Select Listbox into a SQL Server 2005 table. When I run the app everything works correctly except for System.Data.DataRowView getting inserted into the MemberCode column in the data table.
 
The Listbox is loaded from the following:

       Try
            Dim dt As New DataTable
            Dim strSelect As String = "Select UserCode, (UserCode+'-'+FName+' '+LName+ ', ' +Discipline)As Name FROM tblUsersData WHERE Prov_Type = N'U' AND Discipline NOT IN('OT','PT','ST','MD')ORDER BY LName"
           
      Dim sqlDA As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(strSelect, cn)
        sqlDA.Fill(dt)
        lbTeamMember.DisplayMember = "Name"
        lbTeamMember.ValueMember = "UserCode"
        lbTeamMember.DataSource = dt
       
      Catch err As SqlClient.SqlException
            MessageBox.Show("An error occurred in GetAllUsers - " & err.Message)
        End Try

So yes it is bound to the DataTable and that is the root of the System.Data.DataRowView getting inserted into the database.The following code is used to do the inserting:

If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If

        Dim i As Integer

        Dim strTeamName As String = txtTeamName.Text
        Dim strLeader As String = lbTeamLeader.Text
        Dim strLeaderCode As String = lbTeamLeader.SelectedValue
        Dim strMember As String = lbTeamMember.SelectedValue.ToString
        Dim myAdapter As New SqlClient.SqlDataAdapter

        myAdapter.InsertCommand = New SqlClient.SqlCommand
        myAdapter.InsertCommand.Connection = cn
        myAdapter.InsertCommand.CommandType = CommandType.Text
        myAdapter.InsertCommand.CommandText = " INSERT INTO tblUserTeams (TeamName, LeaderCode, LeaderDiscipline, MemberCode, MemberDiscipline)" & _
        "VALUES (@TeamName, @LeaderCode, " & _
        "case when @Leader like '%,%' then " & _
        "SUBSTRING(@Leader,CHARINDEX(',',@Leader)+1,LEN(@Leader)-CHARINDEX(',',@Leader)) else '' end, " & _
        "case when @membercode like '%-%' then LEFT(@MemberCode,(CHARINDEX('-', @MemberCode) - 1)) else @MemberCode end, " & _
        "case when @MemberCode like '%,%' then " & _
        "SUBSTRING(@MemberCode, CHARINDEX(',', @MemberCode)+1,LEN(@MemberCode)-CHARINDEX(',', @MemberCode)) else '' end) "

        myAdapter.InsertCommand.Parameters.AddWithValue("@TeamName", strTeamName)
        myAdapter.InsertCommand.Parameters.AddWithValue("@LeaderCode", strLeaderCode)
        myAdapter.InsertCommand.Parameters.AddWithValue("@Leader", strLeader)
        myAdapter.InsertCommand.Parameters.AddWithValue("@MemberCode", strMember)

        Try
            For i = 0 To lbTeamMember.SelectedItems.Count - 1
                myAdapter.InsertCommand.Parameters("@MemberCode").Value = lbTeamMember.SelectedItems(i).ToString()  
                myAdapter.InsertCommand.ExecuteNonQuery()
            Next
        Catch err As SqlException
            MsgBox(err.Message)
        Finally
            cn.Close()
        End Try


I've traced the variables through to the Try/Catch clause and all is good up to there. After that it  inserts all the correct data and the correct number of records but System.Data.DataRowView in the MemberCode column. I've tried several different methods but I can't convert the DataRowView to a String.

Any help would be appreciated,

JackW

Answer : System.Data.DataRowView getting Inserted into SQL Server

               myAdapter.InsertCommand.Parameters("@MemberCode").Value = lbTeamMember.SelectedItems(i).Item("ID")


If ID is the field name bound to the listbox
Random Solutions  
 
programming4us programming4us