1

When I am inserting data in MS access database .it is not giving any error but data not inserted in database

code:

  private void btnsubmit_Click(object sender, EventArgs e)
    {

        int row = dataGridView1.RowCount;
        for (int i = 0; i < row - 1; i++)
        {
            String str = "insert into JDS_Data(job_no,order_no,Revision,DesignSpec,Engine_Type,date,LE_IN_Designer,CPH_Designer,Exp_Del_Week,Action_code,Rev_Description,Ref_pattern,Name_of_mock_up,EPC_Drawing,Turbocharger_no_Type,Engine_Specific_Requirement,Draft_sketch_with_details,Air_cooler_type,Description_of_Job,SF_No,Standard,Prority_Sequence,Remark,Part_family,Modified_Date,User) values('" + txtjobno.Text + "','" + txtorderno.Text + "','" + txtrevison.Text + "','" + txtds.Text + "','" + txtenginetype.Text + "','" + dateTimePicker1.Text + "','" + txtleindesigner.Text + "','" + txtcphdesigner.Text + "','" + txtexpweek.Text + "','" + txtactioncode.Text + "','" + txtrevdescription.Text + "','" + txtrefpatern.Text + "','" + txtmockup.Text + "','" + txtepcdwg.Text + "','" + txtturbono.Text + "','" + txtenginereq.Text + "','" + txtdraft.Text + "','" + txtaircolertype.Text + "','" + txtdespjob.Text + "','" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[2].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[3].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[4].Value.ToString() + "','" + DateTime.Today + "','" + mdlconnection.user_name + "')";

            int dd = mdlconnection.excuteQuery(str);
            MessageBox.Show(str);
            //if (dd > 0)
            {
                MessageBox.Show("Data Saved Successfully..!!!");

            }

        }

    }   

Code:

    public static int excuteQuery(string q)
    {
        int d = 0;
        try
        {
            OleDbCommand cmd = new OleDbCommand(q, con);
            d = cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return d;
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
shweta
  • 91
  • 1
  • 2
  • 11
  • 1
    What does `mdlconnection.excuteQuery` method exactly? And you should always use [parameterized queries](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/). This kind of string concatenations are open for [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. – Soner Gönül Oct 16 '15 at 07:19
  • Update this in your question with [edit] button under it. – Soner Gönül Oct 16 '15 at 07:22
  • ExcuteQuery is :public static int excuteQuery(string q) { int d = 0; try { OleDbCommand cmd = new OleDbCommand(q, con); d = cmd.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine(e.Message); } return d; } – shweta Oct 16 '15 at 07:23
  • it is not giving error but data not inserted in database ..other forms are working – shweta Oct 16 '15 at 07:30

1 Answers1

0

if you are using DataContext (you provided to little info) you should rewrite your statement to match the example:

var customers = db.ExecuteQuery<Customer>(@"SELECT CustomerID, CompanyName, ContactName, ContactTitle, 
   Address, City, Region, PostalCode, Country, Phone, Fax
   FROM   dbo.Customers
   WHERE  City = {0}", "London");

I should suggest to use this tutorial for the connection instead actually

online Thomas
  • 8,864
  • 6
  • 44
  • 85