Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace School
{
public partial class Classes : Form
{

SqlCommandBuilder scb;
SqlDataAdapter adapter;
DataSet dataset;




public Classes()
{
InitializeComponent();
}



private void Classes_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'schoolDataSet.Add_Student' table. You can move, or remove it, as needed.
this.add_StudentTableAdapter.Fill(this.schoolDataSet.Add_Student);

}



private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string strconnection = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Abbii\Documents\School.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection connection = new SqlConnection(strconnection);

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select Id,F_NIC,Name,Class FROM Add_Student WHERE Class='"+comboBox1.SelectedItem +"'";
cmd.Connection = connection;
DataSet dataset=new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

adapter.Fill(dataset);
dataGridView1.DataSource = dataset.Tables[0];

}

private void button1_Click_1(object sender, EventArgs e)
{
scb = new SqlCommandBuilder(adapter);//nullreference exception was unhandeld.
adapter.Update(dataset);

}


}


}
Posted
Updated 12-Jun-15 9:16am
v2
Comments
virusstorm 12-Jun-15 14:54pm    
What is your exception?
Member 11762109 12-Jun-15 16:17pm    
An unhandled exception of type 'System.NullReferenceException' occurred in School.exe

Additional information: Object reference not set to an instance of an object
virusstorm 12-Jun-15 16:19pm    
Which statement? We are willing to help, but you got to be able to describe the problem in detail.
Member 11762109 12-Jun-15 16:21pm    
i'm really sorry sir,i'm beginner.error is showing on this line
scb = new SqlCommandBuilder(adapter);
as i have mentioned by comment in my code.

virusstorm 12-Jun-15 16:31pm    
Understood.

So your issue is actually self explanatory. Your "adapter" object that you are passing into SqlCommandBuilder is null. You never actually instantiated or set it to value.

Looking your code, there is a sequence that needs to take place. You need to for "adapter" to have a value. The value in "comboBox1" needs to change in order for the correct event to fire to set the value. I see bigger problems with your code, however. What is the goal you are trying to achieve? I might be able to put together a better prototype for you that could put you a better path.

1 solution

As per my Knowledge, change the comboBox1_SelectedIndexChanged function as you are using local variables with same name as the global variable. Please change the code as per below implemented function. It will work.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string strconnection = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Abbii\Documents\School.mdf;Integrated Security=True;Connect Timeout=30";
SqlConnection connection = new SqlConnection(strconnection);

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select Id,F_NIC,Name,Class FROM Add_Student WHERE Class='"+comboBox1.SelectedItem +"'";
cmd.Connection = connection;
dataset=new DataSet();
adapter = new SqlDataAdapter(cmd);

adapter.Fill(dataset);
dataGridView1.DataSource = dataset.Tables[0];

}

private void button1_Click_1(object sender, EventArgs e)
{
scb = new SqlCommandBuilder(adapter);//nullreference exception was unhandeld.
adapter.Update(dataset);

}
 
Share this answer
 
Comments
Member 11762109 14-Jun-15 16:44pm    
Thankyou so much sir :)

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