i am developping a really simple program in c# with Visual studio, it consists simply by extracting data from an Excel sheet and transfer it to a database (only one table ), using MySql it worked i just saved the files as CSV and then imported them into my database but i had a big problem which was the encoding UTF8 i have many french characters ans arabic ones that just showed as random characters, so i went back to OleDb but it keeps shong exceptions now i am stuck with this one: System.Data.OleDb.OleDbException (0x80004005)
i ve tried to change running config to x86 but nothing (notice that i am using a windows 8 X64). its a cummon ISAM problem.
please help me .
this is my code :
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 Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
using System.Data.OleDb;
namespace testoledb
{
public partial class Form1 : Form
{
DataSet OleDs = new DataSet();
OleDbDataAdapter OleAdapter = new OleDbDataAdapter();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void upload_excl_Click(object sender, EventArgs e)
{
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "AGENDA.xlsx");
string OleConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+#";Extend Properties=Excel 12.0 Macro;MDR=Yes;ImportMixedTypes=Text;TypeGuessRows=0"; //,HDR=Yes;IMEX=1""";
OleDbConnection OleConn = new OleDbConnection(OleConnectionString);
string OleStrCmd = "select * from [SHeet1$A1:H330]";
OleDbCommand OleCmd = new OleDbCommand(OleStrCmd, OleConn);
try
{
OleConn.Open();
OleDs.Clear();
OleAdapter.SelectCommand = OleCmd;
OleAdapter.Fill(OleDs);
dataGridView1.DataSource = OleDs.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
OleConn.Close();
}
}
}
}
Related
I have a GUI designed on Visual Studio using C#. I am a beginner in C# but good at C++ programming but due to requirements of task, I am designing it in C#. In this GUI, i have a button that connects to remote ssh server and as a trial, i have following commands to run when user presses button1.
client.Connect();
var output = client.RunCommand("echo happy test");
var dltOutput = client.RunCommand("rm /home/helloWorld.txt");
var launchFirst = client.RunCommand("bash /root/first.sh");
client.Disconnect();
Console.WriteLine(output.Result);
The command to delete "helloWorld.txt" in my home folder runs fine but could not running the command to run the shell script "first.sh". I would like to attach complete code below for your reference.
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 Renci.SshNet;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
//Connection information
string user = "root";
string pass = "hello123ado";
string host = "192.168.38.50";
int port = 22;
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Console.WriteLine("Say Button1_Clicked");
using (var client = new SshClient(host,user,pass))
{
client.Connect();
var output = client.RunCommand("echo happy test");
var dltOutput = client.RunCommand("rm /home/helloWorld.txt");
var launchFirst = client.RunCommand("bash /root/first.sh");
client.Disconnect();
Console.WriteLine(output.Result);
}
}
private void Button2_Click(object sender, EventArgs e)
{
Console.WriteLine("Say Button2_Clicked");
}
private void Button3_Click(object sender, EventArgs e)
{
Console.WriteLine("Say Button3_Clicked");
}
}
}
Ribbon1.cs using Ribbon Designer (ie, no XML)
I am trying to clear the contents of a Drop Down list before repopulating the list with new data.
I have tried using Invalidate() in several places but I can't get it to work.
The flow of the Addin is as followed:
Copy text to clipboard -> works
Click Search -> works
Get new Data from Database using text from Clipboard -> works
Clear Dropdown -> doesn't work
Populate Drop Down with data. -> Does work but add items to drop down instead of
clearing it first
Thanks in advance
using System;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Tools.Ribbon;
namespace MSTEST
{
public partial class Ribbon1 : Office.IRibbonExtensibility
{
private Office.IRibbonUI ribbon;
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
this.ribbon = RibbonUI;
}
private void eventDB(object sender, RibbonControlEventArgs e)
{
RibbonUI.Invalidate();
//this.ribbon.InvalidateControl("resultsDB");
string getTextFromClipboard = Clipboard.GetText();
string queryString = "select distinct file_path as FP, case_id as CS, date_added from documents CONTAINS(documents.file_path, '" + getTextFromClipboard + "') group by case_id,file_path, datE_added order by Date_Added DESC";
using (OdbcConnection odbcConnection = new OdbcConnection("dsn=Needles;UID=dba;PWD=sql;"))
{
OdbcCommand command = new OdbcCommand(queryString, odbcConnection);
try
{
odbcConnection.Open();
OdbcDataReader reader = command.ExecuteReader();
// ribbon.InvalidateControl("resultsDB");
int i = 0;
while (reader.Read())
{
// Being DropDown Populate
RibbonDropDownItem item = this.Factory.CreateRibbonDropDownItem();
item.Label = reader["FP"].ToString();
resultsDB.Items.Add(item);
//MessageBox.Show(reader["CS"].ToString());
// End DropDown Populate
i = i + 1;
}
reader.Close();
odbcConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
public string GetCustomUI(string RibbonID)
{
throw new NotImplementedException();
}
}
}
I have solved this by tinkering around with the Clear() function of the object. example: resultsDB.Items.Clear();
I am using ExcelDataReader here.
I don't know why below code doesn't work.
It doesn't generate any error but it still doesn't show excel data.
It simply doesn't do anything after loading excel file.
I am new to C# and after 5 hours of digging I feel frustrated since I don't know why this doesn't work.
using Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class ExcelForm : Form
{
public ExcelForm()
{
InitializeComponent();
}
private void ExcelForm_Load(object sender, EventArgs e)
{
}
DataSet result;
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel(.xls)|*.xls|Excel(.xlsx)|*.xlsx", ValidateNames = true })
{
if(ofd.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
IExcelDataReader reader;
if (ofd.FilterIndex == 1)
reader = ExcelReaderFactory.CreateBinaryReader(fs);
else
reader = ExcelReaderFactory.CreateOpenXmlReader(fs);
reader.IsFirstRowAsColumnNames = true;
result = reader.AsDataSet();
reader.Close();
dataGridView.DataSource = result;
}
}
}
}
}
dataGridView.DataSource = DtSet.Tables[0];
I have the following C# code, which pulls data from a database & populates cell A1 w/ the data when Excel loads. How do I turn this into a custom function, whereby the field would populate when the user types in the formula (ie '=getMyData("mycustominput")'), rather than when excel loads?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using MySql.Data.MySqlClient;
namespace custom_excel
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
Excel.Range firstRow = activeWorksheet.get_Range("A1", missing);
firstRow.EntireRow.Insert(Excel.XlInsertShiftDirection.xlShiftDown, System.Type.Missing);
Excel.Range newFirstRow = activeWorksheet.get_Range("A1", missing);
string connString = "Server=localhost;Port=3306;Database=test;Uid=name;password=password";
MySqlConnection conn = new MySqlConnection(connString);
MySqlCommand command = conn.CreateCommand();
command.CommandText = "SELECT field_value FROM customers LIMIT 1";
try
{
conn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
newFirstRow.Value2 = reader["field_value"].ToString();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
}
}
Going out on a limb here, but have you looked at ExcelDNA? It sounds like it might work for what you are trying to do.
There is also this somewhat older article from Eric Carter that might be of use, which uses an automation add-in. It appears you can do it without any 3rd party libraries.
I have created a C sharp project in which i have added a ShockwaveFlashObject to play my swf file.
The problem i am facing is when i create an installer for my project it works correctly on my machine on installation, but on my laptop the swf loads correctly but doesn't respond to the _FSCommand. I cannot use a try and catch block as it is not entering the FSCommand handle. Do i need to bundle something with my installation?
The laptop i am using is brand new and i wanted it that way so that i know what all stuff is needed for things to work correctly so that i can add prerequisites to my installer.
Also idk if this information is of any use but I am using advanced installer to build and exe for my project.
PS i have added things like the below code to know if FSCommand gets executed.
MessageBox.Show("step 1/2/3");
Here is the entire code.
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 MySql.Data.MySqlClient;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Globalization;
namespace WindowsFormsApplication1
{
public partial class frmFlashIntro : Form
{
public Form FormfrmMainRef { get; set; }
public frmFlashIntro()
{
InitializeComponent();
axShockwaveFlash1.Playing = true;
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
string currentPath = Directory.GetCurrentDirectory();
axShockwaveFlash1.Movie = "file://\\" + currentPath + "\\intro.swf";
}
private void axShockwaveFlash1_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
{
MessageBox.Show("step 1");
string btn = e.command.ToString();
MessageBox.Show("step 2");
if (btn == "play")
{
MessageBox.Show("step 3");
try
{
MessageBox.Show("step 4");
var form2 = new frmMain();
MessageBox.Show("step 5");
this.Hide();
MessageBox.Show("step 6");
form2.Show();
}
catch (Exception ex) { MessageBox.Show(ex.ToString()); }
}
if (btn == "syllabus")
{
MySqlConnection con = new MySqlConnection(Properties.Settings.Default.conString);
con.Open();
Syllabus_usageInformation syl = new Syllabus_usageInformation(this);
MySqlCommand cmd = new MySqlCommand("SELECT ImageFiles FROM misc WHERE id=1", con);
byte[] img = (byte[])cmd.ExecuteScalar();
string strFn = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream(strFn, FileMode.CreateNew, FileAccess.Write);
fs.Write(img, 0, img.Length);
fs.Flush();
fs.Close();
con.Close();
syl.kpImageViewer1.OpenButton = false;
syl.kpImageViewer1.ImagePath = strFn;
syl.Show();
this.Hide();
}
if (btn == "usageInformation")
{ }
}
}
}
Is flash player ActiveX control installed on your laptop? You have to install it, otherwise there's nothing that can play your .swf file.