Unable to get rid of ambiguous path error - wpf

I've had a look at this post but still can't seem to fix this problem: Resolving an ambiguous reference
I'm trying to make a button in WPF that when clicked browses for an excel file and then opens it. However I keep getting the error
'Path' is an ambiguous reference between 'system.windows.shapes.path'
and 'system.io.path'
Below is my code. I've tried explicity using
System.IO.Path.GetFullPath(fileDialog.FileName)
Below is my full code:
using Microsoft.Win32;
using System.Windows;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
namespace Fasetto
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new WindowViewModel(this);
}
private void WindowCloseButton_Click(object sender, RoutedEventArgs e)
{
SystemCommands.CloseWindow(this);
}
private void WindowMinimizeButton_Click(object sender, RoutedEventArgs e)
{
SystemCommands.MinimizeWindow(this);
}
private void WindowMaximizeButton_Click_1(object sender, RoutedEventArgs e)
{
SystemCommands.MaximizeWindow(this);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
FileDialog fileDialog = new OpenFileDialog();
fileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
fileDialog.ShowDialog();
string location = System.IO.Path.GetFullPath(fileDialog.FileName);
}
}
public class Read_From_Excel
{
public void getExcelFile(string filename)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filename);
}
}
This is the line that fires the error, with the variable 'filename' underlined in red:
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filename);

Related

populating a listbox from another window, but on second attempt the list is replaced instead of added

I'm building a task list application.
From my main window, I click on the add button. The program generates a new Window,and I compete the form and close it with the complete button.
My listbox in the main window has been populated with the tasks that I have entered.
The problem is, when I do this again, the listbox items are replaced with new items instead of the ones being added.
MainWindow.cs
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
List<Task> allTasks = new List<Task>();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void addTaskBtn_Click(object sender, RoutedEventArgs e)
{
NewTaskWindow newTaskWindow = new NewTaskWindow();
newTaskWindow.Owner = this;
newTaskWindow.Show();
}
private void editTaskBtn_Click(object sender, RoutedEventArgs e)
{
}
private void searchBtn_Click(object sender, RoutedEventArgs e)
{
}
private void AddUserBtn_Click(object sender, RoutedEventArgs e)
{
}
private void markCompleteButton_Click(object sender, RoutedEventArgs e)
{
}
private void deleteTaskBtn_Click(object sender, RoutedEventArgs e)
{
}
}
NewTaskWindow.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
namespace Task_Managment
{
public partial class NewTaskWindow : Window
{
Task newTask = new Task();
public NewTaskWindow()
{
InitializeComponent();
}
ObservableCollection<Task> AllTasks = new ObservableCollection<Task>();
ObservableCollection<Task> taskList = new ObservableCollection<Task>();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] taskType = new string[3];
taskType[0] = "Home";
taskType[1] = "College";
taskType[2] = "Work";
CataCombo.ItemsSource = taskType;
}
public void completeBtn_Click(object sender, RoutedEventArgs e)
{
List<Task> allTasks = new List<Task>();
newTask = new Task
{
Title = titletxBx.Text,
Description = DesctxBx.Text,
Priority = prioritytxBx.Text,
Catagory = CataCombo.Text,
taskDate = calander.SelectedDate.Value
};
taskList.Add(newTask);
MainWindow main = Owner as MainWindow;
main.taskListBox.ItemsSource = taskList;
titletxBx.Clear();
prioritytxBx.Clear();
DesctxBx.Clear();
responsibilitytxBx.Clear();
}
private void finishBtn_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
You need to save the TaskList somewhere, make the tasklist definition public to get acces from the main window and pass it from the main window when you load the new form:
observableCollection<Task> AllTasks = new ObservableCollection<Task>();
public ObservableCollection<Task> taskList = new ObservableCollection<Task>();
public NewTaskWindow(ObservableCollection<Task> taskList)
{
InitializeComponent();
this.tasklist = tasklist
}
After that, you only need to retrieve and send it from the main to the new window
private void addTaskBtn_Click(object sender, RoutedEventArgs e)
{
NewTaskWindow newTaskWindow = new NewTaskWindow(tasklist);
newTaskWindow.Owner = this;
newTaskWindow.Show();
tasklist = newTaskWindow.tasklist;
}
In the main window you must initialize it for the first run if not you will get an error:
ObservableCollection<Task> taskList = new ObservableCollection<Task>();
public MainWindow()
{
InitializeComponent();
}
....
Obviously this only save for the time you have the program open, when you close the program it will lose all the info. So if you are interested in that also, save in a file and load from it.

