locked
insert into Access DB RRS feed

  • Question

  • User-584964728 posted

    Hi everyone,

    I'm having a problem in insert to a table and relly don't know where the error, hope u help me and thanks in advance.

     

    OleDbConnection oleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=COE.mdb");

    OleDbCommand oleDbCommand = new OleDbCommand();

    oleDbCommand.Connection = oleDbConnection;

    oleDbCommand.CommandText =

    "INSERT INTO Table1 ([ID], [Trade ID], [Title], [Qty]) VALUES ('5', '" + textBox2.Text + "', '" + comboBox1.SelectedValue + "','" + textBox1.Text + "')";

                oleDbConnection.Open();

                oleDbCommand.ExecuteNonQuery();

                oleDbConnection.Close();

     

    its applied ok but there's no data inserted to the DB when I open it

     

    Monday, December 12, 2011 4:31 AM

Answers

  • User-584964728 posted

    ok I guess I figured the problem what it was, its about x64 & x86, u need to change the platform to x86 to work with u from Build > Configuration Manager

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 12, 2011 7:31 AM

All replies

  • User1839833660 posted

    Add this connection string in web.config 

    <connectionStrings>
    <add name="ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db1.mdb;Persist Security Info=True"
    providerName="System.Data.OleDb" />
    </connectionStrings>

    string strconnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    OleDbConnection con = new OleDbConnection(strconnection);
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = con;
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "Insert Into product(title,empid) values('test',11)";
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    Monday, December 12, 2011 5:11 AM
  • User-584964728 posted

    Thanks for replying amit.jain

    the connection string is already there as u mentioned, and I tried ur code, but still the same problem. 

     

    Monday, December 12, 2011 5:31 AM
  • User1839833660 posted

    try this 

    "INSERT INTO Table1 ([ID], [Trade ID], [Title], [Qty]) VALUES (5, " + textBox2.Text + ", '" + comboBox1.SelectedValue + "'," + textBox1.Text + ")";

    For integer filed if inserting value directly then no need to put qoutes, if taking from controls then put it in double qoutes ""

    For text field put them in ' " " '  ( single quote doubleqoute value double qoute single quote)

    Monday, December 12, 2011 5:37 AM
  • User-584964728 posted

    thanks for the addtional information, but unfortunately its still the same. When I click on the button at the first time its give me no error, but when I click it again its give error on ID bcz its the primary key and of course it can't be duplicated which it means the record is inserted, but when I goes to the DB its show me there's no record

    Monday, December 12, 2011 5:50 AM
  • User-584964728 posted

    ok I guess I figured the problem what it was, its about x64 & x86, u need to change the platform to x86 to work with u from Build > Configuration Manager

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 12, 2011 7:31 AM
  • User-1199946673 posted

    "INSERT INTO Table1 ([ID], [Trade ID], [Title], [Qty]) VALUES (5, " + textBox2.Text + ", '" + comboBox1.SelectedValue + "'," + textBox1.Text + ")";

    For integer filed if inserting value directly then no need to put qoutes, if taking from controls then put it in double qoutes ""

    For text field put them in ' " " '  ( single quote doubleqoute value double qoute single quote)

    I don't think this is a good advice. The better option would be to use parameterized queries:

    http://www.mikesdotnetting.com/Article/26/Parameter-Queries-in-ASP.NET-with-MS-Access

    Monday, December 12, 2011 8:44 AM