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. object reference not set to an instsnce of an object

object reference not set to an instsnce of an object

Scheduled Pinned Locked Moved Database
helpcsharpdatabaselinqgraphics
8 Posts 5 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 records from txtxbox name as txtRegno and txtName when i run my prog i got error at con.open(); as object reference not set to an instsnce of an object need help here is the code 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); cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text ); cmd.Parameters.AddWithValue("@Name", txtName.Text ); try { **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) { } } }

    A M 2 Replies Last reply
    0
    • M Mangesh Tomar

      I am inserting two records from txtxbox name as txtRegno and txtName when i run my prog i got error at con.open(); as object reference not set to an instsnce of an object need help here is the code 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); cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text ); cmd.Parameters.AddWithValue("@Name", txtName.Text ); try { **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) { } } }

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      I cant see anywhere con is initialized... :(

      try
      {
      con = new OleDbConnection(this.connectionString); // this is necessary to overcome from NullReferenceException...
      con.Open();
      rows = cmd.ExecuteNonQuery();

      You need to initialize any object before you access any members . :) :rose:

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      My Latest Articles-->** Microsoft Bing MAP using Javascript
      CLR objects in SQL Server 2005
      Uncommon C# Keywords
      /xml>

      1 Reply Last reply
      0
      • M Mangesh Tomar

        I am inserting two records from txtxbox name as txtRegno and txtName when i run my prog i got error at con.open(); as object reference not set to an instsnce of an object need help here is the code 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); cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text ); cmd.Parameters.AddWithValue("@Name", txtName.Text ); try { **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) { } } }

        M Offline
        M Offline
        Mangesh Tomar
        wrote on last edited by
        #3

        i try this but noe it shows the following error Execute Nonquery : Connection properly has not been initialized what to do?

        N H A 4 Replies Last reply
        0
        • M Mangesh Tomar

          i try this but noe it shows the following error Execute Nonquery : Connection properly has not been initialized what to do?

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          Mangesh Tomar wrote:

          what to do?

          RTFM!!


          only two letters away from being an asset

          1 Reply Last reply
          0
          • M Mangesh Tomar

            i try this but noe it shows the following error Execute Nonquery : Connection properly has not been initialized what to do?

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            For your use in future posts, this is not really a Database Problem. Your problems are not caused by the database. You should have posted it in the forum for the language you are using. C# I think in your case. :)

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            L 1 Reply Last reply
            0
            • H Henry Minute

              For your use in future posts, this is not really a Database Problem. Your problems are not caused by the database. You should have posted it in the forum for the language you are using. C# I think in your case. :)

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              traffic cop at work again? wouldn't it be easier to simply swap the two forum titles? :laugh:

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              1 Reply Last reply
              0
              • M Mangesh Tomar

                i try this but noe it shows the following error Execute Nonquery : Connection properly has not been initialized what to do?

                A Offline
                A Offline
                Abhishek Sur
                wrote on last edited by
                #7

                Inspect your connection object before you call con.open. Put a breakpoint and debug your application. :)

                Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                My Latest Articles-->** Microsoft Bing MAP using Javascript
                CLR objects in SQL Server 2005
                Uncommon C# Keywords
                /xml>

                1 Reply Last reply
                0
                • M Mangesh Tomar

                  i try this but noe it shows the following error Execute Nonquery : Connection properly has not been initialized what to do?

                  A Offline
                  A Offline
                  Abhishek Sur
                  wrote on last edited by
                  #8

                  Also put Initialization code above the creation of Command object

                  query = "INSERT INTO master(RegNo,Name)VALUES(@RegNo,@Name)";
                  con = new OleDbConnection(connectionstring);
                  cmd = new OleDbCommand(query, con);

                  cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text );
                  cmd.Parameters.AddWithValue("@Name", txtName.Text );

                  try
                  {
                  con.Open();
                  rows = cmd.ExecuteNonQuery();
                  if (rows > 0)
                  {
                  MessageBox.Show("records inserted successfully");
                  }
                  }

                  I think it will be working now. :rose:

                  Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                  My Latest Articles-->** Microsoft Bing MAP using Javascript
                  CLR objects in SQL Server 2005
                  Uncommon C# Keywords
                  /xml>

                  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