将String转换为In16 [英] cast String to In16

查看:64
本文介绍了将String转换为In16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字符串从一个Web表单文本框转换为一个Int16(参见下面的代码

),但它不太合适。当我用数字发布文本框(没有

alpha字符)时,我的网页出现以下错误:

异常详细信息:System.FormatException:输入字符串不在一个正确的

格式。

字符串数量;

foreach(GridView1.Rows中的GridViewRow行)

{<如果(!String.IsNullOrEmpty(qty)|| qty!=" 0")
$
.FindControl(" txtQuantity"))。文字;

order.quantity = Int16.Parse(qty); //调试器在此行停止

// order.quantity被定义为Int16

}

}

我不明白为什么我得到一个FormatException,因为我没有使用任何

格式。我应该采用另一种方法来构建我的文本框

值吗?

I''m trying to cast a String from a web forms textbox into an Int16 (see code
below), but it''s not quite working. When I post the textbox with numbers (no
alpha chars) I get the following error in my web page:
Exception Details: System.FormatException: Input string was not in a correct
format.
String qty;
foreach (GridViewRow row in gridView1.Rows)
{
if (!String.IsNullOrEmpty(qty) || qty != "0")
{
qty = ((TextBox)row.FindControl("txtQuantity")).Text;
order.quantity = Int16.Parse(qty); // debugger stops on this line
// order.quantity is defined as an Int16
}
}

I don''t understand why I''m getting a FormatException since I''m not using any
formatting. Is there another approach I should be taking to cast my textbox
value?

推荐答案

archuleta37写道:
archuleta37 wrote:

我正在尝试将一个字符串从Web表单文本框转换为Int16(参见下面的代码

),但它'不太合适。当我用数字发布文本框(没有

alpha字符)时,我的网页出现以下错误:

异常详细信息:System.FormatException:输入字符串不在一个正确的

格式。


字符串数量;

foreach(GridView1.Rows中的GridViewRow行)

{

if(!String.IsNullOrEmpty(qty)|| qty!=" 0")

{

qty =( (TextBox)row.FindControl(" txtQuantity"))。Text;

order.quantity = Int16.Parse(qty); //调试器在此行停止

// order.quantity被定义为Int16

}

}

我不明白为什么我得到一个FormatException,因为我没有使用任何

格式。我应该采用另一种方法来构建我的文本框

值吗?
I''m trying to cast a String from a web forms textbox into an Int16 (see code
below), but it''s not quite working. When I post the textbox with numbers (no
alpha chars) I get the following error in my web page:
Exception Details: System.FormatException: Input string was not in a correct
format.
String qty;
foreach (GridViewRow row in gridView1.Rows)
{
if (!String.IsNullOrEmpty(qty) || qty != "0")
{
qty = ((TextBox)row.FindControl("txtQuantity")).Text;
order.quantity = Int16.Parse(qty); // debugger stops on this line
// order.quantity is defined as an Int16
}
}

I don''t understand why I''m getting a FormatException since I''m not using any
formatting. Is there another approach I should be taking to cast my textbox
value?



当你收到错误时,qty的值是多少?


-

Larry Lard
la *******@googlemail.com

地址是真实的,但未读 - 请回复小组

对于VB和C#问题 - 告诉我们哪个版本

What is the value of qty when you get the error?

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version


那么,数字是什么?你在给它吗?什么*确切*是数量,当它失败?

另外 - 如果2.0你可能希望看看TryParse,但这没有帮助 - 它

会失败以不同的方式。


另外 - 你的代码是按原样吗?在第一次测试之前你没有分配到数量,

所以我认为它是释义(因为它不会编译)......如果是这样,我想知道是否

也许这个错误跟踪了?


Marc
Well, what "numbers" are you giving it? What *exactly* is qty when it fails?
Also - if 2.0 you may wish to look at TryParse, but that wouldn''t help - it
would just fail in a different way.

Also - is your code "as is"? You don''t assign to qty before the first test,
so I assume it is paraphrased (since it won''t compile)... if so, I wonder if
perhaps the bug went with the snip?

Marc


它看起来像你传递给Int16的字符串.Parse没有

代表有效整数。在你通过Parse方法传递它之前,你是否尝试过检测数量字符串的实际价值?


-

Stoitcho Goutsev(100)

" archuleta37" < ar ********* @ discussion.microsoft.com写信息

news:78 ******************* *************** @ microsof t.com ...
It looks like thes string that you pass to the Int16.Parse does not
represent valid integer number. Have you tried to inpect the actual value of
the qty string before you pass it ti the Parse method?

--
Stoitcho Goutsev (100)

"archuleta37" <ar*********@discussions.microsoft.comwrote in message
news:78**********************************@microsof t.com...

我正在尝试从一个字符串中转换一个字符串web将文本框形成一个Int16(参见

代码

以下),但它不太合适。当我发布带有数字的文本框

(没有

alpha chars)时,我的网页出现以下错误:

异常详情:系统.FormatException:输入字符串不是

正确

格式。


字符串数量;

foreach(GridView1.Rows中的GridViewRow行)

{

if(!String.IsNullOrEmpty(qty)|| qty!=" 0")

{

qty =((TextBox)row.FindControl(" txtQuantity"))。文字;

order.quantity = Int16.Parse(qty) ; //调试器在此行停止

// order.quantity被定义为Int16

}

}

我不明白为什么我得到一个FormatException,因为我没有使用

任何

格式化。我应该采取另一种方法来施放我的

文本框

值吗?
I''m trying to cast a String from a web forms textbox into an Int16 (see
code
below), but it''s not quite working. When I post the textbox with numbers
(no
alpha chars) I get the following error in my web page:
Exception Details: System.FormatException: Input string was not in a
correct
format.
String qty;
foreach (GridViewRow row in gridView1.Rows)
{
if (!String.IsNullOrEmpty(qty) || qty != "0")
{
qty = ((TextBox)row.FindControl("txtQuantity")).Text;
order.quantity = Int16.Parse(qty); // debugger stops on this line
// order.quantity is defined as an Int16
}
}

I don''t understand why I''m getting a FormatException since I''m not using
any
formatting. Is there another approach I should be taking to cast my
textbox
value?



这篇关于将String转换为In16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