我想更新gridview中的数据,但面临异常。 [英] i want to update data in gridview,but facing exception .
问题描述
使用System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks ;
使用System.Windows.Forms;
使用System.Data.SqlClient;
名称空间学校
{
公共部门班级:表格
{
>
SqlCommandBuilder scb;
SqlDataAdapter适配器;
DataSet数据集;
公共课程()
{
InitializeComponent();
}
private void Classes_Load(object sender,EventArgs e)
{
// TODO:这行代码加载将数据导入'schoolDataSet.Add_Student'表。您可以根据需要移动或移除它。
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超时= 30;
SqlConnection连接=新的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(数据集);
dataGridView1.DataSource = dataset.Tables [0];
}
private void button1_Click_1(object sender,EventArgs e)
{
scb = new SqlCommandBuilder(adapter); // nullreference exception is unhandeld。
adapter.Update(dataset);
}
}
}
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);
}
}
}
推荐答案
根据我的知识,更改comboBox1_SelectedIndexChanged函数,因为您使用的是与全局变量同名的局部变量。请根据以下实现的功能更改代码。它会工作。
private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
string strconnection = @Data Source =(LocalDB)\ v11.0; AttachDbFilename = C:\ Users \Abbii \ Files \ 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 is unhandeld。
adapter.Update(dataset);
}
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);
}
这篇关于我想更新gridview中的数据,但面临异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!