Run SSIS Package through winforms - 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;
}
}

Related

WinForms: DataGridView with E.F. does not show New Row

I have a simple WinForm with a DataGridView that shows record with Entity Framework.
My problem is that New Row feature appears only if the DataGridView is empty.
How is the problem?
Thank you in advance.
Luis
PS
Here some details of the WinForm class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GestioneFormazione;
namespace MpFormazione
{
public partial class MpFormazione : Form
{
public MpFormazione()
{
InitializeComponent();
this.dataGridView1.CellClick += DataGridView1_CellClick;
this.dataGridView1.AllowUserToAddRows = true;
this.dataGridView1.VirtualMode = true;
this.dataGridView1.AutoGenerateColumns = false;
}
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.ButtonColumunIndex)
{
if (e.RowIndex == 0)
{
Cliente cliente = new Cliente();
cliente.ID = Guid.NewGuid();
if (dataGridView1.Rows[0].Cells[1].Value != null)
cliente.Nome = dataGridView1.Rows[0].Cells[1].Value.ToString();
SaveToDb(cliente);
}
}
}
private void SaveToDb(Cliente cliente)
{
using (var context = new MPFORMAZIONEEntities())
{
context.Cliente.Add(cliente);
context.SaveChanges();
}
}
private void clienteBindingSource_CurrentChanged(object sender, EventArgs e)
{
}
private void ClienteBindingSource_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
{
}
private int ButtonColumunIndex
{
get
{
return 2;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void MpFormazione_Load(object sender, EventArgs e)
{
using (var context = new MPFORMAZIONEEntities())
{
this.dataGridView1.DataSource = context.Cliente.ToList<Cliente>();
}
}
}
}
The page has only this code, and the Entity Framework for "Cliente" and "Dipendente" entities.

I want to covert the datagridview into an ini file after inputting the data

I want to covert the datagridview into an ini file after inputting the data.
http://www.hoons.net/Board/qacshap/Content/67073
When I enter the URL above,
I try to put data into the grid and press the export button to save it as an .ini file in the form of section, key, value. What should I do? Inside the code, the content is created as an ini file, but not as a grid.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
namespace EXPORT
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
[DllImport('kernel32')]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport('kernel32')]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
static string path = 'C:\\Test.ini';
public Form1()
{
InitializeComponent();
dataGridView1.AllowUserToAddRows =true; //자동 행 추가
dataGridView1.AutoGenerateColumns = false;
}
private void button1_Click(object sender, EventArgs e)
{
WritePrivateProfileString('SECTION', 'KEY', 'VALUE', #'C:\ConnectionInfo.ini');
MessageBox.Show('EXPORT successfully to *.INI format');
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void WriteInFile(string section,string key,string value,string path)
{
WritePrivateProfileString(section, key, value, path);
if (value == null)
{
throw new ArgumentException();
}
}
private void button2_Click(object sender, EventArgs e) //ADD_ROW Button
{
DataGridViewButtonColumn button = new DataGridViewButtonColumn();
{
dataGridView1.Rows.Add();
}
}
}
}
You can use my MadMilkman.Ini library for this, here is how:
private void button1_Click(object sender, EventArgs e)
{
IniFile iniFile = new IniFile();
IniSection iniSection = null;
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
if (row.IsNewRow)
break;
string section = row.Cells[0].Value?.ToString();
string key = row.Cells[1].Value.ToString();
string value = row.Cells[2].Value.ToString();
if (!string.IsNullOrEmpty(section))
iniSection = iniFile.Sections.Add(section);
iniSection.Keys.Add(key, value);
}
iniFile.Save("C:\\Test.ini");
}
Also, here is how the generated "Test.ini" file looks like:
[a]
123=456
789=234
345=678
[b]
123=456
789=234
345=678

Unable to get rid of ambiguous path error

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

How do I export data from a datagridview/data set into an Access Database? [C#]

So my title is pretty self explanatory, so thus far I have read Microsofts Documentation and watched some Youtube videos regarding my issue here.
Firstly my project code here:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataRow r in dsEquipment.Tables[0].Rows)
{
DataRow dr = sQTDBDataSet.tblEquipment.NewRow();
dr[0] = r[0];
dr[1] = r[1];
dr[2] = r[2];
dr[3] = r[3];
dr[4] = r[4];
dr[5] = r[5];
dr[6] = r[6];
dr[7] = r[7];
dr[8] = r[8];
dr[9] = r[9];
dr[10] = r[10];
dr[11] = r[11];
dr[12] = r[12];
dr[13] = r[13];
dr[14] = r[14];
dr[15] = r[15];
dr[16] = r[16];
sQTDBDataSet.tblEquipment.Rows.Add(dr);
}
//tblEquipmentTableAdapter.Update(sQTDBDataSet.tblEquipment);
//tableAdapterManager.UpdateAll(sQTDBDataSet);
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.tblEquipmentBindingSource.EndEdit();
this.tblEquipmentTableAdapter.Update(this.sQTDBDataSet.tblEquipment);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
}
}
private void btnReadExcel_Click(object sender, EventArgs e)
{
OpenFileDialog OFD = new OpenFileDialog();
if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string strfilename = OFD.FileName;
txtFileName.Text = strfilename;
}
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void tblEquipmentBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.tblEquipmentBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.sQTDBDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'sQTDBDataSet.tblEquipment' table. You can move, or remove it, as needed.
this.tblEquipmentTableAdapter.Fill(this.sQTDBDataSet.tblEquipment);
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
try
{
// Establish connection between the c# application and the excel file.
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0");
// Reading the data from the excel file.
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Equipments$]", con);
// All data from the file will be loaded into the dataset.
da.Fill(dsEquipment);
// Show in a message box how many rows of data there is.
//MessageBox.Show(dsEquipment.Tables[0].Rows.Count.ToString());
// Show the data in the data grid view.
dgEquipment.DataSource = dsEquipment.Tables[0];
}
catch
{
MessageBox.Show("Please select the SQT2 Excel sheet.");
}
}
}
}
So my first attempt at tackling the problem was this:
//tblEquipmentTableAdapter.Update(sQTDBDataSet.tblEquipment);
//tableAdapterManager.UpdateAll(sQTDBDataSet);
I get no errors but for some reason my Access Database is not showing the updates.
My second attempt was the following:
private void btnOpenFile_Click(object sender, EventArgs e)
{
try
{
// Establish connection between the c# application and the excel file.
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + txtFileName.Text + ";Extended Properties=Excel 12.0");
// Reading the data from the excel file.
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Equipments$]", con);
// All data from the file will be loaded into the dataset.
da.Fill(dsEquipment);
// Show in a message box how many rows of data there is.
//MessageBox.Show(dsEquipment.Tables[0].Rows.Count.ToString());
// Show the data in the data grid view.
dgEquipment.DataSource = dsEquipment.Tables[0];
}
catch
{
MessageBox.Show("Please select the SQT2 Excel sheet.");
}
}
Which was a seperate button and I obtained this code from Microsofts documentation and changed the variables of the dataset and the database table, no errors... yet STILL my access database is not updating!
Completely stuck right now and any help is appreciated! :)
Thank you!

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.

Resources