Click here to Skip to main content
15,562,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am using VS2015..
i have two structure as follows
<pre lang="C#">

 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;//struct array
    };


userprofile structure is read only once when the program runs. but metric struct value will be changed continually. how should i assign stuct array with in a sturct dynamically.. plz help me.

What I have tried:

<pre lang="C#">

<pre>  public bool ReadDBData(MySqlConnection connection, string usercode,string accode)
        {
            try
            {
                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);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    userdetails.metric[i].code = accode;
                    userdetails.metric[i].amount = Convert.ToDouble(dt.Rows[i]["Amount"].ToString());
                    //account.amount = Convert.ToDouble(table.Rows[i]["Amount"].ToString());
                    //account.amnt = Convert.ToString("{\"value\":" + account.amount + "}");
                    //userdetails.email = dt.Rows[i]["EMAIL"].ToString();
                    //userdetails.password = dt.Rows[i]["PASSWORD"].ToString();
                    //userdetails.group
                }
            }
            catch(Exception ex)
            {
                return false;
            }
            return true;
        } 
Posted
Updated 1-Feb-21 5:12am

For starters, not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

After that, I have no idea what problem you are having: Metric is s struct, which means it is a value type - when you declare an array of value types, that create and allocates the space needed for each of the elements for you, just as an array of integers does. And your code doesn't create the array of Metric values at all, unless the external userdetails variable has already created it, which would be weird since the loop since isn't known until that method is called.

But your SQL code ... it returns a single row. So what is the loop there for? Every time that method is called, it only puts data into the first element of the metric array, and I have no idea what else you expected it to do...


If I'm honest, that doesn't look like a design you spent a lot of time thinking about: it looks like you leapt straight into code without much design - and it needs design in order to be coherent!
 
Share this answer
 
Comments
Member 12453921 30-Jan-21 2:11am    
sir..userdetails structure is read one time when the application runs. so these data expect metric struct array is single time read and stored in structure. but the metric is changed for different users and one user may have more than one metric.. here in my code i just included the portion of metric. i dnt know how to assign the values to it.
Member 12453921 30-Jan-21 2:14am    
i put comment for those section.. because i used that first but it is not the proper way i think.. because every time the application runs after a particular interval the user details are read which makes more load. so i changed my code..
Try:
C#
public bool ReadDBData(MySqlConnection connection, string usercode, string accode)
{
    using (var 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))
    {
        command.Parameters.AddWithValue("@accode", accode);
        command.Parameters.AddWithValue("@usercode", usercode);
       
        var adapter = new MySqlDataAdapter(command);
        var dt = new DataTable();
        var adapter.Fill(dt);
       
        var metrics = new Metric[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            metrics[i] = new Metric
            {
                code = accode,
                amount = Convert.ToDouble(dt.Rows[i]["Amount"]),
            };
        }
        
        userdetails.metric = metrics;
        
        if (dt.Rows.Count != 0)
        {
            var row = dt.Rows[0];
            userdetails.email = row["EMAIL"].ToString();
            userdetails.password = row["PASSWORD"].ToString();
        }
    }
}
NB: Based on your column names, you appear to be storing your users' passwords in plain text. DO NOT do that.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900