7 Replies - 944 Views - Last Post: 17 July 2016 - 10:53 AM Rate Topic: -----

#1 newmember25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-January 16

insert into ms access 2010 database with c#

Posted 17 July 2016 - 04:18 AM

i made a database with Micrsoft Access 2010.Then i made a richTextBox and i try to insert the text to mybase. Unfortunately ,i made something wrong and it does not insert anything and i did not have and an exception-problem.What did i make wrong?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace inordertoask
{
    public partial class Form1 : Form
    {
        String cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mybase.mdb";
        OleDbConnection cn;
        public Form1()
        {
            InitializeComponent();
            cn = new OleDbConnection(cs);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cn.Open();
            String mq = "Insert into Table1(textuser) values(richTextBox1.Text.ToString())";
            OleDbCommand cmd = new OleDbCommand(mq, cn);
            cn.Close();

        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: insert into ms access 2010 database with c#

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: insert into ms access 2010 database with c#

Posted 17 July 2016 - 04:32 AM

You haven't executed your statement, ExecuteNonQuery.

But you haven't successfully concatenated the RTB value into the statement, probably causing an error.

You can concatenate the RTB value, or using string.Format. If it is a text value then it needs to be surrounded with single quotes. Much better is to use a parameter, then you are free from SQL injection and won't have to be concerned with delimiting values.

An example using parameters.

(You also don't need to follow .Text with .ToString(), .Text is already a string.)
Was This Post Helpful? 1
  • +
  • -

#3 newmember25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-January 16

Re: insert into ms access 2010 database with c#

Posted 17 July 2016 - 05:10 AM

yes i wrote
cmd.ExecuteNonQuery();
i have an exception problem.

This post has been edited by tlhIn`toq: 17 July 2016 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: insert into ms access 2010 database with c#

Posted 17 July 2016 - 05:21 AM

Which exception problem? There are thousands of them. And does it relate to the information I have already provided?
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: insert into ms access 2010 database with c#

Posted 17 July 2016 - 05:25 AM

The code in your first post does not include the ExecuteNonQuery() line.
Which makes me think the code from your first post is not your REAL code that you're having trouble with.
As you can see not showing us your real code just wastes our time because often it isn't accurate.

Please post your real code by copy/paste from Visual Studio so as to not waste our time.
Was This Post Helpful? 0
  • +
  • -

#6 newmember25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-January 16

Re: insert into ms access 2010 database with c#

Posted 17 July 2016 - 08:20 AM

do you want to upload and the whole project?this is a joke.
Was This Post Helpful? -1
  • +
  • -

#7 newmember25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 18-January 16

Re: insert into ms access 2010 database with c#

Posted 17 July 2016 - 08:28 AM

finally i manage to solve the problem!thank youandrewsw!
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: insert into ms access 2010 database with c#

Posted 17 July 2016 - 10:53 AM

Please share what resolved the problem... That way the next person experiencing a similar issue might be able to get some help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1