Let me start by saying this is my first real c# application, so I know there are probably more efficient ways of doing things, but its my first attempt. Anyway the purpose of the application is to read in data from a comma seperated file, seperate each element of that comma seperated file, and then import those elements into rows in a database. I am currently stuck on my try and catch phrase where it goes to the catch everytime so it never connects to my database. Any and all tips, hints, or help would be greatly appreciated.
Current Error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
Additional information: Connection must be valid and open
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 MySql.Data.MySqlClient;
using System.IO;
using System.Data.SqlClient;
namespace Form1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (File.Exists(@"C:\Users\E15913\Downloads\testfile3.txt"))
File.Delete(@"C:\Users\E15913\Downloads\testfile3.txt");
var reader = new StreamReader(File.OpenRead(@"C:\Users\E15913\Downloads\ALMGrade.csv"));
using (StreamReader readFile = new StreamReader(@"C:\Users\E15913\Downloads\ALMGrade.csv"))
{
string newline = "\n";
string line;
string[] row;
int i = 0;
while ((line = readFile.ReadLine()) != null)
{
i++;
row = line.Split(',');
System.IO.File.AppendAllText(@"C:\Users\E15913\Downloads\testfile3.txt", row[0].ToString() + Environment.NewLine + row[1].ToString() + Environment.NewLine + row[2].ToString() + Environment.NewLine + row[3].ToString() + Environment.NewLine + row[4].ToString() + Environment.NewLine + row[5].ToString() + Environment.NewLine + row[6].ToString() + Environment.NewLine + row[7].ToString() + Environment.NewLine);
}
string MyConString = "SERVER=10.100.135.220;" + "DATABASE=ak_steel;" + "UID=pickler;" + "PASSWORD=csmrecession09;";
string _recDateTimeMySql = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
using (MySqlConnection connection = new MySqlConnection(MyConString))
{
MySqlCommand cmd = new MySqlCommand("insert into p4gradetable_copy (Grade, CoilingTemp, GaugeRange, WidthRange, MeshCassette1, MeshCassette2, XBow, Elongation ) VALUES(" + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"' + "," + '"' + line + '"');
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Grade", line);
cmd.Parameters.AddWithValue("@CoilingTemp", line);
cmd.Parameters.AddWithValue("@GaugeRange", line);
cmd.Parameters.AddWithValue("@WidthRange", line);
cmd.Parameters.AddWithValue("@MeshCassette1", line);
cmd.Parameters.AddWithValue("@MeshCassette2", line);
cmd.Parameters.AddWithValue("@XBow", line);
cmd.Parameters.AddWithValue("@Elongation", line);
connection.Open();
cmd.ExecuteNonQuery();
label1.Text = "It Works";
}
}
}
}
}