NullReferenceException Help me out

any hint requierd urgently

Page 1 of 1

14 Replies - 1207 Views - Last Post: 11 March 2010 - 06:56 AM Rate Topic: -----

#1 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

NullReferenceException Help me out

Posted 10 March 2010 - 07:23 PM

DataTable tab = ds.Tables["Employe_details"];
                    DataRow row = tab.NewRow();
                    row[0] = int.Parse(textBox1.Text);
                    row[1] = textBox2.Text;
                    row[2] = textBox3.Text;
                    row[3] = int.Parse(textBox4.Text);
                    row[4] = textBox5.Text;




exception comng here in DataTable tab = ds.Tables["Employe_details"];


any hint requierd

full code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace libarymangementsystem
{
    public partial class Form1 : Form
    {
        SqlDataAdapter da1;
        DataSet ds;
        string cstr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cstr = "server=DESKTOP;user id=sa;password=admin;database=master";
            DataSet ds = new DataSet();
          

            }

        private bool validate1()
        {

            if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox3.Text.Length > 0 && textBox4.Text.Length > 0 && textBox5.Text.Length > 0 )
                return true;
            else
            {
                if (textBox1.Text == "")
                    MessageBox.Show("Roll Number field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox2.Text == "")
                    MessageBox.Show(" First Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox3.Text == "")
                    MessageBox.Show("Last Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox4.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox5.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return false;

        }



        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (validate1())
                {
                    DataTable tab = ds.Tables["Employe_details"];
                    DataRow row = tab.NewRow();
                    row[0] = int.Parse(textBox1.Text);
                    row[1] = textBox2.Text;
                    row[2] = textBox3.Text;
                    row[3] = int.Parse(textBox4.Text);
                    row[4] = textBox5.Text;

                    tab.Rows.Add(row);
                    MessageBox.Show("Your record has been successfully added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
            da1.InsertCommand = cb1.GetInsertCommand();
            da1.UpdateCommand = cb1.GetUpdateCommand();
            da1.DeleteCommand = cb1.GetDeleteCommand();
            da1.Update(ds, "Employe_details");
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: NullReferenceException Help me out

#2 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:32 PM

object reference not set to instance of the object :-(
Was This Post Helpful? 0
  • +
  • -

#3 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:39 PM

Did you spell the name of the table right?
Was This Post Helpful? 1
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:42 PM

You are not initializing ds before using it:

public partial class Form1 : Form
{
    SqlDataAdapter da1;
    DataSet ds;
    string cstr;
...
    if (validate1())
    {
        DataTable tab = ds.Tables["Employe_details"];
        DataRow row = tab.NewRow();
...




This ds is a variable local to Form1_Load() only:

private void Form1_Load(object sender, EventArgs e)
{
    cstr = "server=DESKTOP;user id=sa;password=admin;database=master";
    DataSet ds = new DataSet();
}


This post has been edited by n8wxs: 10 March 2010 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:46 PM

    public partial class Form1 : Form
    {
        SqlDataAdapter da1;
        DataSet ds;
        string cstr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cstr = "server=DESKTOP;user id=sa;password=admin;database=master";
            ds = new DataSet();
          

            }



i am using ds in public class ds = new DataSet();
Was This Post Helpful? 0
  • +
  • -

#6 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:52 PM

private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (validate1())
                {
                    ds = new DataSet();
                    DataTable tab = ds.Tables["Employe_details"];
                    DataRow row = tab.NewRow();
                    row[0] = int.Parse(textBox1.Text);
                    row[1] = textBox2.Text;
                    row[2] = textBox3.Text;
                    row[3] = int.Parse(textBox4.Text);
                    row[4] = int.Parse(textBox5.Text);

                    tab.Rows.Add(row);
                    MessageBox.Show("Your record has been successfully added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }




changed still getting same error nullreference exception and all things are right

This post has been edited by eclipsed4utoo: 11 March 2010 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:55 PM

Please post the DataSet definition.
Was This Post Helpful? 1
  • +
  • -

#8 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:56 PM

It doesn't look like you're even filling the dataset. Open a connection with SQLConnection. Use SQLDataAdaper to fill the dataset.

This post has been edited by DuoShin85: 10 March 2010 - 07:58 PM

Was This Post Helpful? 1
  • +
  • -

#9 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 07:56 PM

DataRow row = tab.NewRow();



error is comng in this line any new hint
Was This Post Helpful? 0
  • +
  • -

#10 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 08:01 PM

You haven't filled the DataSet. You only initialized it. So, when you are setting the DataTable. It comes out null, because the table "Employe_details" doesn't exist in the DataSet. Therefore, adding a new row to the DataTable will throw the NullReferenceException.
Was This Post Helpful? 1
  • +
  • -

#11 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 08:06 PM

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

namespace libarymangementsystem
{
    public partial class Form1 : Form
    {
        SqlDataAdapter da1;
        DataSet ds;
        string cstr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cstr = "server=DESKTOP;user id=sa;password=admin;database=master";
            ds = new DataSet();
            Fill1();

            }

        private void Fill1()
        {
            string qry = "select * from Employe_details";
            SqlConnection con = new SqlConnection(cstr);
            da1 = new SqlDataAdapter(qry, con);
            da1.Fill(ds, "Employe_details");
        }


        private void label17_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (validate1())
                {
                    
                    DataTable tab = ds.Tables["Employe_details"];
                    DataRow row = tab.NewRow();
                    row[0] = int.Parse(textBox1.Text);
                    row[1] = textBox2.Text;
                    row[2] = textBox3.Text;
                    row[3] = int.Parse(textBox4.Text);
                    row[4] = int.Parse(textBox5.Text);

                    tab.Rows.Add(row);
                    MessageBox.Show("Your record has been successfully added");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
            da1.InsertCommand = cb1.GetInsertCommand();
            da1.UpdateCommand = cb1.GetUpdateCommand();
            da1.DeleteCommand = cb1.GetDeleteCommand();
            da1.Update(ds, "Employe_details");
        }


        private bool validate1()
        {

            if (textBox1.Text.Length > 0 && textBox2.Text.Length > 0 && textBox3.Text.Length > 0 && textBox4.Text.Length > 0 && textBox5.Text.Length > 0)
                return true;
            else
            {
                if (textBox1.Text == "")
                    MessageBox.Show("Roll Number field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox2.Text == "")
                    MessageBox.Show(" First Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox3.Text == "")
                    MessageBox.Show("Last Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox4.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (textBox5.Text == "")
                    MessageBox.Show("Father's Name field cannot be left blank...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return false;

        }

    }
}



error shifted to here da1.Fill(ds, "Employe_details");

need some more help :-(
Was This Post Helpful? 0
  • +
  • -

#12 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 08:10 PM

Is your connection string valid?
Was This Post Helpful? 0
  • +
  • -

#13 javafreak  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 08:13 PM

yes it is
Was This Post Helpful? 0
  • +
  • -

#14 DuoShin85  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 135
  • Joined: 18-December 09

Re: NullReferenceException Help me out

Posted 10 March 2010 - 08:24 PM

Do the following in the Fill1 method.

using(SqlConnection conn=new SqlConnection(cstr))
{
   da1=new SqlDataAdapter(qry, conn);
   da1.Fill(ds,"Employe_details");
}



The using statement handles the opening and closing of the SqlConnection which wasn't being done before.
Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: NullReferenceException Help me out

Posted 11 March 2010 - 06:56 AM

Are you still getting the NullReferenceException with the ds.Fill() method? or are you getting another error message?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1