Showing posts with label float. Show all posts
Showing posts with label float. Show all posts

Sunday, February 19, 2012

CDBVariant

Hi

got small problem. I got three rows in a table

Index (int)
Product (varchar)
Price (float)

I could successfully connect to the database. I also get the values and store them in a CDBVariant type. I can't put the values into List Control Box... insertItem always return -1. It ask for LPCTSTR type.

Anyone can help me please, hanging around here know for hours?

Please
Thx

CODE

{
CDBVariant value;
char sql_statement [2048] = "";

//CDatabase object "db" created to connect database
CDatabase db;
db.OpenEx(_T("DSN=Beauty"),CDatabase::noOdbcDialog);

//CRecordset object "rs" created to access and manipulate database records.
CRecordset rs(&db);
strcpy(sql_statement,_T("SELECT * FROM TestTabelle"));
rs.Open(CRecordset::forwardOnly,sql_statement);

//Get quantity from Database
int n = rs.GetODBCFieldCount( );

while(!rs.IsEOF())
{
for( int i = 0; i < n; i++ )

{

rs.GetFieldValue("index",value);
m_Buy_List.InsertItem(i,LPCTSTR(value.m_lVal)); Won't work at all

rs.GetFieldValue("product",value.m_pstring);
m_Buy_List.SetItemText(i,2,LPCTSTR(value));Getting weird String for example "Soap" and ListBox shows "Aq3"

rs.GetFieldValue("price",value);
m_Buy_List.SetItemText(i,3,LPCTSTR(value.m_fltVal) ); Won't accept float

rs.MoveNext( );
}
}Perhaps you would get better results on a C++ forum...

If you know that the values must be strings, and you are retrieving each one explicitly, why not just create three variables (strings), and use those. Seems like it would be a lot easier and less trouble prone.

If you are concerned about NULLs (which you should be), change your SQL statement to use the ISNULL function on each of the fields you are dealing with:

SELECT ISNULL(index,0) AS INDEX, ISNULL(product,'') AS PRODUCT, ISNULL(price,0) AS PRICE FROM TestTabelle|||thx

i figured another way out... getFieldValue isn't accepting strings so i converted them in char :)

weird... someday i will find the official way... i will change it then :)

iam new into c++ and ms sql... learning by doing :)

CCur

this document says that CCUR is supported in SSRS 2005; but I'm trying
to convert a float to money format-- so it gets 2 decimal places and a
dollar sign in front of it.
and i just can't get CCUR to work _AT_ALL_ in SSRS 2005.
http://msdn2.microsoft.com/en-us/library/ms157205.aspx
my actual code is
="The premium for your plan is " & Parameters!AdHoc_PremiumAmount.Value
& "."
I'm trying to do this:
="The premium for your plan is " &
CCUR(Parameters!AdHoc_PremiumAmount.Value) & "."
can anyone help me figure out what I'm doing wrong?
I've got to allow the end users to enter a premiumAmount as a
parameter; the possible datatypes are String, Integer, Float; etc...
I just want the end users to be able to enter 200 and then my CCUR
function should automagically convert it to '$ 200.00'
shouldnt these things just work out of the box?
-Aaronhello?
does anyone else concur?
I just wish that MS would start doing a decent job of keeping track of
things like this.
heres some helpful hints, microsoft
KEEP YOUR DATA IN A DATABASE INSTEAD OF 100,000 DIFFERENT COPIES OF
XML!
-Aaron
aaron.kempf@.gmail.com wrote:
> this document says that CCUR is supported in SSRS 2005; but I'm trying
> to convert a float to money format-- so it gets 2 decimal places and a
> dollar sign in front of it.
> and i just can't get CCUR to work _AT_ALL_ in SSRS 2005.
> http://msdn2.microsoft.com/en-us/library/ms157205.aspx
> my actual code is
> ="The premium for your plan is " & Parameters!AdHoc_PremiumAmount.Value
> & "."
> I'm trying to do this:
> ="The premium for your plan is " &
> CCUR(Parameters!AdHoc_PremiumAmount.Value) & "."
>
> can anyone help me figure out what I'm doing wrong?
> I've got to allow the end users to enter a premiumAmount as a
> parameter; the possible datatypes are String, Integer, Float; etc...
> I just want the end users to be able to enter 200 and then my CCUR
> function should automagically convert it to '$ 200.00'
> shouldnt these things just work out of the box?
> -Aaron|||yeah I need to do the same thing.. anyone have any ideas'
-Susie
On Oct 3, 5:19 pm, aaron.ke...@.gmail.com wrote:
> this document says that CCUR is supported in SSRS 2005; but I'm trying
> to convert a float to money format-- so it gets 2 decimal places and a
> dollar sign in front of it.
> and i just can't get CCUR to work _AT_ALL_ in SSRS 2005.
> http://msdn2.microsoft.com/en-us/library/ms157205.aspx
> my actual code is
> ="The premium for your plan is " & Parameters!AdHoc_PremiumAmount.Value
> & "."
> I'm trying to do this:
> ="The premium for your plan is " &
> CCUR(Parameters!AdHoc_PremiumAmount.Value) & "."
> can anyone help me figure out what I'm doing wrong?
> I've got to allow the end users to enter a premiumAmount as a
> parameter; the possible datatypes are String, Integer, Float; etc...
> I just want the end users to be able to enter 200 and then my CCUR
> function should automagically convert it to '$ 200.00'
> shouldnt these things just work out of the box?
> -Aaron

Sunday, February 12, 2012

casting float output param throws an exception.

I keep getting an exception when trying to cast an output param as a float type. the SPROC is marked as float OUTPUT and the Cost column in the database is a float type as well. And there are no nulls in the cloumn either. here is how my code looks:


SqlParameter prmCost= new SqlParameter("@.Cost", SqlDbType.Float,8);
prmCost.Direction=ParameterDirection.Output;
cmd.Parameters.Add(prmCost);

//...blah blah blah

//invalid cast gets throw on here (it happens with all my float types)
productDetails.Cost=(float)prmCost.Value;

Any suggestions as to what I am doing wrong?You have to use the <datatype>.Parse() methods to covert to numbers, e.g.


float test = float.Parse(prmCost.Value.ToString());
|||If you do a watch on prmCost before the error do you see a reasonable value in there?

If so, maybe try the cast with a double on the .Net side and see if you can get it in. Try the code below to help debug. If it still crashes maybe the exception message will be more helpful than your current one.


string message = "";
double doubleValue = 0.0;
try{
string seeWhatsThere = prmCost.Value.ToString();
// put a watch on seeWhatsThere to see what it looks like
doubleValue = System.Double.Parse( seeWhatsThere );
}
catch( FormatException ex){
message = ex.Message;
}
catch( Exception ex ){
message = ex.Message;
}

|||Yes, there is a reasonable value for the parameter (I check the param for a null value before attempting to cast it). I did it as follows, but I'm not sure if I will loose any precision this way. From what I understand Single is the same thing as float, correct me if I'm wrong.


product.Cost=Convert.ToSingle(prmCost.Value);

Thanks for the help.

Friday, February 10, 2012

cast from float to varchar

Hi,

Can I convert from float to varchar without trunc the values? Can I use any mask like '#.##'?

from -> cast ( 123.44 as varchar(256) )

result = '123.44'

thanks,

Hi Alessandro,

As long as the varchar type you are casting to is long enough, no truncation of the float value will occur.

For example, cast(23.444 as varchar(3)) will result in a overflow, where cast(23.444 as varchar(6)) will return correctly.

Is that what you meant?

Cheers,

Rob