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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
This is about my first experience with programming Arduino.
Six Sigma Control Plans
Starting up a Project

728 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