Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 22nd, 2005, 10:16 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post
Default HELP - Substring question

I was trying to find a left function in C# that is comparable to the LEFT FUNCTION in vbscript and found substring because I want to ensure that the string in only 100 characters in length before going into the database. Any help or direction would be appreciated. I have my code below as well as the error I am getting. Thank you.

This is the way I am using it when trying to pass in a parameter value via ADO from my web page:

// @FullName (input)
dbCommand.Parameters.Add("@FullName", SqlDbType.VarChar, 100);
dbCommand.Parameters["@FullName"].Direction = ParameterDirection.Input;
dbCommand.Parameters["@FullName"].Value = Server.HtmlEncode((this.fullNameTextBox.Text.Subst ring(1, 100)).Replace("'", ""));

Here is the error I am getting:

Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length
Old April 22nd, 2005, 10:41 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

Not sure why you are getting that error. But maybe you need to test for the length first. If it's >100 use the SubString overload that takes start and length, otherwise, just use the overload that takes the start alone.

Also, unless you have another reason for replacing the ' in your string, you don't need to replace them when you are using a command parameter. The parameter object will take care of escaping the characters that need it.

Old April 22nd, 2005, 10:47 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 119
Thanks: 0
Thanked 1 Time in 1 Post

I was just about to say that I found what my problem is. I was not checking for length > 100. Pretty stupid, sorry for interuption.

Planoie, thanks for your time and reply. It is appreciated.

Similar Threads
Thread Thread Starter Forum Replies Last Post
substring in c# sudhirbharti C# 1 February 21st, 2008 01:29 PM
Substring-before with Regex bonekrusher XSLT 6 November 27th, 2007 01:16 PM
Substring-after collation bonekrusher XSLT 2 May 11th, 2007 09:31 AM
instead of substring what should we use in MSACCES miruthula SQL Language 1 February 9th, 2007 12:13 PM
SubString prasanta2expert Access VBA 1 November 17th, 2006 10:04 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.