School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,436 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,472 people online right now. Registration is fast and FREE... Join Now!




Out of range exception when using spaces?

 

Out of range exception when using spaces?

cdietschrun

26 Jun, 2009 - 05:52 AM
Post #1

New D.I.C Head
*

Joined: 23 Jun, 2009
Posts: 24

I'm new to SQL and am getting an exception on my webpage when I have an argument to my SQL DB that has spaces in it.

Basically, the page is being passed a name like "Name with spaces", which is being sent as templatename: (in VB)

CODE
    
ra = New SqlDataAdapter("exec sp_GetTemplateByName '" & templatename & "'", TestStatConn)
ra.Fill(dt)


The exception gets thrown at this line only for names that have spaces, not if a templatename is passed like "name_without_spaces". The exception is

CODE
System.IndexOutOfRangeException: There is no row at position 0.


CODE

ret &= "<td><span style=""color:#a9a9a9"">" & CheckForNullVal(dt.Rows(0).Item("lastchange")) & "</span></td>"


The sp_GetTemplateByName:
CODE

Select
..stuff..
from tbl_checklist_templates
where templatename=@TEMPLATENAME
order by stepnumber


Can anyone shed some light on what's going on here?

This post has been edited by cdietschrun: 26 Jun, 2009 - 05:54 AM

User is offlineProfile CardPM
+Quote Post


kmangold

RE: Out Of Range Exception When Using Spaces?

26 Jun, 2009 - 05:55 AM
Post #2

D.I.C Head
Group Icon

Joined: 24 Jun, 2009
Posts: 166



Thanked: 7 times
Dream Kudos: 75
My Contributions
If there are no spaces in the database, then adding spaces wont return anything useful.
User is offlineProfile CardPM
+Quote Post

cdietschrun

RE: Out Of Range Exception When Using Spaces?

26 Jun, 2009 - 06:14 AM
Post #3

New D.I.C Head
*

Joined: 23 Jun, 2009
Posts: 24

I have it writing to the database when someone edits the template name field on a form. The name is defaulted to "TEMPLATE00001" or something similar. If I edit it to "Name with spaces", it saves onchange and then becomes invalid. But if I open a regular name, "TEMPLATE00038", there's no problem. The problem occurs at the lines in the first code box
User is offlineProfile CardPM
+Quote Post

kmangold

RE: Out Of Range Exception When Using Spaces?

26 Jun, 2009 - 06:19 AM
Post #4

D.I.C Head
Group Icon

Joined: 24 Jun, 2009
Posts: 166



Thanked: 7 times
Dream Kudos: 75
My Contributions
CODE
Select
..stuff..
from tbl_checklist_templates
where templatename="@TEMPLATENAME"
order by stepnumber


Try something like that?
User is offlineProfile CardPM
+Quote Post

cdietschrun

RE: Out Of Range Exception When Using Spaces?

26 Jun, 2009 - 06:23 AM
Post #5

New D.I.C Head
*

Joined: 23 Jun, 2009
Posts: 24

Nothing, I tried both double quotes and single quotes around templatename and @TEMPLATENAME, same exception on the same line.
User is offlineProfile CardPM
+Quote Post

cdietschrun

RE: Out Of Range Exception When Using Spaces?

26 Jun, 2009 - 06:46 AM
Post #6

New D.I.C Head
*

Joined: 23 Jun, 2009
Posts: 24

If I remove the single quotes from this line

CODE
"exec sp_GetTemplateByName '" &amp; templatename &amp; "'"


and make it

CODE
"exec sp_GetTemplateByName " &amp; templatename &amp; ""


everything acts the same, except when I click on a template name with spaces, I get this exception:

System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near '%'.

From line 55:

CODE
ra = New SqlDataAdapter("exec sp_GetTemplateByName " & templatename & "", TestStatConn)
           (THIS is lne 55) ra.Fill(dt)


User is offlineProfile CardPM
+Quote Post

June7

RE: Out Of Range Exception When Using Spaces?

27 Jun, 2009 - 11:06 AM
Post #7

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 476



Thanked: 37 times
My Contributions
Try brackets ( [ and ] ) as delimiters on each end of templatename. If that doesn't work, build a function that will replace any spaces with underscore.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 12:57AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month