UI not updating with NotifyPropetyChanged for datatable - c#

I have two datatables "ResultData" and "OutputData". One of these receives results from an sql query, the other I am manually adding rows to.
ResultData gets the results from a query updates the UI appropriately. However OutputData does not. I'm going to link the entire method below. I've been stuck on this for a while, it seems to be caused due to it being async but I'm not sure how to fix it. Here is the constructor and method
class SqlInterfaceViewModel : INotifyPropertyChanged
{
private IDBQuery connection;
private string sql;
private DataTable resultData;
private DataTable outputData;
public ButtonViewModel Bvm;
private string resultStatus;
private bool queryRunning;
private string elapsedTime;
private int sqlCount;
public event PropertyChangedEventHandler PropertyChanged;
public SqlInterfaceViewModel(IDBQuery connection)
{
this.connection = connection;
outputData = new DataTable();
outputData.Columns.Add("DateTime", typeof(string));
outputData.Columns.Add("Action", typeof(string));
outputData.Columns.Add("Message", typeof(string));
outputData.Columns.Add("Duration", typeof(string));
Bvm = new ButtonViewModel(new ButtonModel(new Action(executeSql)));
OutputData.RowChanged += new DataRowChangeEventHandler(Row_Changed);
}
public bool QueryRunning
{
get { return queryRunning; }
set
{
if (value != this.queryRunning)
{
queryRunning = value;
OnPropertyChanged("QueryRunning");
}
}
}
public string Sql
{
get { return sql; }
set
{
if (sql != value)
sql = value;
OnPropertyChanged("Sql");
}
}
public DataTable ResultData
{
get { return resultData; }
set
{
if (resultData != value)
resultData = value;
OnPropertyChanged("ResultData");
}
}
public DataTable OutputData
{
get { return outputData; }
set
{
if (outputData != value)
outputData = value;
OnPropertyChanged("OutputData");
}
}
public string ResultStatus
{
get { return resultStatus; }
set
{
if (resultStatus != value)
resultStatus = value;
OnPropertyChanged("ResultStatus");
}
}
private void Row_Changed(object sender, DataRowChangeEventArgs e)
{
OnPropertyChanged("OutputData");
}
private List<string> sqlList(string sql)
{
List<string> SqlList = sql.Split(';').ToList();
return SqlList;
}
public async void executeSql()
{
foreach (string statement in sqlList(sql))
{
QueryRunning = true;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
DateTime dateTime = DateTime.Now;
try
{
sw.Start();
if (statement.ToUpper().Contains("SELECT"))
ResultData = await connection.GetResultSetTask(statement);
else
sqlCount = await connection.ExecuteUpdate(statement);
sw.Stop();
elapsedTime = sw.Elapsed.ToString(#"hh\:mm\:ss\.ffff");
if (statement.ToUpper().Contains("SELECT"))
OutputData.Rows.Add(dateTime.ToString(), statement, ResultData.Rows.Count.ToString() + " rows selected", elapsedTime);
else if (statement.ToUpper().Contains("UPDATE"))
OutputData.Rows.Add(dateTime.ToString(), statement, sqlCount.ToString() + " rows updated successfully", elapsedTime);
else if (statement.ToUpper().Contains("INSERT"))
OutputData.Rows.Add(dateTime.ToString(), statement, sqlCount.ToString() + " rows inserted successfully", elapsedTime);
else if (statement.ToUpper().Contains("DELETE"))
OutputData.Rows.Add(dateTime.ToString(), statement, sqlCount.ToString() + " rows deleted successfully", elapsedTime);
else
OutputData.Rows.Add(dateTime.ToString(), statement, sqlCount.ToString() + " records affected", elapsedTime);
}
catch (Exception ex)
{
OutputData.Rows.Add(dateTime.ToString(), sql, sqlCount.ToString() + " records affected", elapsedTime);
}
finally
{
QueryRunning = false;
}
}
}
public void OnPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(
this, new PropertyChangedEventArgs(propName));
}
}
I have double checked my DataTable properties as well as the binding in the xaml and both are setup the same.
The executeSql method is being called via ICommand
public class ButtonViewModel {
private bool canExecute;
private ICommand clickCommand;
private ButtonModel model;
public ICommand ClickCommand { get { return clickCommand ?? (clickCommand = new CommandHandler(() => ClickAction(), canExecute)); } }
public string ImageUriString { get { return UriStringFactory.GetUriString(model.ImageUriString); } }
public string HoverText { get { return model.HoverOverText; } }
public ButtonViewModel(ButtonModel model) {
canExecute = true;
this.model = model;
}
public void ClickAction() {
model.CommandAction();
}
}
internal class CommandHandler : ICommand {
public event EventHandler CanExecuteChanged;
private readonly Action action;
private readonly bool canExecute;
public CommandHandler(Action action, bool canExecute) {
this.action = action;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter) {
return canExecute;
}
public async void Execute(object parameter) {
await Task.Run(action);
}
}

You might have gotten this by now but: It looks as though executeSql does't set OutputData as it does with ResultData. The method only mutates the state of OutputData. In that case no event will be raised. You can simply raise the change event once you've modified OutputData and your ready to update the UI. Try adding this:
public async void executeSql()
{
/**
**/
finally
{
OnPropertyChanged("OutputData");
QueryRunning = false;
}
}
}
Note: you shouldn't have any issue with this command handler being async as the current context will have been captured during the await.

I solved the issue by using Application.Current.Dispatcher.BeginInvoke
ill post my solution below
class SqlInterfaceViewModel : ViewModelBase
{
private IDBQuery connection;
private string sql;
private DataTable resultData;
private DataTable outputData;
private string resultStatus;
private bool queryRunning;
private string elapsedTime;
private int sqlCount;
private string selectedText;
private bool cancelSql;
public ICommand CommandExecute { get { return new ButtonViewModel(new ButtonModel(new Action(executeSql))).ClickCommand; } }
public ICommand CommandCancel { get { return new ButtonViewModel(new ButtonModel(new Action(Cancel))).ClickCommand; } }
public ICommand CommandClear { get { return new ButtonViewModel(new ButtonModel(new Action(Clear))).ClickCommand; } }
public event PropertyChangedEventHandler PropertyChanged;
public SqlInterfaceViewModel(IDBQuery connection){
this.connection = connection;
outputData = new DataTable();
outputData.Columns.Add("DateTime", typeof(string));
outputData.Columns.Add("Action", typeof(string));
outputData.Columns.Add("Message", typeof(string));
outputData.Columns.Add("Duration", typeof(string));
cancelSql = false;
OutputData.RowChanged += new DataRowChangeEventHandler(Row_Changed);
}
#region public properties
public bool QueryRunning
{
get { return queryRunning; }
set
{
if (value != this.queryRunning)
{
queryRunning = value;
OnPropertyChanged("QueryRunning");
}
}
}
public string Sql
{
get { return sql; }
set
{
if (sql != value)
sql = value;
OnPropertyChanged("Sql");
}
}
public string SelectedText
{
get { return selectedText; }
set
{
if (selectedText != value)
selectedText = value;
}
}
public DataTable ResultData
{
get { return resultData; }
set
{
if (resultData != value)
resultData = value;
OnPropertyChanged("ResultData");
}
}
public DataTable OutputData
{
get { return outputData; }
set
{
if (outputData != value)
outputData = value;
OnPropertyChanged("OutputData");
}
}
public string ResultStatus
{
get { return resultStatus; }
set
{
if (resultStatus != value)
resultStatus = value;
OnPropertyChanged("ResultStatus");
}
}
private void Row_Changed(object sender, DataRowChangeEventArgs e)
{
OnPropertyChanged("OutputData");
}
#endregion
private List<string> sqlList()
{
List<string> SqlList;
if (string.IsNullOrEmpty(selectedText))
SqlList = sql.Split(';').ToList();
else
SqlList = selectedText.Split(';').ToList();
return SqlList;
}
public async Task FormatOutput(string statement, string dateTime, string error)
{
statement = statement.Trim().Trim(new char[] { '\r', '\n' });
string text = string.Empty;
if (string.IsNullOrEmpty(statement) == false)
{
string substring = statement.ToUpper().Substring(0, statement.IndexOf(' '));
if (string.IsNullOrEmpty(error) != true)
text = error;
else
switch (substring)
{
case ("SELECT"):
text = ResultData.Rows.Count.ToString() + " rows selected";
break;
case ("UPDATE"):
text = sqlCount + " rows updated";
break;
case ("INSERT"):
text = sqlCount + " rows inserted";
break;
case ("DELETE"):
text = sqlCount + " rows deleted";
break;
case ("DROP"):
text = "Table dropped";
break;
case ("CREATE"):
text = "Table created";
break;
}
}
await Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
OutputData.Rows.Add(new object[] { dateTime, statement, text, elapsedTime });}));
}
public async void executeSql()
{
QueryRunning = true;
foreach (string statement in sqlList())
{
if (cancelSql == true) { cancelSql = false; break; }
string error = string.Empty;
System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
DateTime dateTime = DateTime.Now;
if (string.IsNullOrEmpty(statement) == false)
{
try
{
if (statement.ToUpper().Substring(0, statement.IndexOf(' ')).Contains("SELECT"))
ResultData = await connection.GetResultSetTask(statement);
else
sqlCount = await connection.ExecuteUpdate(statement);
}
catch (Exception ex)
{
error = ex.Message;
}
finally
{
sw.Stop();
elapsedTime = sw.Elapsed.ToString(#"hh\:mm\:ss\.ffff");
await FormatOutput(statement, dateTime.ToString(), error);
cancelSql = false;
}
}
}
QueryRunning = false;
}
public void Cancel()
{
connection.cancelQuery();
cancelSql = true;
}
public void Clear()
{ Sql = string.Empty; }
}

