Converting text data to money

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using a data window with textbox's. some of the data is money keyed in
as "60.00". I need to convert this to money before I can save it to a sql
data table.
the code is done in C#. can someone help me!!! The specific code is included.

SqlCommand cmdGetIdentity = new SqlCommand();

System.Text.StringBuilder b;

try
{
b = new System.Text.StringBuilder(" ");
b.Append("INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values('");
b.Append(txtServiceCode.Text);
b.Append("','");
b.Append(txtDescription.Text);
b.Append("','");
b.Append(cbLargeAnimalCost.Text);
b.Append("','");
b.Append(cbMediumAnimalCost.Text);
b.Append("','");
b.Append(cbSmallAnimalCost.Text);
b.Append("')" );

sqlInsertCommand1.CommandText=b.ToString();
this.sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNumber.Text +
" Successful.","");

}
 
nbohana,

Have a look at the overloaded ToString method from decimal and other
values.

(You first will have to convert the text to decimal of course)

I hope this helps,

Cor
 
Hi,

You should really use parametrised commands (see SqlCommand.Parameters,
SqlParameter).
As for converting text to money use Convert.ToDecimal (and use
SqlDbType.Money as parameter type).
 
Thanks for your information, could give me and example, I have tried and
cannot code it correctly. I am a little new at this and could use some help.
Thanks in advance.

Norm


Miha Markic said:
Hi,

You should really use parametrised commands (see SqlCommand.Parameters,
SqlParameter).
As for converting text to money use Convert.ToDecimal (and use
SqlDbType.Money as parameter type).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
I am using a data window with textbox's. some of the data is money keyed
in
as "60.00". I need to convert this to money before I can save it to a sql
data table.
the code is done in C#. can someone help me!!! The specific code is
included.

SqlCommand cmdGetIdentity = new SqlCommand();

System.Text.StringBuilder b;

try
{
b = new System.Text.StringBuilder(" ");
b.Append("INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values('");
b.Append(txtServiceCode.Text);
b.Append("','");
b.Append(txtDescription.Text);
b.Append("','");
b.Append(cbLargeAnimalCost.Text);
b.Append("','");
b.Append(cbMediumAnimalCost.Text);
b.Append("','");
b.Append(cbSmallAnimalCost.Text);
b.Append("')" );

sqlInsertCommand1.CommandText=b.ToString();
this.sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNumber.Text +
" Successful.","");

}
 
Something like:

sqlInsertCommand1.CommandText = "INSERT INTO
services([service-code],...,[small-animal-cost]) VALUES (@ServiceCode, ...,
@SmallAnimalCost)";
sqlInsertCommand1.Parameters.Add("(@ServiceCode", SqlDbType.Int).Value =
servicecode;
....
sqlInsertCommand1.Parameters.Add("(@SmallAnimalCost", SqlDbType.Money).Value
= Convert.ToDecimal(smallanimalcost);

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

....
nbohana said:
Thanks for your information, could give me and example, I have tried and
cannot code it correctly. I am a little new at this and could use some
help.
Thanks in advance.

Norm


Miha Markic said:
Hi,

You should really use parametrised commands (see SqlCommand.Parameters,
SqlParameter).
As for converting text to money use Convert.ToDecimal (and use
SqlDbType.Money as parameter type).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
I am using a data window with textbox's. some of the data is money
keyed
in
as "60.00". I need to convert this to money before I can save it to a
sql
data table.
the code is done in C#. can someone help me!!! The specific code is
included.

SqlCommand cmdGetIdentity = new SqlCommand();

System.Text.StringBuilder b;

try
{
b = new System.Text.StringBuilder(" ");
b.Append("INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values('");
b.Append(txtServiceCode.Text);
b.Append("','");
b.Append(txtDescription.Text);
b.Append("','");
b.Append(cbLargeAnimalCost.Text);
b.Append("','");
b.Append(cbMediumAnimalCost.Text);
b.Append("','");
b.Append(cbSmallAnimalCost.Text);
b.Append("')" );

sqlInsertCommand1.CommandText=b.ToString();
this.sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNumber.Text
+
" Successful.","");

}
 
Miha, I changed my code, and here is a copy. I get a error at Exec. Time
"System.InvalidCastException: Specified cast is not valid.
at System.Convert.ToDecimal(Object Value)??

//
// sqlInsertCommand1
//
try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost])" +
" values txtServiceCode,txtServiceDesc,cbLargeAnimalCost
,cbMediumAnimalCost,cbSmallAnimalCost)";
sqlInsertCommand1.Parameters.Add("(@[service-code]",SqlDbType.Int).Value =
txtServiceCode
sqlInsertCommand1.Parameters.Add("(@[service-escription]",SqlDbType.VarChar).Value = txtServiceDesc
sqlInsertCommand1.Parameters.Add("(@[large-animal-cost]",SqlDbType.Money).Value = Convert.ToDecimal(cbLargeAnimalCost)
sqlInsertCommand1.Parameters.Add("(@[medium-animal-ost]",SqlDbType.Money).Value = Convert.ToDecimal(cbMediumAnimalCost)
sqlInsertCommand1.Parameters.Add("(@[small-animal-cost]",SqlDbType.Money).Value = Convert.ToDecimal(cbSmallAnimalCost);

sqlInsertCommand1.Connection = sqlConnection1;
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");

}


Miha Markic said:
Something like:

