【问题标题】:fill object data source with class in another project用另一个项目中的类填充对象数据源
【发布时间】:2018-03-22 10:57:26
【问题描述】:

我在 c# 和 linq 中退出了新手,我需要使用另一个项目中的类(在 .c# 中)使用代码填充 ObjectDataSource(在 vb.net 中)。

我尝试做这些事情:
1.添加参考
2. 后台代码中的“导入 Project2”。
3.填写加载事件:

Dim ObjectClass1 As New Class1(ConnectionString)

myODS.TypeName = "ObjectClass1"
myODS.SelectMethod = "GetData"
myODS.SelectParameters("Filter1Name") = New Parameter("Filter1Name", DbType.String, "xxx")
  1. 另一个项目中的类是这样的:

    public class Class1
       {
        public string ConnectionString { get; }
    
        public Class1(string connectionString)
        {
            ConnectionString = connectionString;
        }
    
        public IQueryable<MyObject> GetData(string Filter1Name)
          {
           using (MyObjectDataContext dataContext = new  MyObjectDataContext(ConnectionString))
              {
                var Result = dataContext.MyObject.Where(x => x.FILTER_1== Filter1Name).Select(x => new MyObject
                      {
                        Field1 = x.FIELD_1,
                        Field2 = x.FIELD_2
                      });
               return Result;
              }
            }
        }
    

这是我得到的错误: ObjectDataSource“myODS”找不到具有参数的非泛型方法“GetData”:Filter1Name

这个错误意味着什么?有可能做我想做的事吗?

【问题讨论】:

  • GetInfo & ObjectClass1 属于什么?您使用了错误的类型名称、选择方法名称或 GetInfo 的参数无效。
  • 对不起,我在这里写问题的时候搞错了……GetInfo是GetData(Class1类中的方法)。而关于 ObjectClass1 变量....是页面中 Class1 的实例。

标签: c# vb.net linq objectdatasource


【解决方案1】:

这意味着它在您的类中找不到名为 GetInfo() 的方法 查看您的 class1 定义,您可能打算将其设为 GetData

将 ObjectClass1 调暗为新的 Class1(ConnectionString)

myODS.TypeName = "ObjectClass1"
myODS.SelectMethod = "GetData"
myODS.SelectParameters("Filter1Name") = New Parameter("Filter1Name", DbType.String, "xxx")

【讨论】:

  • 我在这个例子中犯了错误,但在我的代码中没有。您对错误有任何线索吗?
  • @Melipao 我认为你的TypeName 是错误的。您将其设置为保存class1 实例的变量的名称。它应该是具有完整命名空间的 class1 类型,例如 Samples.AspNet.CS.Class1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多