Related

c#: PropertyChanged always null

Like the title says, I try to notify a property change, the method RaisePropertyChanged is called coorectly, but PropertyChanged is always null.
Here the shortened class:
public class BluetoothManager : INotifyPropertyChanged {
private string selectedBluetoothResult;
private List<string> foundDevices = new List<string>(5);
public List<DeviceInformation> penDevices = new List<DeviceInformation>();
private GattCharacteristic TxCharacteristic;
public string SelectedBluetoothResult {
get {
return selectedBluetoothResult;
}
set {
selectedBluetoothResult = value;
RaisePropertyChanged();
}
}
public List<string> FoundDevices {
get
{
return foundDevices;
}
set
{
foundDevices = value;
RaisePropertyChanged();
}
}
public BluetoothManager() {
StartScanWatcher();
}
public void StartScanWatcher() {
Debug.WriteLine("Starting device watcher...");
String query = "";
//query for Bluetooth LE devices
query += "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\"";
//query for devices with controllers' name
query += " AND (System.ItemNameDisplay:=\"" + DeviceName + "\" )";
var deviceWatcher = DeviceInformation.CreateWatcher(query); //, requestedProperties, DeviceInformationKind.AssociationEndpoint);
deviceWatcher.Added += DeviceWatcher_OnAdded;
deviceWatcher.EnumerationCompleted += DeviceWatcher_OnEnumComplete;
deviceWatcher.Removed += DeviceWatcher_Removed;
deviceWatcher.Stopped += DeviceWatcher_Stopped;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Start();
Debug.WriteLine(" StartScanWatcher end");
}
private void DeviceWatcher_OnAdded(DeviceWatcher sender, DeviceInformation deviceInfo) {
Debug.WriteLine(" DeviceWatcher_OnAdded Start");
lock (foundDevices) {
if (foundDevices.Contains(deviceInfo.Id)) {
return;
}
foundDevices.Add(deviceInfo.Id);
RaisePropertyChanged("FoundDevices");
}
Debug.WriteLine($"[{deviceInfo.Name}] DeviceWatcher_OnAdded...");
if (SelectedBluetoothResult == null)
{
SelectedBluetoothResult = deviceInfo.Id;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
Debug.WriteLine("<<<<<<<<<<<<< BluetoothManager, PropertyChanging: " + propertyName);
if (PropertyChanged == null) {
Debug.WriteLine("============ BluetoothManager, PropertyChanged == null, " + propertyName);
return;
}
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Debug.WriteLine(">>>>>>>>>>>>> BluetoothManager, PropertyChanged: " + propertyName);
}
}
}
And here the Binding in the XAML file:
<ListView ItemsSource="{Binding BluetoothManager.FoundDevices}" SelectedItem="{Binding BluetoothManager.SelectedBluetoothResult}">
<ListView.Resources>
<DataTemplate x:Key="BluetoothDeviceTemplate">
<TextBlock Text="{Binding Path=Sap}"/>
</DataTemplate>
</ListView.Resources>
</ListView>
The binding seems to work properly.
Please note, that the class in which the PropertyChanged is always null is not the DataContext auf the XAML file. Does that mean I have to work differently with the PropertyChange notification?
Thanks in advance.
EDIT:
The complete MainWindowViewModel:
public class MainWindowViewModel : INotifyPropertyChanged {
private ObservableCollection<BookGame> _booksToDisplay = new ObservableCollection<BookGame>();
private ObservableCollection<BookGame> _games = new ObservableCollection<BookGame>();
private string _url;
private int[] _newBooksMID;
private int[] _oldBooksMID;
private Dictionary<int, int> _newBooksVersionNumbers;
private Dictionary<int, int> _oldBooksVersionNumbers;
private Dictionary<int, int> _allVersionNumbers;
private List<BookGame> _booksToAdd;
private long _downloadSpeed;
private bool _isDownloading = true;
private bool _toggleLastSearchKeyWasReturn = false;
string _volumeLabel = "";
FileInfo[] _filesTxt = { };
FileInfo[] _filesAll = { };
private string _folderPath = "";
private string _driveName = null;
List<BookGame> _allGames;
List<BookGame> _allBooks;
List<MP3> _allMP3;
long lengthAllBooks = 0;
long lengthAllGames = 0;
int _percentDownloaded = 100;
private long _amountBytesToDownload;
private long _amountBytesDownloaded;
private long _amountMBytesToDownload;
private long _amountMBytesDownloaded;
private int _downloadTime;
//private bool _isDownloadAborted;
ServerCommi serverComm;
public BluetoothManager BluetoothManager { get; set; }
public MainWindowViewModel() {
DownloadTime = 0;
AmountBytesToDownload = 0;
AmountBytesDownloaded = 0;
DriveInfo drive = null;
foreach (DriveInfo driveInfo in DriveInfo.GetDrives()) {
if (driveInfo.IsReady && driveInfo.VolumeLabel == _volumeLabel) {
drive = driveInfo;
_driveName = drive.Name;
_folderPath = _driveName + _folderPath;
}
}
DirectoryInfo di = new DirectoryInfo(_folderPath);
if (di.Exists)
{
_filesTxt = di.GetFiles("*.txt");
FilesAll = di.GetFiles("*.*");
foreach (FileInfo file in _filesTxt)
{
try
{
Convert.ToInt32(file.Name.Split('_')[0]);
AddBookGameToList(file);
}
catch (Exception e)
{
}
}
}
SearchResults = new ObservableCollection<ResultItem>();
MenuCommand = new RelayCommand(o => {
Debug.WriteLine("Menu Command " + o);
SwitchBooks(o);
});
SearchReturnKeyCommand = new RelayCommand(o => {
Debug.WriteLine("00000000000000000000000000000000 SearchReturnKeyCommand " + o);
SearchActivated();
});
BrowserCommand = new RelayCommand(o => {
Debug.WriteLine("Browser Command main" + o);
CallBrowser("");
});
DeleteCommand = new RelayCommand(o => {
Debug.WriteLine("Delete Command main" + o);
});
ToggleDownloadsCommand = new RelayCommand(o => {
Debug.WriteLine(" |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ");
Debug.WriteLine(" |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ");
Debug.WriteLine("ToggleDownloadsCommand Command main" + o);
ToggleDownloads();
});
_viewModelMusic = new ViewModelMusic(_driveName);
AllGames = Games.ToList<BookGame>();
AllBooks = BooksToDisplay.ToList<BookGame>();
AllMP3 = _viewModelMusic.Mp3s.ToList<MP3>();
long lengthAllMP3 = 0;
foreach (MP3 mp3 in AllMP3) {
lengthAllMP3 += mp3.LengthValue;
}
_viewModelBooks = new ViewModelBooks(BooksToDisplay);
_viewModelGames = new ViewModelGames(Games);
_viewModelFiles = new ViewModelFiles(FilesAll);
_viewModelLumi = new ViewModelLumi(drive, lengthAllBooks, lengthAllGames, lengthAllMP3);
_viewModelOverview = new ViewModelOverview(AllBooks, AllGames, AllMP3);
_screens[0] = _viewModelOverview;
_screens[1] = _viewModelBooks;
_screens[2] = _viewModelGames;
_screens[3] = _viewModelMusic;
_screens[4] = _viewModelFiles;
_screens[5] = _viewModelVideos;
_screens[6] = _viewModelAdults;
_screens[7] = _viewModelLumi;
SearchText = "";
SelectedItem = _viewModelBooks;
Debug.WriteLine("CALLING ServerCommi! Ring, ring!");
serverComm = new ServerCommi(this);
//serverComm.DownloadBooksAsync();
BluetoothManager = new BluetoothManager();
}
private void ToggleDownloads() {
IsDownloading = !IsDownloading;
serverComm.ToggleDownloads(IsDownloading);
_viewModelBooks.ToggleDownloads(IsDownloading);
}
internal void DownloadStateChange(int mid, int newState) {
_viewModelBooks.DownloadStateChange(mid, newState);
}
// params bool[] isDownload : varargs
// returns the mid
public int AddBookGameToList(FileInfo file, bool isDownload = false) {
BookGame bg = new BookGame(file);
if (isDownload) {
bg.DownloadState = 2;
if (bg.Mid == serverComm.DownloadingMID) {
bg.DownloadState = 1;
}
}
if (bg.Group.StartsWith("B")) {
bg.Group = "Bücher";
}
switch (bg.Group) {
case "Bücher":
if (isDownload) {
BooksToDisplay.Insert(0, bg);
} else {
BooksToDisplay.Add(bg);
}
lengthAllBooks += bg.LengthValue;
break;
case "Spiele":
Games.Add(bg);
lengthAllGames += bg.LengthValue;
break;
default:
Debug.WriteLine("Default: " + bg.Title);
break;
}
return bg.Mid;
}
private void CallBrowser(string url) {
Debug.WriteLine("Url: " + Url);
try {
System.Diagnostics.Process.Start(Url);
} catch (System.ComponentModel.Win32Exception noBrowser) {
if (noBrowser.ErrorCode == -2147467259)
MessageBox.Show(noBrowser.Message);
} catch (System.Exception other) {
MessageBox.Show(other.Message);
}
}
string _searchText;
public string SearchText {
get {
return _searchText;
}
set {
Debug.WriteLine("SearchText Value= " + value);
if (!_toggleLastSearchKeyWasReturn) {
_searchText = value;
SearchResults.Clear();
List<ResultItem> _allBooksRI = new List<ResultItem>();
List<ResultItem> _allBooksHelperList = _allBooks.ToList<ResultItem>();
List<ResultItem> _allGamesHelperList = _allGames.ToList<ResultItem>();
List<ResultItem> _allMP3HelperList = _allMP3.ToList<ResultItem>();
if (SelectedItem != null && SelectedItem.Equals(_viewModelGames)) {
AddAllResultItemsIf(SearchResults, _allGamesHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allBooksHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allMP3HelperList, _searchText);
Debug.WriteLine("===================================== Games - " + SearchResults);
Debug.WriteLine("SelectedItem - " + SelectedItem);
} else if (SelectedItem != null && SelectedItem.Equals(_viewModelMusic)) {
AddAllResultItemsIf(SearchResults, _allMP3HelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allGamesHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allBooksHelperList, _searchText);
Debug.WriteLine("====================================== Music " + SearchResults);
Debug.WriteLine("SelectedItem - " + SelectedItem);
} else {
AddAllResultItemsIf(SearchResults, _allBooksHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allGamesHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allMP3HelperList, _searchText);
Debug.WriteLine("====================================== Books " + SearchResults);
}
if (SearchResults.Count == 0) {
SearchResults.Add(new ErrorResultItem("Error", "Nichts passendes gefunden."));
}
} else {
_toggleLastSearchKeyWasReturn = false;
}
}
}
private ObservableCollection<ResultItem> AddAllResultItemsIf(ObservableCollection<ResultItem> searchResults, List<ResultItem> toAdd, string searchText) {
foreach (ResultItem item in toAdd) {
if (item.Title.ToLower().Contains(_searchText.ToLower())) {
searchResults.Add(item);
}
}
return searchResults;
}
public ObservableCollection<ResultItem> SearchResults {
get; set;
}
ResultItem _selectedResult;
public ResultItem SelectedResult {
get {
return _selectedResult;
}
set {
_selectedResult = value;
SearchItemSelected(value);
}
}
private void SearchItemSelected(ResultItem value) {
switch (value.Group) {
case "Bücher":
SelectedItem = _viewModelBooks;
break;
case "Spiele":
SelectedItem = _viewModelGames;
break;
case "Musik":
SelectedItem = _viewModelMusic;
break;
default:
Debug.WriteLine("Search Item Selected, jumped to default: " + value);
break;
}
Unmark(Marked);
Mark(value);
}
ResultItem _marked;
internal void Mark(ResultItem value) {
Marked = value;
value.Marked = true;
}
internal void Unmark(ResultItem value) {
Marked = null;
if (value != null) {
value.Marked = false;
}
}
public ResultItem Marked {
get => _marked;
set => _marked = value;
}
private bool _isSearchResult;
public bool IsSearchResult {
get {
return _isSearchResult;
}
set {
_isSearchResult = value;
Debug.WriteLine("IsSearchResult= " + value);
RaisePropertyChanged();
}
}
private void SearchActivated() {
_toggleLastSearchKeyWasReturn = true;
SelectedItem = _viewModelOverview;
IsSearchResult = true;
}
private object _selectedItem;
public object SelectedItem {
get {
return _selectedItem;
}
set {
_selectedItem = value;
Debug.WriteLine("SELECTED_ITEM SETTER: " + value);
Unmark(Marked);
IsSearchResult = false;
if (SearchText != null) {
SearchText = SearchText;
}
RaisePropertyChanged();
}
}
ViewModelOverview _viewModelOverview;
ViewModelBooks _viewModelBooks;
ViewModelGames _viewModelGames;
ViewModelMusic _viewModelMusic;
ViewModelFiles _viewModelFiles;
ViewModelVideos _viewModelVideos = new ViewModelVideos();
ViewModelAdults _viewModelAdults = new ViewModelAdults();
ViewModelLumi _viewModelLumi;
object[] _screens = new object[8];
public object[] Screens {
get {
return _screens;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null) {
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public ICommand MenuCommand {
get; set;
}
public ICommand SearchReturnKeyCommand {
get; set;
}
public ICommand BrowserCommand {
get; set;
}
public ICommand ToggleDownloadsCommand {
get; set;
}
public RelayCommand DeleteCommand {
get;
private set;
}
public List<BookGame> AllGames {
get => _allGames;
set => _allGames = value;
}
public List<BookGame> AllBooks {
get => _allBooks;
set => _allBooks = value;
}
public List<MP3> AllMP3 {
get => _allMP3;
set => _allMP3 = value;
}
public ViewModelBooks ViewModelBooks {
get => _viewModelBooks;
set => _viewModelBooks = value;
}
public ObservableCollection<BookGame> BooksToDisplay {
get => _booksToDisplay;
set => _booksToDisplay = value;
}
public ObservableCollection<BookGame> Games {
get => _games;
set => _games = value;
}
public string Url {
get {
return _url;
}
set {
_url = value;
RaisePropertyChanged();
}
}
public int[] NewBooksMID {
get => _newBooksMID;
set => _newBooksMID = value;
}
public int[] OldBooksMID {
get => _oldBooksMID;
set => _oldBooksMID = value;
}
public Dictionary<int, int> NewBooksVersionNumbers {
get => _newBooksVersionNumbers;
set => _newBooksVersionNumbers = value;
}
public Dictionary<int, int> OldBooksVersionNumbers {
get => _oldBooksVersionNumbers;
set => _oldBooksVersionNumbers = value;
}
public Dictionary<int, int> AllVersionNumbers {
get => _allVersionNumbers;
set => _allVersionNumbers = value;
}
public int[] OldBooksMID1 {
get => _oldBooksMID;
set => _oldBooksMID = value;
}
public List<BookGame> BooksToAdd {
get => _booksToAdd;
set => _booksToAdd = value;
}
public FileInfo[] FilesAll {
get => _filesAll;
set => _filesAll = value;
}
public string FolderPath {
get => _folderPath;
set => _folderPath = value;
}
public int PercentDownloaded {
get {
return _percentDownloaded;
}
set {
_percentDownloaded = value;
RaisePropertyChanged();
}
}
public long DownloadSpeed {
get {
return _downloadSpeed;
}
set {
_downloadSpeed = value;
RaisePropertyChanged();
}
}
public long AmountBytesToDownload {
get {
return _amountBytesToDownload;
}
set {
_amountBytesToDownload = value;
Debug.WriteLine("Property Changed: " + "AmountBytesToDownload");
AmountMBytesToDownload = value / 1024 / 1024;
}
}
public long AmountBytesDownloaded {
get {
return _amountBytesDownloaded;
}
set {
_amountBytesDownloaded = value;
AmountMBytesDownloaded = value / 1024 / 1024;
}
}
public int DownloadTime {
get {
return _downloadTime;
}
set {
_downloadTime = value;
RaisePropertyChanged();
}
}
/*
public bool IsDownloadAborted {
get {
return _isDownloadAborted;
}
set {
_isDownloadAborted = value;
RaisePropertyChanged();
}
}
*/
public long AmountMBytesDownloaded {
get {
return _amountMBytesDownloaded;
}
set {
_amountMBytesDownloaded = value;
RaisePropertyChanged();
}
}
public long AmountMBytesToDownload {
get {
return _amountMBytesToDownload;
}
set {
_amountMBytesToDownload = value;
RaisePropertyChanged();
}
}
public bool IsDownloading {
get {
return _isDownloading;
}
set {
_isDownloading = value;
RaisePropertyChanged();
}
}
internal void SwitchBooks(object o) {
Debug.WriteLine("SwitchBooksEx " + o);
if (o.ToString().Equals("Tessloff.ViewModelBooks")) {
((ViewModelBooks)_screens[0]).SwitchView();
Debug.WriteLine("SwitchBooksIn " + o);
}
}
}
public class CommandViewModel {
private MainWindowViewModel _viewmodel;
public CommandViewModel(MainWindowViewModel viewmodel) {
_viewmodel = viewmodel;
Debug.WriteLine("LALALALALA");
MenuCommand = new RelayCommand(o => {
Debug.WriteLine("CommandViewModel " + o);
_viewmodel.SwitchBooks(o);
});
DeleteCommand = new RelayCommand(o => {
Debug.WriteLine("Delte Command CVM" + o);
});
}
public ICommand MenuCommand {
get; set;
}
public ICommand DeleteCommand {
get; set;
}
public string Title {
get;
private set;
}
}
public class RelayCommand : ICommand {
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null) {
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute) {
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter) {
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged {
add {
CommandManager.RequerySuggested += value;
}
remove {
CommandManager.RequerySuggested -= value;
}
}
public void Execute(object parameter) {
_execute(parameter);
}
#endregion // ICommand Members
}
public class ViewModelAdults {
public ViewModelAdults() {
Title = "Erwachsene";
ImgUrl = "/Resources/Erwachsene.png";
}
public string Title {
get; set;
}
public string ImgUrl {
get;
private set;
}
}
Edit tl;dr:
Why do all "direct" properties of MainWindowViewModel update great (like MainWindowViewModel.IsSearchResult), but the two "indirect" properties don't (MainWindowViewModel.BluetoothManager.SelectedBluetoothResul‌​t).
List.add() doesnt trigger PropertyChange. You need to use ObservableCollection or raise PropertyChange yourself after adding item.

C# How to load data to messagebox

I have:
private void Tab2KsiazkiBTSzczegoly_Click(object sender, EventArgs e)
{
string KodKsiazki;
KodKsiazki = DataWyszukajKsiazki.Rows[DataWyszukajKsiazki.CurrentCell.RowIndex].Cells[2].Value.ToString();
TSzczegolyDb _szczegoly = new TSzczegolyDb();
Global.listSzczegoly = _szczegoly.GetSZCZEGOLY(KodKsiazki);
//StringBuilder sb = new StringBuilder();
//foreach (DataGridViewCell cell in DataWyszukajKsiazki.SelectedCells)
//{
// sb.AppendLine(cell.Value.ToString());
//}
//MessageBox.Show(sb.ToString());
//}
MessageBox.Show(_szczegoly.ToString());
}
class like that:
public class TSzczegolyDb : Core.CoreMSSQL
{
static string connectionString = TconStrDb.GetConectionString();
public TSzczegolyDb()
: base(connectionString)
{
}
public List<TSzczegolyDto> GetSZCZEGOLY(string co)
{
List<TSzczegolyDto> list = null;
list = new List<TSzczegolyDto>();
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT Tytul, Autorzy, ISBN10, ISBN13, IlStron, Wydawnictwo, Gatunek, Opis FROM dbo.TKsiazki WHERE dbo.TKsiazki.KodKsiazki = '" + co + "'";
SqlDataReader reader = ExecuteQuery(command);
while (reader.Read())
{
TSzczegolyDto message = new TSzczegolyDto();
if (!reader.IsDBNull(0))
{
message.Tytuł = reader.GetString(0);
}
if (!reader.IsDBNull(1))
{
message.Autorzy = reader.GetString(1);
}
if (!reader.IsDBNull(2))
{
message.ISBN10 = reader.GetString(2);
}
if (!reader.IsDBNull(3))
{
message.ISBN13 = reader.GetString(3);
}
if (!reader.IsDBNull(4))
{
message.IlStron = reader.GetInt32(4);
}
if (!reader.IsDBNull(5))
{
message.Wydawnictwo = reader.GetString(5);
}
if (!reader.IsDBNull(6))
{
message.Gatunek = reader.GetString(6);
}
if (!reader.IsDBNull(7))
{
message.Opis = reader.GetString(7);
}
list.Add(message);
}
return list;
}
and second:
public class TSzczegolyDto
{
private string _tytul;
public string Tytuł
{
get { return _tytul; }
set { _tytul = value; }
}
private string _autorzy;
public string Autorzy
{
get { return _autorzy; }
set { _autorzy = value; }
}
private string _ISBN10;
public string ISBN10
{
get { return _ISBN10; }
set { _ISBN10 = value; }
}
private string _ISBN13;
public string ISBN13
{
get { return _ISBN13; }
set { _ISBN13 = value; }
}
private long _ilstron;
public long IlStron
{
get { return _ilstron; }
set { _ilstron = value; }
}
private string _wydawnictwo;
public string Wydawnictwo
{
get { return _wydawnictwo; }
set { _wydawnictwo = value; }
}
private string _gatunek;
public string Gatunek
{
get { return _gatunek; }
set { _gatunek = value; }
}
private string _opis;
public string Opis
{
get { return _opis; }
set { _opis = value; }
}
}
I want show _szczegoly on MessageBox but when I try to MessageBox.Show(_szczegoly.ToString()); then is wrong. In _szczegoly I have string and long type data.
How to create messagebox with this data?
I think you are trying to show an Object with a MessageBox, you need to override the ToString() method to show propertly:
class TSzczegolyDb
{
public override string ToString()
{
return this.Property1 + this.Property2 /*....*/;
}
}

Entity Framework 6.1: CRUD on Child objects

I populate a data grid with a list of objects that come from a repository like this:
public static IEnumerable<Order> GetOrdersForDataGrid()
{
IEnumerable<Order> query;
using (RSDContext = new RSDContext())
{
query = context.Orders.Include(o=>o.OrderDetails).ToList();
}
return query;
}
When I want to edit an order I pass the selected row to a new window like this:
OrderEditWindow orderEdit = new OrderEditWindow();
orderEdit.SelectedOrder = SelectedOrder;
orderEdit.ShowDialog();
Here I set the DataContext of the Window to:
DataContext = SelectedOrder;
In this window I have another data grid that binds to OrderDetails collection property of Order. The problem is on CRUD operations on OrderDetails. For example, after I add a new orderDetail like this:
private void AddProductDetailButton_OnClick(object sender, RoutedEventArgs e)
{
if (!ValidateProductDetail())
return;
var _selectedProduct = ProductAutoCompleteBox.SelectedItem as Product;
var selectedProduct = ProductsRepository.GetProductById(_selectedProduct.ProductId);
OrderDetail orderDetail = new OrderDetail();
orderDetail.Price = selectedProduct.Price;
orderDetail.ProductCode = selectedProduct.Code;
orderDetail.ProductName = selectedProduct.Name;
orderDetail.Quantity = int.Parse(QuantityNumericUpDown.Value.ToString());
orderDetail.Um = selectedProduct.Um;
orderDetail.Total = selectedProduct.Price * int.Parse(QuantityNumericUpDown.Value.ToString());
orderDetail.Group = selectedProduct.Subgroup.Group.Name;
orderDetail.Subgroup = selectedProduct.Subgroup.Name;
orderDetail.SupplierName = selectedProduct.Supplier.Name;
//orderDetail.Order=SelectedOrder;
//orderDetail.OrderId = SelectedOrder.OrderId;
SelectedOrder.OrderDetails.Add(orderDetail);
ProductAutoCompleteBox.Text = string.Empty;
QuantityNumericUpDown.Value = 1;
ProductAutoCompleteBox.Focus();
}
and then I call the update method from repository:
public static void UpdateOrder(Order order)
{
using (RSDContext context = new RSDContext())
{
context.Orders.Attach(order);
context.Entry(order).State = EntityState.Modified;
context.SaveChanges();
}
}
I get an error about OrderId. If i set manualy the navigation property and the id I don't get an error but changes dont get saved into db.
My Order model look like this:
public class Order : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Order()
{
_OrderDetails = new ObservableCollection<OrderDetail>();
_OrderDetails.CollectionChanged += _OrderDetails_CollectionChanged;
}
void _OrderDetails_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
AttachProductChangedEventHandler(e.NewItems.Cast<OrderDetail>());
if (e.OldItems != null)
CalcualteTotals();
}
[NotMapped]
public decimal CalculatedTotal
{
get
{
return OrderDetails.Sum(x => x.Total);
}
}
public int OrderId { get; set; }
private int _Number;
public int Number
{
get { return _Number; }
set
{
_Number = value;
NotifyPropertyChanged("Number");
}
}
private DateTime _Date;
public DateTime Date
{
get { return _Date; }
set
{
_Date = value;
NotifyPropertyChanged("Date");
}
}
private bool _Canceled;
public bool Canceled
{
get { return _Canceled; }
set
{
_Canceled = value;
NotifyPropertyChanged("Canceled");
}
}
private string _ClientName;
public string ClientName
{
get { return _ClientName; }
set
{
_ClientName = value;
NotifyPropertyChanged("ClientName");
}
}
private string _ClientPhone;
public string ClientPhone
{
get { return _ClientPhone; }
set
{
_ClientPhone = value;
NotifyPropertyChanged("ClientPhone");
}
}
private string _DeliveryAddress;
public string DeliveryAddress
{
get { return _DeliveryAddress; }
set
{
_DeliveryAddress = value;
NotifyPropertyChanged("DeliveryAddress");
}
}
private decimal _Transport;
public decimal Transport
{
get { return _Transport; }
set
{
_Transport = value;
NotifyPropertyChanged("Transport");
}
}
private decimal _Total;
public decimal Total
{
get { return _Total; }
set
{
_Total = value;
NotifyPropertyChanged("Total");
}
}
private ObservableCollection<OrderDetail> _OrderDetails;
public virtual ObservableCollection<OrderDetail> OrderDetails
{
//get { return _OrderDetails ?? (_OrderDetails = new ObservableCollection<OrderDetail>()); }
get
{
return _OrderDetails;
}
set
{
_OrderDetails = value;
NotifyPropertyChanged("OrderDetails");
}
}
private void AttachProductChangedEventHandler(IEnumerable<OrderDetail> orderDetails)
{
foreach (var p in orderDetails)
{
p.PropertyChanged += (sender, e) =>
{
switch (e.PropertyName)
{
case "Quantity":
case "Price":
case "Total":
CalcualteTotals();
break;
}
};
}
CalcualteTotals();
}
public void CalcualteTotals()
{
NotifyPropertyChanged("CalculatedTotal");
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
And my OrderDetail model look like this:
public class OrderDetail : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int OrderDetailId { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
private int _ProductCode;
public int ProductCode
{
get { return _ProductCode; }
set
{
_ProductCode = value;
NotifyPropertyChanged("ProductCode");
}
}
private string _ProductName;
public string ProductName
{
get { return _ProductName; }
set
{
_ProductName = value;
NotifyPropertyChanged("ProductName");
}
}
private string _Um;
public string Um
{
get { return _Um; }
set
{
_Um = value;
NotifyPropertyChanged("Um");
}
}
private decimal _Price;
public decimal Price
{
get { return _Price; }
set
{
_Price = value;
NotifyPropertyChanged("Price");
NotifyPropertyChanged("Total");
}
}
private int _Quantity;
public int Quantity
{
get { return _Quantity; }
set
{
_Quantity = value;
NotifyPropertyChanged("Quantity");
NotifyPropertyChanged("Total");
}
}
private string _SupplierName;
public string SupplierName
{
get { return _SupplierName; }
set
{
_SupplierName = value;
NotifyPropertyChanged("SupplierName");
}
}
private string _Subgroup;
public string Subgroup
{
get { return _Subgroup; }
set
{
_Subgroup = value;
NotifyPropertyChanged("Subgroup");
}
}
private string _Group;
public string Group
{
get { return _Group; }
set
{
_Group = value;
NotifyPropertyChanged("Group");
}
}
public decimal _Total;
public decimal Total
{
get { return Quantity * Price; }
set
{
_Total = value;
NotifyPropertyChanged("Total");
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
I'm really trying to use some sort of unit of work and I don't understand how i'm supposed to apply CRUD on objects with child collections and keep the UI updated in the same time (by working in a ObservableCollection and using Binding ClientPhone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged my parent window is updated as I type)
A final working solution:
using (RSDContext context = new RSDContext())
{
var details = order.OrderDetails;
order.OrderDetails = null;
List<int> OriginalOrderDetailsIds =
context.OrderDetails.Where(o => o.OrderId == order.OrderId).Select(o => o.OrderDetailId).ToList();
List<int> CurrentOrderDetailsIds = details.Select(o => o.OrderDetailId).ToList();
List<int> DeletedOrderDetailsIds = OriginalOrderDetailsIds.Except(CurrentOrderDetailsIds).ToList();
context.Entry(order).State = EntityState.Modified;
foreach (var deletedOrderDetailId in DeletedOrderDetailsIds)
{
context.Entry(context.OrderDetails.Single(o => o.OrderDetailId == deletedOrderDetailId)).State = EntityState.Deleted;
}
foreach (OrderDetail detail in details)
{
// Add.
if (detail.OrderDetailId == 0)
{
detail.OrderId = order.OrderId;
context.Entry(detail).State = EntityState.Added;
}
// Update.
else
{
context.Entry(detail).State = EntityState.Modified;
}
}
context.SaveChanges();
}
You could do this way for adding and updating the child, but not sure about deleted order details in the ui. If you don't want to get the order from entity, you need some kind of marking in the OrderDetail for deleted OrderDetail.
using (RSDContext context = new RSDContext())
{
var details = order.OrderDetails;
order.OrderDetails = null;
context.Entry(order).State = EntityState.Modified;
foreach (var detail in details)
{
if (detail.Id == 0)
{
// Adds.
detail.OrderId = order.Id;
context.Entry(detail).State = EntityState.Added;
}
else if (detail.IsDeleted)
// Adds new property called 'IsDeleted'
// and add [NotMapped] attribute
// then mark this property as true from the UI for deleted items.
{
// Deletes.
context.Entry(detail).State = EntityState.Deleted;
}
else
{
// Updates.
context.Entry(detail).State = EntityState.Modified;
}
}
order.OrderDetails = details;
context.SaveChanges();
}

Directory created by C# program thread is locked

I have this program that creates threads on which i must create queue folders and
check them for files.
Now I noticed my program failed after processing a huge number of files without problems.
I produces a UnauthorizedAccessException so I went looking for that folder and it appears
the folder has been locked out totally?!
Could this be my anti-virus blocking access or is it something I must fix on my thread?
public class worker
{
public bool Stopping = false;
private System.Timers.Timer _timer;
private List<string> _files;
#region Feedback
public event FeedbackHandler Feedback;
public delegate void FeedbackHandler(object sender, string text);
#endregion
#region Properties
private string _name;
public string Name
{
get { return _name; }
}
private string _folder;
public string Folder
{
get { return _folder; }
set { _folder = value; }
}
private string _outfolder = Path.Combine(shared.Root, "out");
public string Outfolder
{
get { return _outfolder; }
set { _outfolder = value; }
}
private string _backupfolder = Path.Combine(shared.Root, "backup");
public string Backupfolder
{
get { return _backupfolder; }
set { _backupfolder = value; }
}
private string _filter = "*.*";
public string Filter
{
get { return _filter; }
set { _filter = value; }
}
private SearchOption _subfolders = SearchOption.TopDirectoryOnly;
public bool Subfolders
{
get { return (_subfolders == SearchOption.AllDirectories); }
set { if (value) { _subfolders = SearchOption.AllDirectories; } else { _subfolders = SearchOption.TopDirectoryOnly; } }
}
#endregion
#region Constructor
public worker(string Name)
{
_name = Name;
_folder = Path.Combine(shared.Root, "queues");
_folder = Path.Combine(_folder, Name);
}
#endregion
#region Destructor
~worker()
{
}
#endregion
#region Control
public void Start()
{
Stopping = false;
Directory.CreateDirectory(_folder);
_timer = new System.Timers.Timer(1);
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
Feedback(this, "[" + _name + "] started!");
}
public void Stop()
{
Stopping = true;
Feedback(this, "[" + _name + "] stopped...");
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (Stopping)
{
_timer.Stop();
_files.Clear();
return;
}
_timer.Stop();
Process();
_timer.Start();
}
#endregion
void Process()
{
if (Directory.Exists(_folder))
{
_files = Directory.GetFiles(_folder, _filter, _subfolders).ToList();
foreach (string _file in _files.ToArray())
{
if (Stopping) { break; }
document _document = new document(_file);
_document.Copy(_backupfolder);
_document.Move(_outfolder);
}
_files = new List<string>();
}
}
}
public class document
{
private string _header;
#region Feedback
public event FeedbackHandler Feedback;
public delegate void FeedbackHandler(object sender, string text);
#endregion
#region Properties
private string _file;
public string File
{
get { return _file; }
}
private job _job;
public job Job
{
get { return _job; }
}
#endregion
#region Constructor
public document(string File)
{
_file = File;
_header = shared.FileOperations.ReadHeader(_file);
_job = new job(_file, _header);
_job.ReadHeader();
}
#endregion Constructor
public void Copy(string Folder)
{
string _backupfile;
_backupfile = Path.Combine(Folder,_job.Name);
_backupfile = Path.Combine(_backupfile,_job.Company);
_backupfile = Path.Combine(_backupfile, DateTime.Now.ToString("yyyy"));
_backupfile = Path.Combine(_backupfile, DateTime.Now.ToString("MMMM"));
Directory.CreateDirectory(_backupfile);
_backupfile = Path.Combine(_backupfile, Path.GetFileName(_file));
shared.FileOperations.CopyFile(_file, _backupfile, true);
}
public void Move(string Folder)
{
string _outfile;
_outfile = Path.Combine(Folder, Path.GetFileNameWithoutExtension(_file));
shared.FileOperations.MoveFile(_file, _outfile, true);
}
}
public struct shared
{
public static string Root
{
get
{
string _base = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
return Directory.GetParent(_base).Parent.FullName.ToString();
}
}
public struct Patterns
{
public const string Header = #"\^?JOB\s(?<JOB>[a-zA-Z0-9]+[0-9]{3})[D]?(?<ACTION>[JFE]+)(?<COMPANY>[A-Z]{2,2})\s" +
#"(?<EMAIL>-emto=.*)?" +
#"-C(?<COPIES>[0-9]{2,2})\s" +
#"-Z""(?<PRINTER>[A-Z0-9]+)""\s" +
#"(?:\^?PAGE 01|(?<FAX>\^?FAX.*)\s\^?PAGE 01?)";
public const string Jump = #"\^PAGE\s[0-9]+";
public const string Pages = #"(\$?PAGE\s)";
public const string Fax = #"\^?FAX FROM_COMPANY\s""(?<FROM>.*)""\s" +
#"\^?FAX FROM_FAX_NUM\s""(?<FFAX>.*)""\s" +
#"\^?FAX FROM_NAME\s""(?<FNAME>.*)""\s" +
#"\^?FAX TO_FAX_NUM\s""(?<TFAX>.*)""\s" +
#"\^?FAX TO_COMPANY\s""(?<TO>.*)""\s" +
#"\^?FAX TO_NAME\s""(?<TNAME>.*)""\s" +
#"\^?FAX WHO\s""(?<WHO>.*)""\s" +
#"\^?FAX ID\s+(?<ID>.*)";
public const string Mail = #"-em([^\s=]+)=(""[^""]*""|[^\s]+)";
public const string Seperator = #"^";
}
public struct FileOperations
{
// Encoding
public static Encoding ReadEncoding = Encoding.GetEncoding(1252);
public static Encoding WriteEncoding = Encoding.UTF8;
// Timeouts
static int Timeout = 1;
static int FileTimeout = 10000; // 10 seconds/file permitted..
// Header
public static string ReadHeader(string SourceFile)
{
return ReadHeader(SourceFile, Patterns.Jump);
}
public static string ReadHeader(string SourceFile, string Beacon)
{
WaitFile(SourceFile);
string r = null;
string l = null;
try
{
StreamReader _reader = new StreamReader(SourceFile, ReadEncoding);
Match _match;
do
{
l = _reader.ReadLine();
r += l + " ";
_match = Regex.Match(l, Beacon);
} while (!_match.Success);
_reader.Close();
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
return r;
}
// Read Contents
public static List<string> ReadFile(string SourceFile)
{
return ReadFile(SourceFile, Patterns.Seperator);
}
public static List<string> ReadFile(string SourceFile, string Seperator)
{
WaitFile(SourceFile);
List<string> lines = new List<string>();
try
{
StreamReader sr = new StreamReader(SourceFile, Encoding.GetEncoding(1250));
string tmp = null;
string line = null;
while (!sr.EndOfStream)
{
line = sr.ReadLine();
if (!string.IsNullOrEmpty(line) && line.Substring(0, 1) == Seperator)
{
if (!string.IsNullOrEmpty(tmp))
{
lines.Add(tmp);
}
tmp = line.Replace(Seperator, "^");
}
else
{
tmp += Environment.NewLine + line;
}
}
sr.Close();
if (!string.IsNullOrEmpty(tmp))
{
lines.Add(tmp);
}
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) {throw ex;}
}
return lines;
}
// Write Contents
public static void WriteFile(string DestinationFile, List<string> Lines)
{
try
{
File.WriteAllLines(DestinationFile, Lines.ToArray(), WriteEncoding);
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
}
public static void WriteFile(string DestinationFile, string Contents)
{
try
{
File.WriteAllText(DestinationFile, Contents);
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
}
// Move File
public static void MoveFile(string SourceFile, string DestinationFile, bool Overwrite)
{
WaitFile(SourceFile);
try
{
string _count = null;
string _destination = Path.GetDirectoryName(DestinationFile);
string _file = Path.GetFileNameWithoutExtension(DestinationFile);
string _extension = Path.GetExtension(DestinationFile);
string[] _files = Directory.GetFiles(_destination, _file + "*");
if (_files.Length > 0)
{
if (Overwrite)
{
for (int x = 0; x <= _files.Length - 1; x++)
{
File.Delete(_files[x]);
}
}
else
{
_count = "_" + (_files.Length - 1).ToString("D4");
}
}
DestinationFile = Path.Combine(_destination, _file + _count + _extension);
File.Move(SourceFile, DestinationFile);
}
catch (Exception ex)
{
if (Debugger.IsAttached) { throw ex; }
}
}
public static void CopyFile(string SourceFile, string DestinationFile, bool Overwrite)
{
WaitFile(SourceFile);
try
{
string _count = null;
string _destination = Path.GetDirectoryName(DestinationFile);
string _file = Path.GetFileNameWithoutExtension(DestinationFile);
string _extension = Path.GetExtension(DestinationFile);
string[] _files = Directory.GetFiles(_destination, _file + "*");
if (_files.Length > 0)
{
if (Overwrite)
{
for (int x = 0; x <= _files.Length - 1; x++)
{
File.Delete(_files[x]);
}
}
else
{
_count = "_" + (_files.Length - 1).ToString("D4");
}
}
DestinationFile = Path.Combine(_destination, _file + _count + _extension);
File.Copy(SourceFile, DestinationFile);
}
catch (Exception ex)
{
if (Debugger.IsAttached) { throw ex; }
}
}
// Delete File
public static void DeleteFile(string SourceFile)
{
WaitFile(SourceFile);
try
{
File.Delete(SourceFile);
}
catch (Exception ex)
{
// todo
if (Debugger.IsAttached) { throw ex; }
}
}
// Check File
static void WaitFile(string SourceFile)
{
Timeout = 1;
while (!File.Exists(SourceFile))
{
System.Threading.Thread.Sleep(Timeout);
Timeout++;
if (Timeout == FileTimeout)
{
// todo
if (Debugger.IsAttached) { throw new Exception("Timout exceeded!"); }
}
}
Timeout = 1;
while (!IsFileReady(SourceFile))
{
System.Threading.Thread.Sleep(Timeout);
Timeout++;
if (Timeout == FileTimeout)
{
// todo
if (Debugger.IsAttached) { throw new Exception("Timout exceeded!"); }
}
}
}
static bool IsFileReady(String SourceFile)
{
try
{
using (FileStream inputStream = File.Open(SourceFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
if (inputStream.Length > 0)
{
return true;
}
else
{
return false;
}
}
}
catch (Exception)
{
return false;
}
}
}
public struct Functions
{
public static string CleanXML(string Text)
{
Text = Text.Replace(#"&", #"&");
Text = Text.Replace(#"<", #"<");
Text = Text.Replace(#">", #">");
Text = Text.Replace(#"""", #""");
Text = Text.Replace(#"'", #"&apos;");
return Text;
}
}
}
void Work(string Name)
{
_worker = _workers.FirstOrDefault(w => w.Name == Name);
if (_worker == null)
{
_worker = new worker(Name);
_worker.Feedback+=new worker.FeedbackHandler(Feedback);
_worker.Folder = Path.Combine(_queuefolder, Name);
_worker.Outfolder = _outfolder;
_worker.Backupfolder = _backupfolder;
_workers.Add(_worker);
Thread _thread = new Thread(_worker.Start);
_thread.Start();
_thread.Join();
}
}
To clarify what i meant:
//worker class
private volatile bool _stopping;
private Thread _thread;
public void Start()
{
_stopping = false;
Directory.CreateDirectory(_folder);
_thread = new Thread(Process);
_thread.Start();
Feedback(this, "[" + _name + "] started!");
}
public void Stop()
{
_stopping = true;
_thread.Join();
Feedback(this, "[" + _name + "] stopped...");
}
private void Process()
{
while(!_stopping)
{
......
Thread.Sleep(100);
}
}
Because the way you are using timers... It's wrong. And while its interesting to know, why windows locks the folder, you should start from doing some refactoring. It might actually solve your problem along the way.

Display new form based on GridView data

I have a GridView, radGvA133s, on my main form, MainForm. I would like to be able to double-click on a row of the GridView and have that open up a new form, A133Form, to allow editing of the selected row.
Here is the double-click code:
private void radGvA133s_DoubleClick(object sender, EventArgs e)
{
A133 oA133 = (A133)A133BindingSource.CurrencyManager.List[A133BindingSource.CurrencyManager.Position];
A133Form oA133Form = new A133Form();
oA133Form.NewA133 = oA133;
oA133Form.IsNew = false;
oA133Form.ShowDialog(this);
//On return - if not cancelled, then continue
if (oA133Form.Cancelled != true)
{
this.radGvA133s.Refresh();
}
oA133Form.Dispose();
oA133Form = null;
}
Here is the A133Form code:
public partial class A133Form : Form
{
public A133Form()
{
InitializeComponent();
}
private bool _IsNew;
public bool IsNew
{
get
{
return _IsNew;
}
set
{
_IsNew = value;
}
}
private bool _Cancelled;
public bool Cancelled
{
get
{
return _Cancelled;
}
set
{
_Cancelled = value;
}
}
private A133 _newA133 = new A133();
public A133 NewA133
{
get
{
return _newA133;
}
set
{
_newA133 = value;
}
}
private void A133Form_Load(object sender, EventArgs e)
{
A133DB A133DB = new A133DB();
DataTable dtRSNs = A133DB.GetRSNList();
DataRow drFirstItem = dtRSNs.NewRow();
radComboRSN.DataSource = dtRSNs;
drFirstItem["rsn_id"] = "0";
drFirstItem["rsn_name"] = "";
dtRSNs.Rows.InsertAt(drFirstItem, 0);
radComboRSN.ValueMember = "rsn_id";
radComboRSN.DisplayMember = "rsn_name";
//Set databindings
radComboRSN.DataBindings.Add(new Binding("Text", NewA133, "RSN", true, DataSourceUpdateMode.OnPropertyChanged));
radTxtSubcontractor.DataBindings.Add(new Binding("Text", NewA133, "Subcontractor", true, DataSourceUpdateMode.OnPropertyChanged));
radMTxtCFDANumber.DataBindings.Add(new Binding("Text", NewA133, "CFDANumber", true, DataSourceUpdateMode.OnPropertyChanged));
radCbIncludeCFDA.DataBindings.Add(new Binding("Checked", NewA133, "IncludeCFDA", true, DataSourceUpdateMode.OnPropertyChanged));
radMTxtYear.DataBindings.Add(new Binding("Text", NewA133, "sYear", true, DataSourceUpdateMode.OnPropertyChanged));
radTxtFedAward.DataBindings.Add(new Binding("Text", NewA133, "FedAward", true, DataSourceUpdateMode.OnPropertyChanged));
radCbExceeds.DataBindings.Add(new Binding("Checked", NewA133, "Exceeds", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPDateMHDReceived.DataBindings.Add(new Binding("Value", NewA133, "DateMHDReceived", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPPeriodEnding.DataBindings.Add(new Binding("Value", NewA133, "PeriodEnding", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPDateAudited.DataBindings.Add(new Binding("Value", NewA133, "DateAudited", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPForwardDate.DataBindings.Add(new Binding("Value", NewA133, "ForwardDate", true, DataSourceUpdateMode.OnPropertyChanged));
radTxtSAOPerson.DataBindings.Add(new Binding("Text", NewA133, "SAOPerson", true, DataSourceUpdateMode.OnPropertyChanged));
}
private void radBtnCancel_Click(object sender, EventArgs e)
{
this.Cancelled = true;
this.Close();
}
private void radBtnSave_Click(object sender, EventArgs e)
{
this.Cancelled = false;
bool bValid = true;
foreach(Control control in this.Controls)
{
if (Convert.ToString(control.Tag) == "Required")
{
bool bMissingInfo = false;
if (control is RadDateTimePicker)
{
RadDateTimePicker dtp = control as RadDateTimePicker;
if (dtp.Value.ToString() == "1/1/0001 12:00:00 AM")
{
bMissingInfo = true;
}
}
else
{
if (string.IsNullOrEmpty(control.Text))
{
bMissingInfo = true;
}
}
if (bMissingInfo == true)
{
errorProvider1.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider1.SetError(control, "");
}
}
}
if (bValid == true)
{
bool bSaved = NewA133.SaveData();
if (bSaved == true)
{
this.Close();
}
else
{
MessageBox.Show("There was an error saving the data! If this continues, please contact technical assistance.",
"Error Saving Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("The information you have entered is incomplete. Please fill out all required fields.",
"Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
And, finally, here is the A133 Class code:
#region A133 Collection
public class A133Collection : BindingListView<A133>
{
public A133Collection() : base()
{
}
public A133Collection(List<A133> a133s) : base(a133s)
{
}
public A133Collection(DataTable dt)
{
foreach(DataRow oRow in dt.Rows)
{
A133 a = new A133(oRow);
this.Add(a);
}
}
private int FindMaxId()
{
int maxId = -1;
foreach(A133 a in this)
{
if (a.A133Id > maxId)
{
maxId = a.A133Id;
}
}
return maxId;
}
}
#endregion
#region A133 Class
///<summary>
///Class: A133
///Desc: Manages a single A133 business object
/// Note - 4 states for the object: Unchanged, Added, Deleted, Modified
/// Added flags that a brand new object was added; set prior to db insert
/// Deleted flags that the object was deleted; set after db delete
/// Unchanged is default state
/// Modified flags that props have changed
/// >> The IsDirty indicator looks to see if the object is "modified" or "added"
/// since these are pre-database flags
///</summary>
public class A133 : INotifyPropertyChanged, IEditableObject, IDataErrorInfo
{
//Declare internal class collection object
private A133Collection _A133s = new A133Collection();
//Declare internal class objects
private MHDFMS.BusinessLogic.A133DB _DB = new A133DB();
//Declare internal class props
private int _A133Id;
private string _RSN;
private string _Subcontractor;
private string _CFDANumber;
private string _IncludeCFDA;
private string _Year;
private string _FedAward;
private string _Exceeds;
private string _DateMHDReceived;
private string _PeriodEnding;
private string _DateAudited;
private string _ForwardDate;
private string _SAOPerson;
private int _OldA133Id;
private string _OldRSN;
private string _OldSubcontractor;
private string _OldCFDANumber;
private string _OldIncludeCFDA;
private string _OldYear;
private string _OldFedAward;
private string _OldExceeds;
private string _OldDateMHDReceived;
private string _OldPeriodEnding;
private string _OldDateAudited;
private string _OldForwardDate;
private string _OldSAOPerson;
private bool _Editing;
private string _Error = string.Empty;
private EntityStateEnum _EntityState;
private Hashtable _PropErrors = new Hashtable();
private void FirePropertyChangeNotification(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public A133()
{
this.EntityState = EntityStateEnum.Unchanged;
}
public A133(DataRow dr)
{
//Populates the business object item from a data row
this.A133Id = Convert.ToInt32(dr["a133_id"]);
this.RSN = dr["rsn"].ToString();
this.Subcontractor = dr["subcontractor"].ToString();
this.CFDANumber = dr["cfda_no"].ToString();
this.IncludeCFDA = dr["include_cfda"].ToString();
this.sYear = dr["year"].ToString();
this.FedAward = dr["fed_award"].ToString();
this.Exceeds = dr["exceeds"].ToString();
if (dr["date_mhd_received"] != null)
{
this.DateMHDReceived = Convert.ToDateTime(dr["date_mhd_received"]).ToShortDateString();
}
if (dr["period_ending"] != null)
{
this.PeriodEnding = Convert.ToDateTime(dr["period_ending"]).ToShortDateString();
}
if (dr["date_audited"] != null)
{
this.DateAudited = Convert.ToDateTime(dr["date_audited"]).ToShortDateString();
}
if (dr["forward_date"] != null)
{
this.ForwardDate = Convert.ToDateTime(dr["forward_date"]).ToShortDateString();
}
this.SAOPerson = dr["sao_person"].ToString();
this.EntityState = EntityStateEnum.Unchanged;
}
#region Public Methods/Constructors
public bool SaveData()
{
bool bSaved = false;
if (this.A133Id == 0)
{
bSaved = _DB.SaveA133Data(this);
}
else
{
bSaved = _DB.SaveA133Data(this);
}
if (bSaved == true)
{
this.EntityState = EntityStateEnum.Unchanged;
}
return bSaved;
}
public bool Delete()
{
bool bSaved = _DB.SaveA133AsInactive(this);
if (bSaved == true)
{
this.EntityState = EntityStateEnum.Deleted;
}
return bSaved;
}
public Int32 A133Id
{
get
{
return _A133Id;
}
set
{
_A133Id = value;
}
}
public string RSN
{
get
{
return _RSN;
}
set
{
_RSN = value;
FirePropertyChangeNotification("RSN");
}
}
public string Subcontractor
{
get
{
return _Subcontractor;
}
set
{
_Subcontractor = value;
FirePropertyChangeNotification("Subcontractor");
}
}
public string CFDANumber
{
get
{
return _CFDANumber;
}
set
{
_CFDANumber = value;
FirePropertyChangeNotification("CFDANumber");
}
}
public string IncludeCFDA
{
get
{
return _IncludeCFDA;
}
set
{
_IncludeCFDA = value;
FirePropertyChangeNotification("IncludeCFDA");
}
}
public string sYear
{
get
{
return _Year;
}
set
{
_Year = value;
FirePropertyChangeNotification("sYear");
}
}
public string FedAward
{
get
{
return _FedAward;
}
set
{
_FedAward = value;
FirePropertyChangeNotification("FedAward");
}
}
public string Exceeds
{
get
{
return _Exceeds;
}
set
{
_Exceeds = value;
FirePropertyChangeNotification("Exceeds");
}
}
public string DateMHDReceived
{
get
{
return _DateMHDReceived;
}
set
{
_DateMHDReceived = value;
FirePropertyChangeNotification("DateMHDReceived");
}
}
public string PeriodEnding
{
get
{
return _PeriodEnding;
}
set
{
_PeriodEnding = value;
FirePropertyChangeNotification("PeriodEnding");
}
}
public string DateAudited
{
get
{
return _DateAudited;
}
set
{
_DateAudited = value;
FirePropertyChangeNotification("DateAudited");
}
}
public string ForwardDate
{
get
{
return _ForwardDate;
}
set
{
_ForwardDate = value;
FirePropertyChangeNotification("ForwardDate");
}
}
public string SAOPerson
{
get
{
return _SAOPerson;
}
set
{
_SAOPerson = value;
FirePropertyChangeNotification("SAOPerson");
}
}
public Boolean IsDirty
{
get
{
return ((this.EntityState != EntityStateEnum.Unchanged) || (this.EntityState != EntityStateEnum.Deleted));
}
}
public enum EntityStateEnum
{
Unchanged,
Added,
Deleted,
Modified
}
public A133Collection A133s
{
get
{
return _A133s;
}
}
void IEditableObject.BeginEdit()
{
if (!_Editing)
{
_OldA133Id = _A133Id;
_OldRSN = _RSN;
_OldSubcontractor = _Subcontractor;
_OldCFDANumber = _CFDANumber;
_OldIncludeCFDA = _IncludeCFDA;
_OldYear = _Year;
_OldFedAward = _FedAward;
_OldExceeds = _Exceeds;
_OldDateMHDReceived = _DateMHDReceived;
_OldPeriodEnding = _PeriodEnding;
_OldDateAudited = _DateAudited;
_OldForwardDate = _ForwardDate;
_OldSAOPerson = _SAOPerson;
}
this.EntityState = EntityStateEnum.Modified;
_Editing = true;
}
void IEditableObject.CancelEdit()
{
if (_Editing)
{
_A133Id = _OldA133Id;
_RSN = _OldRSN;
_Subcontractor = _OldSubcontractor;
_CFDANumber = _OldCFDANumber;
_IncludeCFDA = _OldIncludeCFDA;
_Year = _OldYear;
_FedAward = _OldFedAward;
_Exceeds = _OldExceeds;
_DateMHDReceived = _OldDateMHDReceived;
_PeriodEnding = _OldPeriodEnding;
_DateAudited = _OldDateAudited;
_ForwardDate = _OldForwardDate;
_SAOPerson = _OldSAOPerson;
}
this.EntityState = EntityStateEnum.Unchanged;
_Editing = false;
}
void IEditableObject.EndEdit()
{
_Editing = false;
}
public EntityStateEnum EntityState
{
get
{
return _EntityState;
}
set
{
_EntityState = value;
}
}
string IDataErrorInfo.Error
{
get
{
return _Error;
}
}
string IDataErrorInfo.this[string columnName]
{
get
{
return (string)_PropErrors[columnName];
}
}
private void DataStateChanged(EntityStateEnum dataState, string propertyName)
{
//Raise the event
if (PropertyChanged != null && propertyName != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
//If the state is deleted, mark it as deleted
if (dataState == EntityStateEnum.Deleted)
{
this.EntityState = dataState;
}
if (this.EntityState == EntityStateEnum.Unchanged)
{
this.EntityState = dataState;
}
}
#endregion
}
#endregion
Unfortunately, when I double-click on the GridView, I receive this error: "InvalidCastException was unhandled. Unable to cast object of type 'System.Data.DataRowView' to type 'MHDFMS.BusinessLogic.A133'"
This error occurs at the very first line of the double-click event.
I am at a loss here and have been pulling my hair out for some time. Am I missing something obvious? Is there an easier (or better!) way to achieve my desired result?
Any help is greatly appreciated!
Try this:
DataRowView currentRow = A133BindingSource.CurrencyManager.List[A133BindingSource.CurrencyManager.Position] as DataRowView;
A133Form oA133Form = new A133Form();
oA133Form.NewA133 = new A133(currentRow.Row);
You forgot to post the event handler causing the exception... ;)
But the source of the problem seem quite obvious - you get a DataRowView object from the property grid and try to cast it (maybe by assigning to a variable) to A133 were you probably wanted new A133(DataRow dataRow).

Categories

Resources