| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 627 人关注过本帖
标题:未将对象引用设置到对象的实例
只看楼主 加入收藏
ybh24138227
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2014-12-8
结帖率:81.82%
收藏
已结贴  问题点数:20 回复次数:3 
未将对象引用设置到对象的实例
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=&quot;data source=PC-20141020ARNY;initial catalog=检测试验;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

求解,(1)哪里不对,(2)应该怎么修改,(3)为什么这么修改
谢谢!
搜索更多相关主题的帖子: private public 
2014-12-11 22:53
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:20 
自己不会调试么

Maybe
2014-12-12 08:20
ybh24138227
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2014-12-8
收藏
得分:0 
额,刚开始学,不太懂!您能解释一下吗?

在校学生
2014-12-12 19:06
ybh24138227
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2014-12-8
收藏
得分:0 
好了

在校学生
2014-12-12 20:05
快速回复:未将对象引用设置到对象的实例
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015714 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved