可以转换为'string'的类型的对象是必需的 [英] An object of a type convertible to 'string' is required

查看:98
本文介绍了可以转换为'string'的类型的对象是必需的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该类中创建login_class我编码这个



  public   void  loginfu( string  username, string 密码)
{
尝试
{
实体2 lg = new Entities2();

列表< SP_Get> gr = lg.SP_GetL(用户名,密码).ToList();
DataTable dt = new DataTable();
dt.Columns.Add( id typeof string ));
foreach var l in gr)
{
dt.Rows.Add(l.id);
}

}
catch (例外)
{
throw new 异常();

}
return dt;
}





我尝试过:



如何在会话中传递列ID

解决方案

这是一个相当简单的错误定位,如果你有代码 - 我们没有。

这是一个编译器错误,它正在发生,因为你试图做这样的事情:

  private   void  MyMethod(){...} 
...
var x = MyMethod()。DoSomething();

因为MyMethod被定义为返回一个void - 即没有值 - 编译器正在抱怨(正确)你可以'使用。在调用方法时。



但是我们无法分辨它抱怨哪一行 - 所以看看错误信息,它会告诉你究竟是哪个文件并且该文件中的行发现了问题。看看你在这一行上调用的eth方法,它应该是非常明显的。修复它取决于你:为什么要调用该方法并期望它返回一个值?


你已经定义了 loginfunction()返回 bool 不是 DataTable 因此错误在线:

< pre lang =C#> ...
string logn = Convert.ToString(lg_class.loginfunction(UserName,Password).Tables [ 0 ]。行[ 0 ] [ ID]);
...


i create login_class in that class i code this

public void loginfu(string username,string password)
   {
       try
           {
              Entities2 lg = new Entities2();

              List<SP_Get> gr = lg.SP_GetL(username, password).ToList();
               DataTable dt = new DataTable();
               dt.Columns.Add("id", typeof(string));
               foreach (var l in gr)
               {
                   dt.Rows.Add(l.id);
               }

           }
          catch (Exception)
           {
               throw new Exception();

           }
           return dt;
   }



What I have tried:

how i pass column id in session

解决方案

That's a fairly simple error to locate, if you have the code - which we don't.
It's a compiler error and it's coming because you have tried to do something like this:

private void MyMethod() { ... }
...
var x = MyMethod().DoSomething();

Because MyMethod is defined as returning a void - i.e. no value - the compiler is complaining (correctly) that you can't use "." on a call to the method.

But we can't tell which line it complains about - so look at the error message, and it will tell you exactly which file and line within that file it found the problem with. Look at eth methods you call on that line, and it should be pretty obvious. Fixing it is up to you: why are you calling that method and expecting it to return a value at all?


You have defined loginfunction() to return bool not a DataTable hence the error on line :

...
string logn = Convert.ToString(lg_class.loginfunction(UserName, Password).Tables[0].Rows[0]["id"]);
...


这篇关于可以转换为'string'的类型的对象是必需的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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