How to assign values to struct array by calling a function whose return type is struct.

Mousmi21 1 Reputation point
2021-02-05T07:15:03.337+00:00

I have created windows application, and i use C# code. in this , call a readdbdata method whose return type is structure. and i want to assign this returned value to the structure array.

{

acccode = 120101000001; widget = 1;
            result = ebis.ReadData(conn, acccode);
            for (int i = 0; i < result.Count; i++)
            {
                UserProfile details = (UserProfile)result[i];               
                details.metric[i] = ebis.ReadDBData(conn, details.usercode, "120101000001");//here an error is showing "An unhandled exception of type 'System.NullReferenceException' occurred in eBis Application.exe
"

            }
}

//this are the code in main application. the following code is ReadDBData

 public Metric ReadDBData(MySqlConnection connection, string usercode, string accode)
        {

            int count = 0;
            ArrayList userDataList = new ArrayList();
            Metric[] usermetric;

            command = new MySqlCommand("SELECT SUM(d.amt) Amount, MAX(d.CREATED_DATE) createddate, MAX(d.MODIFIED_DATE) modifieddate FROM ddata d, branchuser b, userprofile p WHERE d.acccode = '" + accode + "' AND d.br = b.BRCODE  AND b.USERCODE = p.CODE AND cancel = 'No' and b.USERCODE='" + usercode + "' GROUP BY b.USERCODE;", connection);
            adapter = new MySqlDataAdapter(command);
            adapter.Fill(dt);
            int i;
            count = dt.Rows.Count;
            usermetric = new eBisExtension.Metric[count];
            for (i = 0; i < dt.Rows.Count; i++)
            {
                usermetric[i] = new Metric
                {
                    code = accode,
                    amount = Convert.ToDouble(dt.Rows[i]["Amount"].ToString()),
                    amountstring = Convert.ToString("{\"value\":" + dt.Rows[i]["Amount"].ToString() + "}")
                };
                return usermetric[i];
            }

        }

structures in my class

public struct Metric
    {
        public string code;
        public double amount;
        public string amountstring;
    };

public struct UserProfile
    {
        public string usercode;
        public string name;
        public string group;
        public string password;
        public string email;
        public Metric[] metric;
    };

Plz help me..i wanted to complete this project as early as possible.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ken Tucker 5,861 Reputation points
    2021-02-05T09:01:25.803+00:00

    An unhandled exception of type 'System.NullReferenceException' occurred in eBis Application.exe means you are trying to access variable that does not have something assigned to it.

    I would put a break point on the line of code giving you an error and make sure details is not null. If it is not I would step into your ReadDBData function and see where you get the error. Maybe you are not returning any records from the query


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.