Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Progress

609 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