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
301 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

778 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