In a STATIC corner

B

bgaddis

Am trying to adapt a pice of sample code in a proof of concept project
and can not seem to fugure out how to load the required values from a
database vs the static values in the sample.

Here is the Sample Class...
public class ModeItem : INotifyPropertyChanged
{
#region Creation

public static ModeItem[] GetModeItems()
{
// In a real app this would probably call into a data
access layer to get records from a database.
return new ModeItem[]
{
new ModeItem(1, "Item 1", "0"),
new ModeItem(2, "Item 2", "1"),
new ModeItem(3, "Item 3", "0"),
new ModeItem(4, "Item 4", "1"),
new ModeItem(5, "Item 5", "0"),
new ModeItem(6, "Item 6", "1"),
new ModeItem(7, "Item 7", "0"),
new ModeItem(8, "Item 8", "1"),
new ModeItem(9, "Item 9", "0"),
new ModeItem(10, "Item 10", "1"),
};
}

private ModeItem(int iItemID, string sItemName, string
sItemIcon)
{
this.ItemID = iItemID;
this.ItemName = sItemName;
this.ItemIcon = sItemIcon;
}

#endregion // Creation

#region Properties

public int ItemID { get; private set; }

string itemName;
public string ItemName
{
get { return itemName; }
set
{
if (value == itemName)
return;

itemName = value;

this.OnPropertyChanged("ItemName");
}
}

public string ItemIcon { get; private set; }

#endregion // Properties

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler =
this.PropertyChanged;
if (handler != null)
handler(this, new
PropertyChangedEventArgs(propertyName));
}

#endregion
}
}


And here is what I want to do,
public class CategoryItem : INotifyPropertyChanged
{
private int itemID;
private string itemDescription;
private int itemIcon;

#region Public Properties
public int ItemID
{
get { return this.itemID; }
set { this.itemID = value; }
}

public string ItemDescription
{
get { return this.itemDescription; }
set
{
if (value == this.itemDescription)
return;

this.itemDescription = value;

this.OnPropertyChanged("ItemDescription");
}
}

public int ItemIcon
{
get { return this.itemIcon; }
set { this.itemIcon = value; }
}
#endregion // Public Properties

#region Creation
/// <summary>
///
/// </summary>
/// <returns>Categories</returns>
public static CategoryItem[] GetCategoryItems(string
sqlConnectionString)
{
OleDbConnection sqlConnection = new
OleDbConnection(sqlConnectionString.ToString());
string sqlQueryString = String.Format(@"SELECT [recID],
[catName], [catIcon] " +
"FROM [categories] " +
"WHERE [catActive] = 'True'");

sqlConnection.Open();

OleDbCommand oleDbCommand = new
OleDbCommand(sqlQueryString.ToString(), sqlConnection);
OleDbDataReader oleDbDataReader =
oleDbCommand.ExecuteReader();

while (oleDbDataReader.Read())
{
new CategoryItem(
oleDbDataReader.GetInt32(0),
oleDbDataReader.GetString(1).ToString(),
oleDbDataReader.GetInt32(2));
}
if (sqlConnection != null) sqlConnection.Close();

return TheCategory;
}

private CategoryItem(int iItemID, string sItemDescription, int
iItemIcon)
{
this.ItemID = iItemID;
this.ItemDescription = sItemDescription;
this.ItemIcon = iItemIcon;
}
#endregion // Creation

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler =
this.PropertyChanged;
if (handler != null)
handler(this, new
PropertyChangedEventArgs(propertyName));
}

#endregion
}


Any help you can provide would be greatly appreciated.
 
R

raylopez99

Am trying to adapt a pice of sample code in a proof of concept project
and can not seem to fugure out how to load the required values from a
database vs the static values in the sample.
PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

Any help you can provide would be greatly appreciated.

Not quite sure what the problem is, but the biggest problem I had when
I studied databases was the connection: e.g., "OleDbConnection
sqlConnection = new OleDbConnection(sqlConnectionString.ToString()); "

You have to be sure your environment has security permissions that
allows you to work with SQL Server. It took me about a week of back
and forth with various newsgroups (the one that deals with SQL
specifically was very good, in particular a Scandanavian chap whose
name escapes me but who always is very helpful). I can't even tell
you how or what I did to make it finally connect, but it involved
switching and tweaking settings from the Administrator account (I was
using XP) and the user account, until finally I made the connection.
It was a several days project (a few hours every day) before I got it
to work.

As a last resort, I suggest this: just build a database, and manually
input the information exactly as the 'static values in the sample',
and then work from the newly created database. It's a pain to do the
manual input, but it's a last resort.

Good luck.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top