473,378 Members | 1,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

decimal numbers c# and sql server

Hi!

I just want to update a column in a table having a value of a decimal
number.
Then I tried this via sql-query analyzer

update tablepost set preis = 0.71 where idnumber = 50
It worked well.

Then in my c# program, I tried the same: but first of all

command = "update tablepost set preis = 0.71 where idnumber = 50";
//I passed it as a paramter for a given method:
This also functioned well and stored the right value.

Then I tried these:
decimal preis = 0.71;

command = "update tablepost set preis = " + this.preis + " where
idnumber = 50";

It did not compile, then I added:
command = "update tablepost set preis = convert(decimal(28,2)," +
this.preis + ")where idnumber = 50";

It compiled, but instead of 0.71, it stored 0, when I give 32.8 it
stores 32.

In customized regional numbers I have:
Decimal symbol: ','
No. of digits after decimal: '.'

And for the tablepost - preis, I do have
decimal (as datatype), 28 and 2 for accuracy and decimal places
respectively.

Can anyone give me some tips, how to go about this...
Lest I forget, I can select from tables returning back decimal numbers
without any problem

Thanks in advance.
Stropher

Nov 17 '05 #1
6 7262
try this:

decimal preis = 0.71m;

It might be that it is somewhere trying, or actually converting it to a
double. Appending your literal with 'm', as in the above example
prevents that from happening.

Nov 17 '05 #2
Thanks Gmiley for your quick answer,
I have tried it, but it could not go through complaining of not having
'm' defined

Nov 17 '05 #3
Could you provide the code for the block where this all takes place?

starting with the variable definition and setting, and the sql string
set, to the execution of the sql string?

Nov 17 '05 #4
Oh thanks, here:

private DataTable tblResult;
private void btnImport_Click(object sender, EventArgs e)
{
int articleID = 0;
decimal preis = 0;
decimal dek = 0;
string command = "";

//created and returned articleID
//...

//Extract the Article numbers from the excel file

//Use the article numbers to select their preises from the
respective tables
command = "SELECT art_id as ArticleID, eg_preis as Preis
from article inner join boughtarticles on
art_id = bgt_id WHERE art_aktive = 1";

tblResult = this.dal.SelectStatementsData(command); //call
the function and get datatable in return

if (tblResult != null)
{
foreach (DataRow dr in tblResult.Rows)
{
if (dr[0] != null)
articleID = Convert.ToInt32(dr[0]);
if (dr[1] != null)
preis = Decimal.Parse(dr[1].ToString()); //here
I get sth. like 0.71, 32.80, etc

//create and insert into a new row of tablepost
this.tablepostID =
this.dal.CreateStatements(insertCommandText);
if (tablepostID > 0)
{
updateCommandText = "update tablepost set
articleID = " + articleID + ", Preis = convert
(decimal(28,2)," + preis + ") where tablepostID
= " + this.tablepostID.ToString();

//Update the Table and indicate whether
successful or not
updateSuccessful =
this.dal.UpdateStatements(updateCommandText);

if (!updateSuccessful)
MessageBox.Show("UpdateTablepost NOT
successful!\n " + dal.ExceptionMsg);
}
}//end of foreach (...)

dataGVDisplay.DataSource = tblResult; //display it on
the grid table
//...
}//end of if (tableResult ...)
else
MessageBox.Show(dal.ExceptionMsg);
}
//have to convert to decimal, b/c what I get with select is sth. like
0,71, and what I send to the db (its what works or what the db accepts)
in return is 0.71 and when I take look at the table it stores 0,71
//the problem of german and english languages...: If I enter 0.71 (no
problem), but as a parameter preis having 0.71 no way.

---------------------------------------//another class DAL for the
connections to the db
private string exeptionMsg;
public string ExceptionMsg
{
get
{
return this.exceptionMsg;
}

set
{
this.exceptionMsg = value;
}
}

public bool UpdateStatements(string updateCommandText)
{

SqlCommand command = new SqlCommand(updateCommandText);
command.Connection = connection;
bool rowsAffected = false;
try
{
connection.Open();

int affectedRows = command.ExecuteNonQuery();

if (affectedRows > 0)
rowsAffected = true;
}
catch (SqlException ex)
{
exceptionMsg = "PROBLEM by UpdateStatements (Update
failed) " + ex.Message;
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
return rowsAffected;
}

Nov 17 '05 #5
has no one had such a problem before?..
still searching for help....

Thanks

Nov 17 '05 #6
This was the solution to my problem....
Thanks to all for your contributions

double price = 0;

SqlParameter parPrice = new SqlParameter("@price", SqlDbType.Float);
parPrice.Value = price;

commandText = "update Article set price = @price where ArticleID = " +
articleID.ToString();
this.command.Parameters.Insert(0, parPreis);
this.command.Connection = this.connection;
this.connection.Open();

int affectedRows = this.command.ExecuteNonQuery();

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
3
by: gaius | last post by:
I am using the Data Access Application Block for a data interface tier in my application. All data goes to and from the database (MSDE) without a problem, except for decimal numbers which get...
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
4
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! I'm using greece - greek in my control panel's regional options, and so, my decimal point is the comma (,), while it is the dot (.) for the sql server db, however, I'm facing...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
by: DustWolf | last post by:
Hello, I am wondering, what is the standard for including decimal numbers in XML code? What determines what is the decimal delimiter and what can be the grouping symbol? I have just realized...
0
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.