sqlInsertCommand1.CommandText = "INSERT INTO
services([service-code],...,[small-animal-cost]) VALUES (@ServiceCode, ...,
@SmallAnimalCost)";
sqlInsertCommand1.Parameters.Add("(@ServiceCode", SqlDbType.Int).Value =
servicecode;
....
sqlInsertCommand1.Parameters.Add("(@SmallAnimalCost", SqlDbType.Money).Value
= Convert.ToDecimal(smallanimalcost);

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

....
nbohana said:
Thanks for your information, could give me and example, I have tried and
cannot code it correctly. I am a little new at this and could use some
help.
Thanks in advance.

Norm


Miha Markic said:
Hi,

You should really use parametrised commands (see SqlCommand.Parameters,
SqlParameter).
As for converting text to money use Convert.ToDecimal (and use
SqlDbType.Money as parameter type).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

I am using a data window with textbox's. some of the data is money
keyed
in
as "60.00". I need to convert this to money before I can save it to a
sql
data table.
the code is done in C#. can someone help me!!! The specific code is
included.

SqlCommand cmdGetIdentity = new SqlCommand();

System.Text.StringBuilder b;

try
{
b = new System.Text.StringBuilder(" ");
b.Append("INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values('");
b.Append(txtServiceCode.Text);
b.Append("','");
b.Append(txtDescription.Text);
b.Append("','");
b.Append(cbLargeAnimalCost.Text);
b.Append("','");
b.Append(cbMediumAnimalCost.Text);
b.Append("','");
b.Append(cbSmallAnimalCost.Text);
b.Append("')" );

sqlInsertCommand1.CommandText=b.ToString();
this.sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNumber.Text
+
" Successful.","");

}
 
You should convert value, not combobox:
For example,
cbLargeAnimalCost.Text and not cbLargeAnimalCost.

And make sure, that text is in correct format.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
Miha, I changed my code, and here is a copy. I get a error at Exec. Time
"System.InvalidCastException: Specified cast is not valid.
at System.Convert.ToDecimal(Object Value)??

//
// sqlInsertCommand1
//
try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost])" +
" values txtServiceCode,txtServiceDesc,cbLargeAnimalCost
,cbMediumAnimalCost,cbSmallAnimalCost)";
sqlInsertCommand1.Parameters.Add("(@[service-code]",SqlDbType.Int).Value =
txtServiceCode;
sqlInsertCommand1.Parameters.Add("(@[service-escription]",SqlDbType.VarChar).Value
= txtServiceDesc;
sqlInsertCommand1.Parameters.Add("(@[large-animal-cost]",SqlDbType.Money).Value
= Convert.ToDecimal(cbLargeAnimalCost);
sqlInsertCommand1.Parameters.Add("(@[medium-animal-ost]",SqlDbType.Money).Value
= Convert.ToDecimal(cbMediumAnimalCost);
sqlInsertCommand1.Parameters.Add("(@[small-animal-cost]",SqlDbType.Money).Value
= Convert.ToDecimal(cbSmallAnimalCost);

sqlInsertCommand1.Connection = sqlConnection1;
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");

}


Miha Markic said:
Something like:

sqlInsertCommand1.CommandText = "INSERT INTO
services([service-code],...,[small-animal-cost]) VALUES (@ServiceCode,
...,
@SmallAnimalCost)";
sqlInsertCommand1.Parameters.Add("(@ServiceCode", SqlDbType.Int).Value =
servicecode;
....
sqlInsertCommand1.Parameters.Add("(@SmallAnimalCost",
SqlDbType.Money).Value
= Convert.ToDecimal(smallanimalcost);

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

....
nbohana said:
Thanks for your information, could give me and example, I have tried
and
cannot code it correctly. I am a little new at this and could use some
help.
Thanks in advance.

Norm


:

Hi,

You should really use parametrised commands (see
SqlCommand.Parameters,
SqlParameter).
As for converting text to money use Convert.ToDecimal (and use
SqlDbType.Money as parameter type).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

I am using a data window with textbox's. some of the data is money
keyed
in
as "60.00". I need to convert this to money before I can save it to
a
sql
data table.
the code is done in C#. can someone help me!!! The specific code is
included.

SqlCommand cmdGetIdentity = new SqlCommand();

System.Text.StringBuilder b;

try
{
b = new System.Text.StringBuilder(" ");
b.Append("INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values('");
b.Append(txtServiceCode.Text);
b.Append("','");
b.Append(txtDescription.Text);
b.Append("','");
b.Append(cbLargeAnimalCost.Text);
b.Append("','");
b.Append(cbMediumAnimalCost.Text);
b.Append("','");
b.Append(cbSmallAnimalCost.Text);
b.Append("')" );

sqlInsertCommand1.CommandText=b.ToString();
this.sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " +
txtServiceNumber.Text
+
" Successful.","");

}
 
Miha, Now that I have converted to money, I need to display in the correct
format.
i.e. (60) should be ($60.00), how do I find how to do this or can you tell me.

Thanks in advanced Norm
 
The data consumer is a textbox.

Miha Markic said:
What is the data consumer?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
Miha, Now that I have converted to money, I need to display in the correct
format.
i.e. (60) should be ($60.00), how do I find how to do this or can you tell
me.

Thanks in advanced Norm
 
Hi,

You might use Binding Class' Format (and Parse) event to format the value.
See help on Binding Class.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
The data consumer is a textbox.

Miha Markic said:
What is the data consumer?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
Miha, Now that I have converted to money, I need to display in the
correct
format.
i.e. (60) should be ($60.00), how do I find how to do this or can you
tell
me.

Thanks in advanced Norm


:

It works, thank you for your help!!!

Norm
 
Back
Top