Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Execute Nonquery : Connection properly has not been initialized

Execute Nonquery : Connection properly has not been initialized

Scheduled Pinned Locked Moved Database
databasehelpcsharplinqgraphics
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mangesh Tomar
    wrote on last edited by
    #1

    i am inserting two record in data base usin txtReg and txtname using acess database when i run my program it shows following error Execute Nonquery : Connection properly has not been initialized here is my code need help 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 studentManagementSystem1 { public partial class StudentDetails : Form { public OleDbCommand cmd; public OleDbConnection con; string connectionString, query; public StudentDetails() { InitializeComponent(); } private void StudentDetails_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'studmanageDataSet.master' table. You can move, or remove it, as needed. this.masterTableAdapter.Fill(this.studmanageDataSet.master); connectionString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\studentManagementSystem1\studmanage.mdb"; } private void btnAdd_Click(object sender, EventArgs e) { int rows; query = "INSERT INTO master(RegNo,Name)VALUES(@RegNo,@Name)"; cmd = new OleDbCommand(query, con); con = new OleDbConnection(this.connectionString); cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text ); cmd.Parameters.AddWithValue("@Name", txtName.Text ); try { con = new OleDbConnection(this.connectionString); con.Open(); rows = cmd.ExecuteNonQuery(); if (rows > 0) { MessageBox.Show("records inserted successfully"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } private void btnEdit_Click(object sender, EventArgs e) { } } }

    C I N 3 Replies Last reply
    0
    • M Mangesh Tomar

      i am inserting two record in data base usin txtReg and txtname using acess database when i run my program it shows following error Execute Nonquery : Connection properly has not been initialized here is my code need help 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 studentManagementSystem1 { public partial class StudentDetails : Form { public OleDbCommand cmd; public OleDbConnection con; string connectionString, query; public StudentDetails() { InitializeComponent(); } private void StudentDetails_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'studmanageDataSet.master' table. You can move, or remove it, as needed. this.masterTableAdapter.Fill(this.studmanageDataSet.master); connectionString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\studentManagementSystem1\studmanage.mdb"; } private void btnAdd_Click(object sender, EventArgs e) { int rows; query = "INSERT INTO master(RegNo,Name)VALUES(@RegNo,@Name)"; cmd = new OleDbCommand(query, con); con = new OleDbConnection(this.connectionString); cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text ); cmd.Parameters.AddWithValue("@Name", txtName.Text ); try { con = new OleDbConnection(this.connectionString); con.Open(); rows = cmd.ExecuteNonQuery(); if (rows > 0) { MessageBox.Show("records inserted successfully"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } private void btnEdit_Click(object sender, EventArgs e) { } } }

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      I would say just change the order of these two lines from: cmd = new OleDbCommand(query, con); con = new OleDbConnection(this.connectionString); to: con = new OleDbConnection(this.connectionString); cmd = new OleDbCommand(query, con); because you initializing your OleDbCommand (cmd) with a not initilized connection value. Edit: I reviewed your code and saw some other things you do wrong. Your function btnAdd_Click should look like:

      private void btnAdd_Click(object sender, EventArgs e)
      {
      int rows;
      query = "INSERT INTO master(RegNo,Name)VALUES(@RegNo,@Name)";
      con = null;

      try
      {
          con = new OleDbConnection(this.connectionString); 
          con.Open();
      
          cmd = new OleDbCommand(query, con);
          cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text );
          cmd.Parameters.AddWithValue("@Name", txtName.Text );
      
          rows = cmd.ExecuteNonQuery();
          if (rows > 0)
              MessageBox.Show("records inserted successfully");
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
      finally
      {
          if(con != null)
              con.Close();
      }
      

      }

      Greetings Covean PS: Its a not the best way to do queries in the ui-code.

      1 Reply Last reply
      0
      • M Mangesh Tomar

        i am inserting two record in data base usin txtReg and txtname using acess database when i run my program it shows following error Execute Nonquery : Connection properly has not been initialized here is my code need help 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 studentManagementSystem1 { public partial class StudentDetails : Form { public OleDbCommand cmd; public OleDbConnection con; string connectionString, query; public StudentDetails() { InitializeComponent(); } private void StudentDetails_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'studmanageDataSet.master' table. You can move, or remove it, as needed. this.masterTableAdapter.Fill(this.studmanageDataSet.master); connectionString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\studentManagementSystem1\studmanage.mdb"; } private void btnAdd_Click(object sender, EventArgs e) { int rows; query = "INSERT INTO master(RegNo,Name)VALUES(@RegNo,@Name)"; cmd = new OleDbCommand(query, con); con = new OleDbConnection(this.connectionString); cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text ); cmd.Parameters.AddWithValue("@Name", txtName.Text ); try { con = new OleDbConnection(this.connectionString); con.Open(); rows = cmd.ExecuteNonQuery(); if (rows > 0) { MessageBox.Show("records inserted successfully"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } private void btnEdit_Click(object sender, EventArgs e) { } } }

        I Offline
        I Offline
        i j russell
        wrote on last edited by
        #3

        Two points: 1. This is a database forum not a c# one which is where your question should go as it's an ADO.Net issue. 2. If you have already started a thread, don't start a new one on exactly the same issue 12 hours later. Have you even tried using Google to find an answer by searching on the error message?

        1 Reply Last reply
        0
        • M Mangesh Tomar

          i am inserting two record in data base usin txtReg and txtname using acess database when i run my program it shows following error Execute Nonquery : Connection properly has not been initialized here is my code need help 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 studentManagementSystem1 { public partial class StudentDetails : Form { public OleDbCommand cmd; public OleDbConnection con; string connectionString, query; public StudentDetails() { InitializeComponent(); } private void StudentDetails_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'studmanageDataSet.master' table. You can move, or remove it, as needed. this.masterTableAdapter.Fill(this.studmanageDataSet.master); connectionString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\studentManagementSystem1\studmanage.mdb"; } private void btnAdd_Click(object sender, EventArgs e) { int rows; query = "INSERT INTO master(RegNo,Name)VALUES(@RegNo,@Name)"; cmd = new OleDbCommand(query, con); con = new OleDbConnection(this.connectionString); cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text ); cmd.Parameters.AddWithValue("@Name", txtName.Text ); try { con = new OleDbConnection(this.connectionString); con.Open(); rows = cmd.ExecuteNonQuery(); if (rows > 0) { MessageBox.Show("records inserted successfully"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } private void btnEdit_Click(object sender, EventArgs e) { } } }

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4
          1. You have already asked this question in another thread on this forum 2) You have been advised this is not the correct forum for your question 3) It's clear you don't understand what you are doing. Go read a book, then come back and ask any questions in the appropriate forum.

          only two letters away from being an asset

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups