data conversion to double [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Can someone tell me why this C# code
item.Price = Convert.ToDouble(rdr["Ar"]);
gives me an error:
Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
The Price item is double, rdr is a SqlDataReader and Ar is a float type column of a table ... I thought that I should use float too in C# but I think that has other representation.
Can someone help me with this? I am trying to get some prices from the DB but it's not working. If you have any suggestions?

The problem is that item.Price is defined as an integer.

One possible answer is that the Convert.ToDouble() method is seeing your DBs float as a double, yet the expected parameter is an int (seeing as you wouldnt convert from double to double). Have you tried this?:
item.Price = (double)rdr["Ar"];

Related

C# CodeDom Cant type in " [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I want to compile a code with codedom which should connect to my ftp server.
But I cant type in the credentials because of the ""...
Look here :
Temp.AppendLine(#"request.Credentials = new NetworkCredential("userid","userpassword");");
If I type " in the code, it automatic ends the content of the brackets...
Help?
You may need to escape the content by using double quotes, like this:
Temp.AppendLine(#"request.Credentials = new NetworkCredential(""userid"",""userpassword"");");
Temp.AppendLine(#"request.Credentials = new NetworkCredential(""userid"",""userpassword"");");
Escape the " with ""

"Error converting data type double to float" on inserting to DECIMAL column in SQL Server [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've got a SQL Server table with a DECIMAL(10,2) column. This should fit numbers up to 99999999.99. But when I insert any number >= 1000 into it via the following C# code, I get the exception
Error converting data type double to float.
If the value being inserted is < 1000, it works fine. The C# value is a double by the way.
Can anyone explain this please?
// ...
Database.AddInParameter(cmd, "#AssessmentScaleScore", DbType.Double,
(goal.StudentAssessmentInstanceID > 0
&& goal.AssessmentScaleScore > 0)
? goal.AssessmentScaleScore
: System.Data.SqlTypes.SqlDouble.Null);
// ...
Database.ExecuteDataSet(cmd);
So again, the goal.AssessmentScaleScore is a double, and I'm inserting it into a DECIMAL(10,2) column in the DB. If the value is < 1000 it works fine, but if its >= 1000 I get an exception.
I don't get it.
Sorry, I jumped the gun asking this here.
Turns out I'm an idiot and my parameter in my stored procedure has it as DECIMAL(5,2) instead of DECIMAL(10,2) like the DB column.
So I was overflowing the parameter in the procedure, not the column in the DB.

Converting C# line to VB.net [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I was trying to convert the following c# code to vb.net.
I see the problem is my lack of familiarity with the syntax of the parameters of OrderByDescending() What is the proper VB.Net equivalent of the C# line?
//C# code
SelectedFolder.Search("ALL", true).OrderByDescending(_ => _.Date).ToList();
//VB.Net part which doesn't work
For Each msg In SelectedFolder.Search("ALL", True).OrderByDescending(Function(_).[Date]).ToList()
After removing the underscore before [Date] the error became,
Error 1 Identifier expected.
The _ character is a line continuation in VB. Try changing the variable name to something more common, like x
For Each msg In SelectedFolder.Search("ALL", True).OrderByDescending(Function(x) x.[Date]).ToList()

what does format {0:x} mean? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I came across this C# literal and was wondering what does it mean?
Especially, in the following case:
string.Format("{0:x}", byteArray[i]);
Thanks
It means format the first argument (index 0) as hexadecimal: http://msdn.microsoft.com/en-us/library/s8s7t687(v=vs.80).aspx
It means the first argument will be output as hexadecimal (in lowercase !!).
To output uppercase you could use "{0:X}".
Look msdn for more info about string formatting : MSDN Custom string format
This represents the hexadecimal format.

explicitly casting generics in C# 4 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Can anyone help me in casting generic collection in c# 4.
Here is the code snippet.
GridView1.DataSource = dataServiceColl.Select(t => t.product_desc="EdibileItem")
It is throwing up runtime error at the below line,
Gridview1.Databind();
Saying it is a HTTP Exception.
I think it should be a simple type cast.
Thanks,
Kris.
Use
t => t.product_desc=="EdibileItem"
HTTP Exception? That has nothing to do with casting.
More importantly, why are you assigning "EdibleItem" to t.product_desc here?
Select(t => t.product_desc="EdibileItem")
Did you meant == instead of =? If so, would a Where be more appropriate than a Select?
I think it all boils down to: what are you trying to achieve, exactly?

Categories