Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:07:46+00:00 2026-06-14T19:07:46+00:00

I am trying to insert a row of data into my MS Access Database.

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:07:47+00:00Added an answer on June 14, 2026 at 7:07 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to insert data into a particular row in the database, but i
I'm trying to insert a data into oracle database(version 11g xe).But when i'm trying
I'm trying to insert a row into an access database (mdb, access 2000 AFAIK)
I am trying to insert a row of data into a table in SQL
I am trying to insert Data into my Sql-Server database though C#. I'm Calling
When trying to INSERT data into a database table, I've been getting the following
I'm trying to insert a row into a table I have in Access. The
I'm trying to insert a large volume of data with NHibernate into a database,
I am currently trying to insert data into a table called customer_quote , this
I'm trying to insert XML data into mysql database. Issue I'm having is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.