Click on Start -> Microsoft word 2010 attaches a new blank document to existing instance

I am automating MSWord in a WPF application. Everything working fine, but
a Click on Start -> Microsoft word 2010 attaches a new blank document to my instance which is already created by the Wpf application.How restrict this behaviour ?
public partial class MainWindow : System.Windows.Window
{
Word.Application _oApp;
Word.Document _oDoc;
object oMissing = System.Reflection.Missing.Value; // Missing Value
object oTrue = true;
object oFalse = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Create_Click(object sender, RoutedEventArgs e)
{
_oApp = new Word.Application();
_oApp.Visible = true;
_oApp.ShowWindowsInTaskbar = false;
((Word.ApplicationEvents4_Event)_oApp).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
}
private void btn_AddDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc = _oApp.Documents.Add(oMissing, oMissing, oMissing);
}
private void btn_RemoveDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc.Close(oFalse, oMissing, oMissing);
}
private void Application_NewDocument(Word.Document doc)
{
MessageBox.Show("New: " + _oApp.ActiveDocument.Name);
}
}
Microsoft listed this as a bug and illustrated a workaround to this problem in the following KB article
BUG: Starting Word Manually Uses Same Instance as Automation -
https://support.microsoft.com/en-us/kb/188546
using System.Windows;
using Word = Microsoft.Office.Interop.Word;
namespace WordAutomationTestApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
Word.Application _oApp;
Word.Document _oDoc;
object oMissing = System.Reflection.Missing.Value; // Missing Value
object oTrue = true;
object oFalse = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Create_Click(object sender, RoutedEventArgs e)
{
//To restrict the automation instance sharing with the user's documents.
//Create a temporary app instance and quit the same after the automation instance is created.
//Ref :: Go through the following work around.
//https://support.microsoft.com/en-us/kb/188546
Word.Application temp = new Word.Application(); //Create temporary instance.
_oApp = new Word.Application(); //Create automation instance.
temp.Quit(oFalse,oMissing,oMissing); //Close the temporary instance.
temp = null;
_oApp.Visible = true;
_oApp.ShowWindowsInTaskbar = false;
((Word.ApplicationEvents4_Event)_oApp).NewDocument += new Word.ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument);
}
private void btn_AddDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc = _oApp.Documents.Add(oMissing, oMissing, oMissing);
}
private void btn_RemoveDoc_Click(object sender, RoutedEventArgs e)
{
_oDoc.Close(oFalse, oMissing, oMissing);
}
private void Application_NewDocument(Word.Document doc)
{
MessageBox.Show("New: " + _oApp.ActiveDocument.Name);
}
}
}
My sincere thanks to #Cindy Meister , for his help to solve this problem.

mjpeg streaming in wpf not working

I develop a WPF app for mjpg streaming. I include the code here
public partial class MainWindow : Window
{
MjpegDecoder _mjpeg;
public MainWindow()
{
InitializeComponent();
_mjpeg = new MjpegDecoder();
_mjpeg.FrameReady += _mjpeg_FrameReady;
}
void _mjpeg_FrameReady(object sender, FrameReadyEventArgs e)
{
// What to write to get BitmapImage
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
_mjpeg.ParseStream(new Uri("http://155.41.145.37/mjpg/video.mjpg"));
}
}
What I need to write to get the bitmap from the frameReadyEventArgs and how to assign that bitmap to a WPF Image control
I've never used MJPEG Decoder library but if you go to their WPF example you'll find this:
private void mjpeg_FrameReady(object sender, FrameReadyEventArgs e)
{
image.Source = e.BitmapImage;
}
FrameReadyEventArgs should already have BitmapImage
public class FrameReadyEventArgs : EventArgs
{
...
public BitmapImage BitmapImage;
}

Devexpress: An object reference is required for the non-static field

