简体   繁体   English

我无法将带有convert.ToString()的int和float转换为字符串

[英]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.

相关问题 之间的区别“ Convert.ToString(Nullable <int> )和“可为空 <int> 的ToString()”? - Difference between “Convert.ToString(Nullable<int>)” and “Nullable<int>.ToString()”? (string)reader [0] vs Convert.ToString(reader [0]) - (string)reader[0] vs Convert.ToString(reader[0]) 为什么Convert.ToString(object为null)vs Convert.ToString(string为null)的行为 - Why the behavior of Convert.ToString(object as null) vs Convert.ToString(string as null) Convert.ToString不处理null - Convert.ToString not handling null myData as string,(string)myData和Convert.ToString(myData)之间的差异 - Diference between myData as string, (string) myData and Convert.ToString(myData) Convert.ToString(Decimal,IFormatProvider)或String.Format - Convert.ToString(Decimal, IFormatProvider) or String.Format Convert.ToString返回string.empty而不是null - Convert.ToString returns string.empty instead of null Convert.ToString(字符串值)可以做什么吗? - Does Convert.ToString(string value) do anything? Convert.ToString对“NULL对象”和“NULL字符串”的行为有所不同 - Convert.ToString behaves differently for “NULL object” and “NULL string” Convert.ToString(...)或Object.ToString()以提高性能 - Convert.ToString(…) or Object.ToString() for performance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM