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
281 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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility

>>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
Comment Utility
thanx srinims  your answer is valid too, but I had already accepted Raynards..
Thanx again
0
 
LVL 3

Expert Comment

by:srinims
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now