Solved

ADO.Net in C# INSERT Problems

Posted on 2006-06-14
3
234 Views
Last Modified: 2010-04-16
Hi,

I asked a previous question on this topic and thought I had a working answer but was then still unable to get it to work so i'm posting this again.  I need to figure out why I get an error when doing an insert from the following code:

OleDbDataAdapter daOurAdapter;
            DataSet dsResults;

            //do a search
            this.mstrSQL = "SELECT * FROM Customers";

            this.instantiateDatabase();

            try
            {
                //create an adapter to interact with the database
                daOurAdapter = new OleDbDataAdapter(this.mstrSQL, this.dbConnect);

                // Set up insert, update, and delete commands
                OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(daOurAdapter);

                //make our dataset object an actual dataset
                dsResults = new DataSet();

                //now fill our dataset object with data (if any)
                daOurAdapter.Fill(dsResults, "Customers");

                //we now have a DataSet object that contains any results that have been returned by our query
                //create a DataTable object representing the table returned
                DataTable dtCustomers = dsResults.Tables["Customers"];

                DataRow drCustomerResults = dtCustomers.NewRow();              
               
                //fill in all the data on this form now
                drCustomerResults["CompanyName"] = txtCompanyName.Text;
                drCustomerResults["SpecialNotes"] = txtSpecialNotes.Text;
                drCustomerResults["Username"] = txtUsername.Text;
                drCustomerResults["Password"] = txtPassword.Text;
                drCustomerResults["EmailAddress"] = txtEmailAddress.Text;
                drCustomerResults["FirstName"] = txtFirstName.Text;
                drCustomerResults["LastName"] = txtLastName.Text;
                drCustomerResults["PhoneNumber"] = txtPhoneNumber.Text;
                drCustomerResults["FaxNumber"] = txtFaxNumber.Text;
                drCustomerResults["Country"] = txtCountry.Text;
                drCustomerResults["BillingAddress"] = txtAddress.Text;
                drCustomerResults["BillCity"] = txtCity.Text;
                drCustomerResults["BillState"] = cboState.Text;
                drCustomerResults["BillZipCode"] = txtZipCode.Text;
                drCustomerResults["CreditCardType"] = cboCreditCard.Text;
                drCustomerResults["CreditCardNumber"] = txtCreditCardNumber.Text;
                drCustomerResults["ShipAddress"] = txtShipAddress.Text;
                drCustomerResults["ShipCity"] = txtShipCity.Text;
                drCustomerResults["ShipState"] = cboShipState.Text;
                drCustomerResults["PreferredShipping"] = cboPreferredShipping.SelectedIndex;
                drCustomerResults["ShippingNumber"] = txtShippingNumber.Text;
                drCustomerResults["ShipZip"] = txtShipZipCode.Text;
                drCustomerResults["ShipCountry"] = txtShipCountry.Text;
                if (txtShipAddress.Enabled == false)
                    drCustomerResults["ShippingSame"] = 1;
                else
                    drCustomerResults["ShippingSame"] = 0;
                //push all changes to the database
                dtCustomers.Rows.Add(drCustomerResults);

                if (!dsResults.HasErrors)
                {

//THIS LINE BELOW DOES NOT WORK, CAUSES AN ERROR: "SYNTAX ERROR in INSERT INTO statement"
                    daOurAdapter.Update(dsResults, "Customers");

                    dsResults.AcceptChanges();
                }                
            }
            catch (Exception ex)
            {
                string strMsg = "Error Message = " + ex.Message;
            }


As you can see, the daOurAdapter.Update line does not work even though I constructed a command builder object.  Is there anything else I need to do?  My understanding is that it should handle basic inserts, updates, and deletes.

Thanks,
Nathan
0
Comment
Question by:ironwill96
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
prosh0t earned 500 total points
ID: 16907256
just a guess..

try adding this after creating the command builder:

cmdBuilder.QuotePrefix = "[";
cmdBuilder.QuoteSuffix = "]";

0
 
LVL 2

Author Comment

by:ironwill96
ID: 16907594
That fixed it!  Thanks a ton!!!  Now, what exactly is that doing?  Creating some sort of escape character for quotes?  None of the inputted data contained quotes though (the user input) so why would I have to put that in to make it work?

Thanks,
Nathan
0
 
LVL 7

Expert Comment

by:prosh0t
ID: 16907626
Usually this happens when your column or table names are set to keywords that the database system uses in the background.  I think 'username' and 'password' are keywords in this case... so if they don't have brackets around them in  your query, the query is invalid because the database doesn't know whether it's a column name or the system keyword.

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
Angular JS Route 3 70
Page navigation in windows phone 8.1 application not working. 5 24
HttpPostedFile 1 26
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question