未将对象引用设置到对象的实例
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace 作业00001
{
public partial class Form1 : Form
{
private SqlDataReader dr;
private DataSet ds;
private BindingSource bs;
public Form1()
{
InitializeComponent();
ds = new DataSet();
bs = new BindingSource();
}
private void shuxin(){string ssql="select * from student";
SqlDataAdapter ad = new SqlDataAdapter(ssql, ConfigurationManager.ConnectionStrings["Setting"].ConnectionString);
ds.Clear();
ad.Fill(ds,"student");
bs.DataSource=ds;
bs.DataMember="student";
dataGridView1.DataSource=bs;}
private void Form1_Load(object sender, EventArgs e)
{
shuxin();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true) { radioButton1.Text = "男"; }
else { radioButton1.Text = "女"; }
string ssql = "insert into student(sno,sname,sage,ssex,sdept) values('" + textBox1.Text + "','" + textBox2.Text + "'," + textBox3.Text + ",'" + radioButton1.Text + "','" + comboBox1.Text + "')";
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Settings"].ConnectionString);
cn.Open();
SqlCommand cmd=new SqlCommand(ssql,cn);
cmd.ExecuteNonQuery();
cn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string ssql="update student set ssage="+ textBox3.Text +",sname='"+ textBox2.Text +"',ssex='"+ radioButton1.Text +"',sdept='"+ comboBox1.Text +"' where sno='"+ textBox1.Text +"'";
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Settings"].ConnectionString);
cn.Open();
SqlCommand cmd=new SqlCommand(ssql,cn);
cmd.ExecuteNonQuery();
cn.Close();
}
private void button3_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["Settings"].ConnectionString);
SqlDataAdapter da=new SqlDataAdapter("select * from student where sno='"+ textBox1.Text +"'",cnn);
SqlCommandBuilder cmd=new SqlCommandBuilder(da);
DataSet ds=new DataSet();
da.Fill(ds,"student");
DataTable dt=ds.Tables[0];
DataRow dr=dt.Rows[0];
dr.Delete();
da.Update(ds,"student");
cnn.Close();
}
private void button4_Click(object sender, EventArgs e)
{
DataRow stuDR =ds.Tables["student"].NewRow();
stuDR["sno"] = textBox1.Text;
stuDR["sname"] = textBox2.Text;
stuDR["sage"] = textBox3.Text;
ds.Tables["student"].Rows.Add(stuDR);
string ssql = "select * from student";
SqlDataAdapter ad = new SqlDataAdapter(ssql, ConfigurationManager.ConnectionStrings["Settings"].ConnectionString);
SqlCommandBuilder scb = new SqlCommandBuilder(ad);
ad.Update(ds,"student");
}
private void bindingNavigator1_RefreshItems(object sender, EventArgs e)
{
}
private void bindingNavigator1_RefreshItems_1(object sender, EventArgs e)
{
}
}
}
APP文件<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="作业00001.Properties.Settings.Setting" connectionString="Data Source=PC-20141020ARNY;Initial Catalog=检测试验;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="检测试验Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=PC-20141020ARNY;initial catalog=检测试验;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
求解,(1)哪里不对,(2)应该怎么修改,(3)为什么这么修改
谢谢!