Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
im using sqlserver2008,Visual studio 2010 on windows 7 ultimate.i have school database which contains student table.im not able to access the data from the backend.whats wrong in this code.object reference not set to an instance of an object.


C#
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.SqlClient;

namespace WindowsFirstsample
{
    public partial class Form1 : Form
    {
        private SqlConnection cn;
       
        private SqlDataAdapter da;
        private SqlCommand cmd;
        private DataSet ds;
       
        private string strcon;
        private string strq;
        private int icounter;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            strcon="Data Source=HP-PC//SQLEXPRESS;Initial Catalog=school;Integrated security=true; ";
            
            cn = new SqlConnection(strcon);
            
            strq = "select * from student";
            cn.Open();
            da = new SqlDataAdapter(strq,cn);
           
            ds = new DataSet();
          
            da.Fill(ds);
            textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString();
            textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString();
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            icounter = 0;
            textBox1.Text = ds.Tables[0].Rows[0]["id"].ToString();
            textBox2.Text = ds.Tables[0].Rows[0]["name"].ToString();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int irowcount = ds.Tables[0].Rows.Count - 1;
            if (icounter < irowcount)
            {
                icounter++;
                textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString();
                textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString();
            }
            else
            {
                MessageBox.Show("you are already on the last record");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (icounter > 0)
            {
                icounter--;
                textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString();
                textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString();
            }
            else
            {
                MessageBox.Show("you are already on the first record");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            icounter = ds.Tables[0].Rows.Count - 1;
            textBox1.Text = ds.Tables[0].Rows[icounter]["id"].ToString();
            textBox2.Text = ds.Tables[0].Rows[icounter]["name"].ToString();
        }
    }
}
Posted
Updated 22-May-14 2:36am
v2
Comments
_Asif_ 22-May-14 8:38am    
On which line you are getting this object reference not set to an instance of an object error?
Member 10833429 22-May-14 8:41am    
on the Data set line
_Asif_ 22-May-14 9:02am    
Did you check whether ds.Tables[0].Rows[0] exist? Do we have any data in the row? Even do we have a Tables[0] exist? Check the existence. You can check by debugging the line.
[no name] 22-May-14 9:33am    
This occurs due to not initializing an object.

1 solution

from msdn:
Quote:
The exception that is thrown when there is an attempt to dereference a null object reference.

here is the link: NullReferenceException[^]

this exception means that you tried to access a non-existing object. it is better to check for null values before using references.
the best: use the debugger

F5 start debug
F9 toggle breakpoint
F10 step over
F11 step into

have a good day.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900