[英]Difference between “Convert.ToString(Nullable<int>)” and “Nullable<int>.ToString()”?
[英]I can't convert an int and a float with convert.ToString() to string
我的表单中有2个控件,一个是int,另一个是float,我尝试使用“ Convert.toString()”方法进行转换,但是它没有用,并且我一直都得到null值,这是我的代码:
string req = "select coef from amortissement where id_amort =@p";
textBox4.Text =Convert.ToString(GetRecordsetValue(req, textBox1.Text) as String)
req = "select Plan from amortissement where id_amort =@p";
textBox3.Text =Convert.ToString(GetRecordsetValue(req, textBox1.Text) as String);
这是GetRecordsetValue方法:
private Object GetRecordsetValue(string req, string sValParam)
{
// ExecuteScalar est optimisée pour récupérer une seule valeur
SqlCommand cmd = null;
try
{
cmd = new SqlCommand(req, con);
cmd.Parameters.AddWithValue("@p", sValParam);
return cmd.ExecuteScalar();
}
catch
{
return String.Empty;
}
finally
{
cmd.Dispose();
}
}
感谢帮助
如果您的查询找不到与WHERE条件匹配的记录,则GetRecordsetValue
的代码将返回NULL。 返回的空值传递回Convert.ToString(),这将引发异常
string req = "select coef from amortissement where id_amort =@p";
object result = GetRecordsetValue(req, textBox1.Text);
if(result != null)
{
textBox4.Text = Convert.ToString(result);
}
else
{
// and/or a message to the user to correct its inputs.
textBox4.Text = "";
}
还存在在GetRecordsetValue
内部传递值的问题。 该值作为字符串传递,并作为字符串数据类型添加到参数集合内。 如果字段id_amort
不是字符串(从它的名称看来),那么您的查询很可能找不到记录。
我建议创建不同的GetRecordsetValue重载。 以整数为例的一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.