0

I have a database and I want to insert some data into it. First, I insert 4 different datatypes. After that I have to insert one last thing. I made 2 functions to do it. After I execute the first one in form load , the second one don't run.

I tried to do some debugging by showing a message after the first call but nothing happened.

string sir;
string[] siruri;
OleDbCommand cmd;
StreamReader f;
public Form1()
{
    InitializeComponent();
}

private void add()
{
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=TurismDB.accdb");
    con.Open();
    f = new StreamReader("Vacante.txt");
    while ((sir = f.ReadLine()) != null)
    {
        siruri = sir.Split('|');
        cmd = new OleDbCommand("INSERT INTO Vacante(NumeVacanta , Descriere , Pret , NrLocuri) VALUES(@nume , @descriere , @pret , @nr)", con);
        cmd.Parameters.AddWithValue("@nume", siruri[0].Trim());
        cmd.Parameters.AddWithValue("@descriere", siruri[1].Trim());
        cmd.Parameters.AddWithValue("@pret", siruri[2].Trim());
        cmd.Parameters.AddWithValue("@nr", siruri[3].Trim());
        cmd.ExecuteNonQuery();
    }
    con.Close();

}
private void add2()
{
    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=TurismDB.accdb");
    con.Open();
    string path = @"C:\Users\Elev\Desktop\alkin\Turismul de pretutindeni\Turismul de pretutindeni\bin\Debug\Imagini";
    string result = Path.GetFileName(path);
    cmd = new OleDbCommand("SELECT * FROM Vacante", con);
    OleDbDataReader reader = cmd.ExecuteReader();
    DirectoryInfo d = new DirectoryInfo(path);
    FileInfo[] files = d.GetFiles("*.jpg");

    while (reader.Read())
    {
        MessageBox.Show(reader[1].ToString());
        foreach (FileInfo file in files)
        {
            if (reader[1].ToString() == file.Name)
            {
                OleDbCommand cmd1 = new OleDbCommand("INSERT INTO Vacante(CaleFisier) VALUES(@cale)", con);
                cmd1.Parameters.AddWithValue("@cale", file.Name);
                cmd1.ExecuteNonQuery();
            }
            else
            {
                OleDbCommand cmd1 = new OleDbCommand("INSERT INTO Vacante(CaleFisier) VALUES implicit.txt", con);
                cmd1.ExecuteNonQuery();
            }
        }
    }
}
private void Form1_Load(object sender, EventArgs e)
{
    add();
    add2();
}
Steve
  • 213,761
  • 22
  • 232
  • 286
Alin00
  • 23
  • 4
  • 1
    If you know how to put a breakpoint, then put one in that while loop and see if anything is inserted at the first place. – Mat J May 09 '19 at 11:11
  • I suggest you to move that code from the Form Load event to a button click event. Sometime Form_Load hides exceptions and this could explain the missing call to the second Add. Here the details: https://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a?noredirect=1&lq=1 – Steve May 09 '19 at 11:35
  • Also your second method _Add2_, when you will be able to call it, will fail again because you are using the same connection for ExecuteReader and for ExecuteNonQuery. A connection, when is busy serving a datareader cannot be used to execute other commands. You need a second connection here. – Steve May 09 '19 at 11:59
  • 2
    Note: you should almost always use the `using` statement for connections and commands. E.g., `using (OleDbConnection con = new OleDbConnection()) { con.Open(); ... }` Right now, your code is likely to leak memory and exhaust your connection pool. Same with `StreamReader`s – Heretic Monkey May 09 '19 at 12:00
  • You are calling Path.GetFileName(path); where path has no file specified. – Ross Bush May 09 '19 at 12:56
  • However, it appears you are not referencing that variable anywhere else so disregard. – Ross Bush May 09 '19 at 12:58

0 Answers0