I keep getting this error in my devexpress code.
Here is the code I have put in:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Drawing.Printing;
namespace DD
{
public partial class MasterReport : DevExpress.XtraReports.UI.XtraReport
{
//detailReport.catName.Value = ((DataRowView)e.Brick.Value).Row["EcoYear"].ToString();
public MasterReport()
{
InitializeComponent();
}
private void xrLabel1_BeforePrint(object sender, PrintEventArgs e)
{
// XRLabel l = sender as XRLabel;
// l.Tag = GetCurrentRow();
((XRLabel)sender).Tag = GetCurrentRow();
}
private void xrLabel1_PreviewClick(object sender, PreviewMouseEventArgs e)
{
DetailReport detailReport = new DetailReport();
detailReport.CaID.Value = (int)((DataRowView)e.Brick.Value).Row["CaseID"];
detailReport.EYear.Value = (int)((DataRowView)e.Brick.Value).Row["EcoYear"];
detailReport.ShowPreviewDialog();
}
private void xrLabel1_PreviewMouseMove(object sender, PreviewMouseEventArgs e)
{
Cursor.Current = Cursors.Hand;
}
private void xrPictureBox1_BeforePrint(object sender, PrintEventArgs e)
{
((XRLabel)sender).Tag = GetCurrentRow();
}
private void xrPictureBox1_PreviewClick(object sender, PreviewMouseEventArgs e)
{
PW pw = new PW();
PW.CaID.Value = (int)((DataRowView)e.Brick.Value).Row["CaseCaseID"];
PW.ShowPreviewDialog();
}
private void xrPictureBox1_PreviewMouseMove(object sender, PreviewMouseEventArgs e)
{
Cursor.Current = Cursors.Hand;
}
}
}
The PW.CaID.Value and the PW.ShowPreviewDialog(); are the lines that are giving me this error. Perhaps I am doing this the wrong way. What I wanted to do is click an icon (xrPictureBox1) on the main report and have it bring up the subreport (PW). I have done this in the code above for another subreport (detailreport). Thank you in advance for your help.
Your code should read:
PW pw = new PW();
pw.CaID.Value = (int)((DataRowView)e.Brick.Value).Row["CaseCaseID"];
pw.ShowPreviewDialog();
Note the case on PW/pw.

Run SSIS Package through winforms

I need to run a SSIS package through winforms.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Deployment;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net.Mime;
public string sPackage = String.Empty;
public string sConfig = String.Empty;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open Package";
fDialog.Filter = "SSIS Package (*.dts, *.dtsx)|*.dts;*.dtsx";
fDialog.InitialDirectory = #"C:\";
sPackage = fDialog.FileName.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = "Open Package";
fDialog.Filter = "SSIS Package (*.dts, *.dtsx)|*.dts;*.dtsx";
fDialog.InitialDirectory = #"C:\";
sConfig = fDialog.FileName.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
Microsoft.SqlServer.Dts.Runtime.Wrapper.Application app = new Microsoft.SqlServer.Dts.Runtime.Wrapper.Application();
Package package = app.LoadPackage(sPackage,false,null);
package.ImportConfigurationFile(sConfig);
DTSExecResult result = package.Execute();
MessageBox.Show(result.ToString());
}
But this is giving me error at LoadPackage(sPackage,false,null)
Cannot implicitly convert type 'Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSPackage90' to 'Microsoft.SqlServer.Dts.Runtime.Wrapper.Package'. An explicit conversion exists (are you missing a cast?)
I figured out that button3_click should be handled like this
private void button3_Click(object sender, EventArgs e)
{
MyEventListener eventListener = new MyEventListener();
Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
Microsoft.SqlServer.Dts.Runtime.Package pkg = app.LoadPackage(sPackage, eventListener,false);
Microsoft.SqlServer.Dts.Runtime.DTSExecResult pkgResults = pkg.Execute(null, null, eventListener, null, null);
MessageBox.Show(pkgResults.ToString());
}
class MyEventListener : DefaultEvents
{
public override bool OnError(DtsObject source, int errorCode, string subComponent,
string description, string helpFile, int helpContext, string idofInterfaceWithError)
{
// Add application-specific diagnostics here.
MessageBox.Show("Error in " + "/t" + source + "/t" + subComponent + "/t" + description);
return false;
}
}

Resources