Xoaks
Xoaks

Reputation: 53

Store SQL Database in an Object?

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.

Upvotes: 2

Views: 42

Answers (1)

mnieto
mnieto

Reputation: 3874

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;
        }
    }

Upvotes: 1

Related Questions