?
Solved

I'm trying to insert a string (varchar) with numbers and dashes into a table column but it is subtracting the numbers, then inserting into database

Posted on 2006-11-20
5
Medium Priority
?
333 Views
Last Modified: 2010-04-16
C#:
                   for (int j = 0; j < 12; j++)
                            {
                                Temp += p[j];
                                if (j == 1 || j == 3 || j == 7 || j == 10)
                                {
                                    Temp += "-";
                                }
                            }

this gives something like 00-45-8760-567-01

but later in the script when It runs

string strSql = "UPDATE " + sTable + " SET " + sColumn + " = " + Temp;

the data in inserted into sColumn is like -9373 (which is actually 00 minus 45 minus 8760 minus 567 minus 1)

How can I go about fixing that?

THanx
0
Comment
Question by:bluud
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 2000 total points
ID: 17977657
whatever you are inserting it into is treating this as a number - you need to surround it with inverted commas " or ' depending on the database.

string strSql = "UPDATE " + sTable + " SET " + sColumn + " = '" + Temp + "'";

should work

0
 
LVL 3

Expert Comment

by:srinims
ID: 17977734
in the case said by Raynard,
+Temp+ won't get translated in to the value.


for solving your problem . you have to add single quotes before and after the data of the string.

i.e Temp = "'"+Temp+"'"

and then you can use your string directly.

string strSql = "UPDATE " + sTable + " SET " + sColumn + " = " + Temp;


i.e your sql look like this

UPDATE <ttable name> SET <colom name> = '00-45-8760-567-01'
(or you could use double quotes also)

like this
UPDATE <ttable name> SET <colom name> = "00-45-8760-567-01"

and not like this
UPDATE <ttable name> SET <colom name> = 00-45-8760-567-01


srini ms


0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17977770

>>in the case said by Raynard,
>>+Temp+ won't get translated in to the value.

why not????

it is a string concat - just as you suggested
0
 

Author Comment

by:bluud
ID: 17977836
thanx srinims  your answer is valid too, but I had already accepted Raynards..
Thanx again
0
 
LVL 3

Expert Comment

by:srinims
ID: 17978210
hi Raynard,
i haven't look in to the string exactly as it has lot of double quotes. And bit confused with "='". yours will work correctly.

sorry...
hi bluud. i'm no the mere source right, no issues.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Loops Section Overview

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question