Question : Invalid length passed to SUBSTRING Function

Hello Experts,
I have  WinForm  that has 3 primary controls; txtTeamName (text box) lbTeamLeader (single select list box) and lbTeamMembers (multi-select list box). The user will enter a Team Name in txtTeamName, select a Team Leadr from lbTeamLeader and add Members in lbTeamMembers.

This code works in the Query Window:
USE IP_MASTER
GO

DECLARE
@TeamName nvarchar(50),
@LeaderCode nvarchar(10),
@Leader nvarchar(50),
@MemberCode nvarchar(50)


Set @TeamName = 'TEAM_PARKER'
Set @LeaderCode = 'PAR01'
Set @Leader = 'PETER PARKER, PT'
Set @MemberCode = 'BUC01 - AL BUCK, PTA'



INSERT INTO tblUserTeams (TeamName, LeaderCode, LeaderDiscipline, MemberCode, MemberDiscipline)
VALUES (@TeamName, @LeaderCode,
SUBSTRING(@Leader,CHARINDEX(',',@Leader)+1,LEN(@Leader)-CHARINDEX(',',@Leader)),
LEFT(@MemberCode,(CHARINDEX('-', @MemberCode) - 1)),
SUBSTRING(@MemberCode, CHARINDEX(',', @MemberCode)+1,LEN(@MemberCode)-CHARINDEX(',', @MemberCode)))

This code returns the error, "Invalid length passed to the SUBSTRING function:


        Dim i As Integer

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

        Dim strTeamName As String = txtTeamName.Text
        Dim strLeader As String = lbTeamLeader.Text
        Dim strLeaderCode As String = lbTeamLeader.SelectedValue

        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,  " & _
        "SUBSTRING(@Leader,CHARINDEX(',',@Leader)+1 ,LEN(@Leader)-CHARINDEX(',',@Leader)),  " & _
        "LEFT(@MemberCode,(CHARINDEX('-', @MemberCode) - 1)),  " & _
        "SUBSTRING(@MemberCode, CHARINDEX(',', @MemberCode)+1 ,LEN(@MemberCode)-CHARINDEX(',', @MemberCode)))"


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

        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

Any help will be greatly appreciated,

JackW9653

Answer : Invalid length passed to SUBSTRING Function

Restore Guide is here http://support.gateway.com/s/Manuals/gateway/GTW_SJV01_User_Guide_Eng.pdf

Pages 11 and 12

The correct keys are ALT-F10 but you will need to read the document first before doing that as there is a bit more to it.
Random Solutions  
 
programming4us programming4us