简体   繁体   English

尝试从数据库中检索时出错

[英]Error when trying to retrieve from database

I have generated three classes from XSD schema:我从 XSD 模式生成了三个类:

public partial class PathDetailsMessage {
    private MessageHeader messageHeaderField;

    public MessageHeader MessageHeader {
        get {
            return this.messageHeaderField;
        }
        set {
            this.messageHeaderField = value;
        }
    }
}


public partial class MessageHeader {
    private Recipient recipientField;

    public Recipient Recipient {
        get {
            return this.recipientField;
        }
        set {
            this.recipientField = value;
        }
    }
}


public partial class Recipient {
    private string valueField;

    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

When I try to put the value of id (which is a char in database) to medusa.MessageHeader.Recipient.Value, I get an error (see below):当我尝试将 id 的值(数据库中的一个字符)放入 medusa.MessageHeader.Recipient.Value 时,我收到一个错误(见下文):

if (dr.HasRows)
{
  dr.Read();
  medusa.MessageHeader.Recipient.Value = Convert.ToString(dr["id"]);
  dr.Close();
}

I get the following error:我收到以下错误:

 System.NullReferenceException: 'Object reference not set to an instance of an object.' PathDetailsMessage.MessageHeader.get returned null.

I use the following SQL string:我使用以下 SQL 字符串:

string sqlString = "SELECT id FROM view_otf WHERE id LIKE '%079%'";

id column is CHAR in database. id 列是数据库中的 CHAR。

I can't figure out a solution for this issue.我想不出这个问题的解决方案。

Can you help me?你能帮助我吗?

Thank you.谢谢你。

LE : Those 3 classes are in the same.cs file. LE :这 3 个类在同一个.cs 文件中。

LE2 : medusa is an object initialized from the PathDetailsMessage.cs file, class that was created from XSD schema LE2 :美杜莎是一个 object 从 PathDetailsMessage.cs 文件中初始化,class 是从 XSD 模式创建的

PathDetailsMessage medusa = new PathDetailsMessage();

We are missing the medusa initialization code, but you should do something like this:我们缺少美medusa初始化代码,但你应该这样做:

medusa.MessageHeader = new MessageHeader {
    Recipient = new Recipient()
};
medusa.MessageHeader.Recipient.Value = Convert.ToString(dr["id"]);

That is because the initialization of a class like PathDetailsMessage will set no value to the MessageHeader property which will be null.这是因为像PathDetailsMessage这样的 class 的初始化不会为MessageHeader属性设置任何值,该属性将为 null。 The same applies to the Recipient property.这同样适用于Recipient属性。 So with the above initialization, you'll have some object to store the value.因此,通过上述初始化,您将拥有一些 object 来存储值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM