Sign in to follow this  

[.net] C# SQL Insert Command.. Not working?

This topic is 3294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm quite new and have been looking for good advice! Lucky you. Anywayss, I'm writing a program which has to INSERT data into my Access database. Simple, atleast it should be. Having trouble though, its not actually doing it. Can't figure it out either, I assume it mys SQL statement. I don't know how else I'd write it at least. Permissions may be an issue, I'm not sure. Heres the code:
            Int64 id, ctele;
            id = Convert.ToInt64(lbl_add_Id.Text);
            string cname, caddress, cpost;
            cname = txt_Add_Name.Text;
            caddress = txt_Add_Address.Text;
            cpost = txt_Add_PC.Text;
            ctele = Convert.ToInt64(txt_Add_Tele.Text);

   //SQL adapter to gain access to the database, including the SQL statement
            OleDbDataAdapter da_Customer_View = new OleDbDataAdapter(
                  "INSERT INTO tbl_Customers" +
                  "VALUES('" + (id) + "'" + ",'" + (cname) + "','" +                 (caddress) + "','" + (cpost) + "','" + (ctele) + "')",
                  "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Documents and Settings\\Me\\My Documents\\"
                  + "SQL Databases\\XMAS SQL Database\\FoodTastic.accdb");
            lst_New_Customers.Items.Clear();
            UpdateCustomer();

Any idas?

Share this post


Link to post
Share on other sites
First problem I see is:


"INSERT INTO tbl_Customers" +

"VALUES('" + (id) + "'" ...



Without the +, this works out to:

"INSERT INTO tbl_CustomersVALUES('id' ..."

I'd suggest you use string.Format() to build your strings like these, so you can just do:


string s = string.Format("INSERT INTO {0} VALUES ('{1}','{2}','{3}' ...")...



It'll make problems like this a lot easier to find.

Share this post


Link to post
Share on other sites

This topic is 3294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this