我对ASP.NET和C#有一点问题.这是我的错误代码:
mscorlib.dll中出现"System.FormatException"类型的异常,但未在>用户代码中处理
附加信息:输入字符串的格式不正确.
protected void Page_Load(object sender, EventArgs e)
{
if(this.IsPostBack == false)
{
Currency.Items.Add(new ListItem("Euro", "0.85"));
Currency.Items.Add(new ListItem("Yen", "11.30"));
Currency.Items.Add(new ListItem("PLN", "4.20"));
Currency.Items.Add(new ListItem("GBP", "5.62"));
}
}
protected void Convert_Click(object sender, EventArgs e)
{
decimal oldAmount;
bool succes = Decimal.TryParse(TextBox.Value, out oldAmount);
if(succes)
{
ListItem item = Currency.Items[Currency.SelectedIndex];
decimal newAmount = oldAmount * decimal.Parse(item.Value);
Result.InnerText = "Result: " + newAmount;
}
}
Run Code Online (Sandbox Code Playgroud)
我试过Decimal.Parse,Decimal.TryParse和其他奇怪的组合.现在我确定问题是字符串并将它们解析为十进制.当我创建String变量时 - 解析时出现了同样的错误.那么有人可以告诉我该怎么做才能将String转换为十进制?
目前我正在编写一种从 CSV 文件读取数据并导入到 SQL 表的方法。
DataTable dt = new DataTable();
String line = null;
int i = 0;
while ((line = reader.ReadLine()) != null)
{
String[] data = line.Split(',');
if (data.Length > 0)
{
if (i == 0)
{
foreach (object item in data)
{
DataColumn c = new DataColumn(Convert.ToString(item));
if (Convert.ToString(item).Contains("DATE"))
{
c.DataType = System.Type.GetType("System.DateTime");
}
else { c.DataType = System.Type.GetType("System.String"); }
dt.Columns.Add(c);
}
i++;
}
else
{
DataRow row = dt.NewRow();
for (int j = 0; j < …Run Code Online (Sandbox Code Playgroud) private void ReadUnitPrice()
{
Console.Write("Enter the unit gross price: ");
unitPrice = double.Parse(Console.ReadLine());
}
Run Code Online (Sandbox Code Playgroud)
这应该有效,但我遗漏了一些明显的东西.每当我输入一个double时,它就会给出错误:System.FormatException:输入字符串的格式不正确.请注意,'unitPrice'被声明为double.
我通过解析XElement从xml中检索日期和时间字符串.日期和时间值分别由file.Element("Date").Value和检索
file.Element("Time").Value.
检索Date值后,我将其解析为DateTime变量
DateTime dt,ts;
dt = file.Element("Date").Value; // the value is say 12/29/2012
Run Code Online (Sandbox Code Playgroud)
然后将此dt值设置为xaml UI上的datepicker值
datepicker.Value = dt;
Run Code Online (Sandbox Code Playgroud)
我还有一个timepicker,其值必须由从xml检索的Time值设置.要设置timepicker值,请执行以下操作.声明3个字符串,说:
string a = file.Element("Time").Value; // the value is say 9:55 AM
string b = file.Element("Time").Value.Substring(0, 5) + ":00"; // eg 9:55:00
string c = file.Element("Time").Value.Substring(5); // the value is ' AM'
Run Code Online (Sandbox Code Playgroud)
然后我连接日期值和字符串'b'和'c'
string total = file.Element("Date").Value + " " + b + c;
Run Code Online (Sandbox Code Playgroud)
价值total现在是'12/29/2012 9:55:00 AM'
然后我尝试将此total字符串解析为DateTime,但它会抛出一个formatexception
DateTime.Parse(total, CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏...
我尝试使用decimal.parse,如下所述:http://msdn.microsoft.com/en-us/library/cafs243z( v = vs1010) .aspx
所以我从这个页面复制了以下例子:
string value;
decimal number;
value = "1.62345e-02";
try
{
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个FormatException,你知道它为什么会发生吗?
谢谢,eyal
我试图在有人试图输入非整数字符的实例中抛出格式异常.
Console.WriteLine("Your age:");
age = Int32.Parse(Console.ReadLine());
Run Code Online (Sandbox Code Playgroud)
我不熟悉C#语言,可以使用帮助为此实例编写try catch块.
非常感谢.
为什么Another exception was thrown: FormatException: Invalid number (at character 1)在一切恢复正常之前,我的屏幕上会出现错误几微秒。有时它甚至不会发生。下面是我的 StreamBuilder 函数:
_delivered() {
print('In the delivered function:${resId},${customerId}, ');
return StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('restaurants')
.document(resId)
.collection('customers')
.document(customer)
.collection('orders')
.where('deliveryTime', isGreaterThan: '')
.snapshots(),
builder: (context, snapshot) {
print('Does snapshop have data? ${snapshot.hasData}');
if (!snapshot.hasData) return Container();
List deliveredListFromServer = snapshot.data.documents;
return Expanded(
child: ListView(
shrinkWrap: true,
children: deliveredListFromServer.map((item) {
print('document id: ${item.documentID}');
return InkWell(
child: SizedBox(
height: 50,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
width: 80,
child: Text( …Run Code Online (Sandbox Code Playgroud) 为什么以下代码会生成 FormatException?
DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", null);
Run Code Online (Sandbox Code Playgroud)

也许这与代码在 IIS 7.5 Express 下作为 MVC3 站点执行逻辑的一部分运行的事实有关?
我试图将一些字符串解析为double值,使用此解析方法重载:
double.Parse("198.222213745118", CultureInfo.CurrentUICulture);
Run Code Online (Sandbox Code Playgroud)
CultureInfo.CurrentUICulture是fr-FR.但这是抛出FormatException类型的异常.
可能是什么原因?
我正在尝试将数组元素格式化为一个表格,其中一些列具有正确的对齐方式,而另一些则如复合格式化文章中所示(或者C#插入字符串中的可选参数是什么?)
我为什么FormatException接听电话Console.WriteLine?
以下是演示此示例的示例:
using System;
namespace ConsoleApplication97
{
class Program
{
public struct User
{
public string Name;
public byte Age;
}
static void Main(string[] args)
{
User[] Bar = new User[2];
Bar[0].Name = "Jan Kowalski";
Bar[0].Age = 32;
Bar[1].Name = "Piotr Nowak";
Bar[1].Age = 56;
for (int i = 0; i < Bar.Length; i++)
{
Console.WriteLine("{0 , -15} | { 1, 5}", Bar[i].Name, Bar[i].Age);
}
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
值"名称"应位于左侧(对齐-15),"年龄"应位于右侧(对齐5).