[英]I can't convert an int and a float with convert.ToString() to string
I have 2 controls in my form,one is an int and the other is a float,I tried to convert with the "Convert.toString()" method but it didn't work and I get all the time a null value this is my code: 我的表单中有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);
this is the GetRecordsetValue method: 这是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();
}
}
thanks for Help 感谢帮助
The code in GetRecordsetValue
returns NULL if your query doesn't find any record matching the WHERE condition. 如果您的查询找不到与WHERE条件匹配的记录,则GetRecordsetValue
的代码将返回NULL。 The null value returned is passed back to Convert.ToString() and this throws the exception 返回的空值传递回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 = "";
}
There is also the problem of the value passed inside the GetRecordsetValue
. 还存在在GetRecordsetValue
内部传递值的问题。 This value is passed as a string and it is added inside the parameter collection as a string datatype. 该值作为字符串传递,并作为字符串数据类型添加到参数集合内。 If the field id_amort
is not a string (as it seems from its name) then it is highly probable that your query cannot find the record. 如果字段id_amort
不是字符串(从它的名称看来),那么您的查询很可能找不到记录。
I suggest to create different overloads of GetRecordsetValue. 我建议创建不同的GetRecordsetValue重载。 One that takes an integer for example 以整数为例的一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.