Solved

ADO.Net in C# INSERT Problems

Posted on 2006-06-14
3
235 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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