2

UPDATE: think i figured it out. Any extra advice is appreciated.

I've connected to my Database, but now I want to be able to use that data. I want to populate the ListView with the items(LargeIcon/SmallIcon views only) and then be able to click on the items using the SelectedItemsChanged to view them in textboxes and/or be able to update the values.

(ListView displays all data/items on start. User can choose an item. That item holds data. That data is displayed in appropriate controls.)

So i've created another class "UserInfo" to store them in. On my Main Form, I've set up the getter, but I am not sure about how to do the setter.

int row = 0;
public UserInfo userData
        {

            get
            {
                    string sqltest = data.Rows[row]["occupationId"].ToString();
                    decimal val;
                    decimal.TryParse(sqltest, out val);
                    UserInfo u = new UserInfo();
                    u.FirstName = data.Rows[row]["firstname"].ToString();
                    u.LastName = data.Rows[row]["lastname"].ToString();
                    u.UserName = data.Rows[row]["username"].ToString();
                    u.occupId = val;
                    u.ImageIndex = 0;

                return u;              
            }
            //Setter 
        }

Normally you'd have something like thisValue = value.Text I can't do data.Rows[row]["lastname"].ToString() = value.Lastname

Can i just use string f = data.Rows[row]["firstname"].ToString(); in the setter?

Help appreciated, will edit this as needed.

0

1 Answer 1

1

Be aware that the property is depending on the value of row. It's not a good practice that a property depends on other. Perhaps you can implment an indexed property:

public UserInfo UserData[int row] {
    get {
       return new UserInfo { 
          FirstName = data.Rows[row]["firstname"].ToString(),
          //Remaining fields
       }
    }
    set {
        data.Rows[row]["firstname"] = value.FirstName;
        //Remaining fildes
    }
}

Also I'm assuming that occupationId is stored as decimal in the database and it's not allow nulls. If it allows nulls, you may check like this:

decimal val = data.Rows[row]["occupationId"] == DBNull.Value ? 0.0m; (decimal)data.Rows[row]["occupationId"];

If you dont need and indexed property, the code can be as follow.

Be aware that DataRow fields are declared as object. You can assign (set) any value to object. In the getter, you must cast object to the concrete type

int row = 0;
public UserInfo userData
    {

        get
        {
                UserInfo u = new UserInfo();
                u.FirstName = data.Rows[row]["firstname"].ToString();
                u.LastName = data.Rows[row]["lastname"].ToString();
                u.UserName = data.Rows[row]["username"].ToString();
                u.occupId = (double)data.Rows[row]["occupationId"];
                u.ImageIndex = 0;

            return u;              
        }
        set
        {
                data.Rows[row]["firstname"] = value.FirstName;
                data.Rows[row]["lastname"] = value.LastName;
                data.Rows[row]["username"] = value.UserName;
                data.Rows[row]["occupationId"] = value.occupId;
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your answer. I am also having issues where I use a for loop to add the items, but since i am using row, it ends on 10, which then throws an outofindex error.
Use a foreach instead of a classic for. Or if ussing for, you can get the total rows to process form data.Rows.Count
when the items are added, it uses the userData. If I use say int i = 0 instead of row = 0 and then use the for loop, it will only ever use index 0 since I did not loop in my userData
Really, the getter / setter aproximation is not good. You have more than two operations. read->getter, update->setter, but you'll also need add and delete. For adding new records, you can implement an Add(UserData user) method
normally I would have controls like textboxes so its easier. With Database, im not sure. I would just populate it with (foreach row...), but then would I be able to use each item? It also didn't display well unless it was in Details view, which im not using. So putting it into an object, showing only the firstname, and then being able to click on it to display that items values is what i want. Right now i am just focusing on that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.