i want to change first letter of both First name and Last Name to uppercase here is my code however, i am getting error..as

Index and length must refer to a location within the string.
Parameter name: length

MY CODE IS AS BELOW :

Function uppercasefirstletter(ByVal oldString As String) As String
Dim firstletter As Char
Dim newString As String
firstletter = Char.ToUpper(oldString.Substring(0, 1))
Mid(oldString, 1, 1) = firstletter
newString = oldString
Return newString

End Function

PLZ LET ME KNOW THE ERROR AS QUICK AS POSSIBLE...AM WORKING WITH VB.NET 2005 THIS CODE HOWEVER WORKED IN VB.NET 2003 PLZ HELP ME TO SOLVE THE ERROR
THANKS
CYA
ROHAN

Recommended Answers

All 2 Replies

You are calling function with an empty string which causes that error in oldString.Substring(0, 1) You should check for an empty string first:

Function uppercasefirstletter(ByVal oldString As String) As String
'
If String.IsNullOrEmpty(oldString) Then
  Return oldString
Else
  Return oldString.Substring(0, 1).ToUpper & oldString.Substring(1, oldString.Length - 1)
End If

End Function

thanks hp it works..ll mark it solved if it works......thanks in advance once again.. :)

cya
Rohan

You are calling function with an empty string which causes that error in oldString.Substring(0, 1) You should check for an empty string first:

Function uppercasefirstletter(ByVal oldString As String) As String
'
If String.IsNullOrEmpty(oldString) Then
  Return oldString
Else
  Return oldString.Substring(0, 1).ToUpper & oldString.Substring(1, oldString.Length - 1)
End If

End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.