7 Replies - 531 Views - Last Post: 06 March 2013 - 12:20 PM Rate Topic: -----

#1 JKT1030  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-March 13

ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 09:38 AM

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


namespace ProjectOCJB
{
    public partial class Form5 : Form
    {

        private OleDbConnection myCon;
        public Form5()
        {
            
            myCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|ForeignWorkerinfo.accdb");
            InitializeComponent();
            
        }
       
        private void Form5_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'foreignWorkerinfoDataSet.FWinFO' table. You can move, or remove it, as needed.
            this.fWinFOTableAdapter.Fill(this.foreignWorkerinfoDataSet.FWinFO);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = myCon;
            cmd = new OleDbCommand("INSERT INTO [FWinFO] ([ID], [Name], [Gender], [Date of Birth], [Country], [Date of Expire], [Passport No], [Working Place]) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
           
           
            cmd.Parameters.AddWithValue("@id", textBox8.Text);
            cmd.Parameters.AddWithValue("@name", textBox1.Text);
            cmd.Parameters.AddWithValue("@gender", textBox2.Text);
            cmd.Parameters.AddWithValue("@dob", dateTimePicker1.Value);
            cmd.Parameters.AddWithValue("@country", textBox4.Text);
            cmd.Parameters.AddWithValue("@doe", dateTimePicker2.Value);
            cmd.Parameters.AddWithValue("@passport", textBox6.Text);
            cmd.Parameters.AddWithValue("@workplace", textBox7.Text);
            
            cmd.ExecuteNonQuery();
            myCon.Close();
          
           

        }
    }
}



I get this error. anyone could help me? :ohno:

I was using visual studio 2010 c#.
1. Added datasource

Is This A Good Question/Topic? 0
  • +

Replies To: ExecuteNonQuery: Connection property has not been initialized.

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 09:47 AM

Where is foreignWorkerinfoDataSet.FWinFO initialized?
Was This Post Helpful? 0
  • +
  • -

#3 JKT1030  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-March 13

Re: ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 09:53 AM

View PostSkydiver, on 06 March 2013 - 09:47 AM, said:

Where is foreignWorkerinfoDataSet.FWinFO initialized?


HI, Where should i put this?

View PostJKT1030, on 06 March 2013 - 09:52 AM, said:

View PostSkydiver, on 06 March 2013 - 09:47 AM, said:

Where is foreignWorkerinfoDataSet.FWinFO initialized?


HI, Where should i put this?


Sorry, I'm new to C#, new learner
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 09:55 AM

If you are new to C#, I recommend learning the language first, instead of jumping right away into a GUI project that includes ADO.NET and WinForms. Additionally, since you are just learning, might as well invest time in learning WPF instead of WinForms.
Was This Post Helpful? 0
  • +
  • -

#5 JKT1030  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-March 13

Re: ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 09:57 AM

View PostSkydiver, on 06 March 2013 - 09:55 AM, said:

If you are new to C#, I recommend learning the language first, instead of jumping right away into a GUI project that includes ADO.NET and WinForms. Additionally, since you are just learning, might as well invest time in learning WPF instead of WinForms.

Thanks for your advised, but can you advise me on this ? because i stuck on the half way, i hope i can success..
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 10:09 AM

Why are you closing the connection on line 51. Are you sure that button1 will never ever be pressed again and that the connection will never be used again elsewhere?
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 11:21 AM

I don't see where the connection is even opened.
Was This Post Helpful? 0
  • +
  • -

#8 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: ExecuteNonQuery: Connection property has not been initialized.

Posted 06 March 2013 - 12:20 PM

You must to open connection.

try{
myCon.Open();
//Execute
}
catch
throw;
finally
myCon.Close();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1