Solved

ADO.Net in C# INSERT Problems

Posted on 2006-06-14
3
232 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
Comment Utility
just a guess..

try adding this after creating the command builder:

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

0
 
LVL 2

Author Comment

by:ironwill96
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now