Search code examples
c#databasems-accessparametersoledb

Inserting a row of data from ASP webpage to Access database with OleDB


I am trying to insert a row of data into my MS Access Database. When I debug this through VS it does not catch a problem, but isn't inserting rows. This query is turning out to not have inserted any rows. My date are strings, and formatted correctly. I am passing 18 values over in the parameter list, there has got to be an easier way to do this. Is there another way to use parameters for OleDb? I have posted below my methods. Could you see if my syntax is correct? It still won't work.

Here is where I pass the information:

rowsAdded = ((DataAccessLayer)Application["dbAccess"]).insert("Employees", txtLname.Text, txtFname.Text, txtTitle.Text, txtCourt.Text,
        txtAddress.Text, txtCity.Text, txtCountry.Text, txtHomePhone.Text, txtExtension.Text, int.Parse(txtReports.Text), txtPassword.Text,
        txtPostalCode.Text, txtNotes.Text, txtRegion.Text, txtHireDate.Text, txtBday.Text, upPhoto.FileName.ToString());

Here is where I query the db with this method

public int insert(string tablename, string lname, string fname, string title, string toc, string address, string city,
    string country, string phone, string ext, int report, string pass, string postal, string notes, string region, 
    string hire, string birth, string photo)
{
    string tblName = tablename;
    string last = lname;
    string first = fname;
    string tlt = title;
    string tOfc = toc;
    string addy = address;
    string town = city;
    string reg = country;
    string phum = phone;
    string exten = ext;
    int rep = report;
    string pas = pass;
    string pc = postal;
    string note = notes;
    string regions = region;
    string hD = hire;
    string bD = birth;
    string pho = photo; 
    int rows = 0;
    int ID = 0;
    string insertString = "INSERT INTO @tablename ([EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[Address],[City]," +
    "[Country],[HomePhone],[Extension],[ReportsTo],[Password],[PostalCode],[Notes],[Region], [HireDate],[BirthDate],[Photo]) VALUES (" + 
    ID + ", @lname, @fname, @title, @toc, @addy, @city, @country," +
    "@phone, @ext, @report, @pass,  @postal, @notes,@region, @hire, @birth, @photo)";
    string queryLast = "Select @@Identity";      
    try
    {
        conn.Open();
        oleCommand = new OleDbCommand(insertString, conn);
        oleCommand.CommandText = queryLast;
        ID = (int)oleCommand.ExecuteScalar();

        oleCommand.Parameters.AddWithValue("@tablename", tblName);
        oleCommand.Parameters.AddWithValue("@lname", last);
        oleCommand.Parameters.AddWithValue("@fname", first);
        oleCommand.Parameters.AddWithValue("@title", tlt);
        oleCommand.Parameters.AddWithValue("@toc", tOfc);
        oleCommand.Parameters.AddWithValue("@addy", addy);
        oleCommand.Parameters.AddWithValue("@city", town);
        oleCommand.Parameters.AddWithValue("@country", reg);
        oleCommand.Parameters.AddWithValue("@phone", phum);
        oleCommand.Parameters.AddWithValue("@ext", exten);
        oleCommand.Parameters.AddWithValue("@report", rep);
        oleCommand.Parameters.AddWithValue("@pass", pas);
        oleCommand.Parameters.AddWithValue("@region", regions);
        oleCommand.Parameters.AddWithValue("@postal", pc);
        oleCommand.Parameters.AddWithValue("@notes", note);
        oleCommand.Parameters.AddWithValue("@hire", hD);
        oleCommand.Parameters.AddWithValue("@birth", bD);
        oleCommand.Parameters.AddWithValue("@photo", pho);
        oleCommand.ExecuteNonQuery();

        rows = (int)oleCommand.ExecuteNonQuery();

    }
    catch (Exception ex)
    {
        dbError = "Add Employee command Error: " + ex.Message;
    }
    finally
    {
        conn.Close();
    }
    return rows;
}

Solution

  • In your code you do not execute oleCommand with CommandText being insertString. All three executions are done with command text equal to queryLast.

    The fact that you passed insertString to OleDbCommand constructor does not mean that it somehow will be remembered after you assign

    oleCommand.CommandText = queryLast;
    

    On this assignment oleCommand forgets about insertString and will only execute what is in its CommandText until you assign something else.

    Another thing: executing insertString will likely throw exception because sql parameterization does not support table names specified as parameters, if I am not mistaken.

    If EmployeeID column is autoincremented - do not include it in the insert. And again - you need to get your steps straight:

    1. create command with insertString,

    2. specify parameters,

    3. execute the insert,

    4. assign queryLast to CommandText,

    5. execute to get the last id.