32学生小白 2019-06-20 00:14 采纳率: 0%
浏览 281

这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 数据库
{
public partial class 工种种类 : Form
{
string connStr = "Data Source=.;Initial Catalog=db_person;User ID=sa;Password=1";
SqlConnection conn;
DataTable ds;
int flag = 0;
public 工种种类()
{
InitializeComponent();

    }

    public void gzlx_SelectionChanged(object sender, EventArgs e)
    {
        if (gzlx.CurrentRow != null)
        {

            tbxbh.Text = gzlx.CurrentRow.Cells[0].Value.ToString();
            tbxmc.Text = gzlx.CurrentRow.Cells[1].Value.ToString();
            tbxms.Text = gzlx.CurrentRow.Cells[2].Value.ToString();
        }
    }
    private void a()
    {
        buttj.Enabled = false;
        butxg.Enabled = false;
        butsc.Enabled = true;
        gbxDetailed.Enabled = false;
    }
    private void b()
    {
        buttj.Enabled = false;
        butxg.Enabled = false ;
        butsc.Enabled =false ;
        gbxDetailed.Enabled =true;           
    }

    private void 工种种类_Load_1(object sender, EventArgs e)
    {
        conn = new SqlConnection(connStr);
        string sql = "select * from tb_JobInfo";
        SqlCommand sb = new SqlCommand(sql, conn);
        a();
        ds = new DataTable();
        conn.Open();
        SqlDataReader cnm = sb.ExecuteReader(CommandBehavior.CloseConnection);
        if (cnm.HasRows)
        {
            ds.Load(cnm);
            cnm.Close();
            gzlx.DataSource = null;
            gzlx.DataSource = ds;
        }

    }

    private void buttj_Click(object sender, EventArgs e)
    {
        b();
        tbxbh.Enabled = true;
        tbxbh.Focus();
    }

    private void butxg_Click(object sender, EventArgs e)
    {
        b();
        tbxbh.Enabled = false;
        tbxmc.Focus();
    }
    //删除
    private void butsc_Click(object sender, EventArgs e)
    {
        string sql = "delete from tb_JobInfo where JobID " + gzlx.CurrentRow.Cells[0].Value;
        SqlCommand cnm = new SqlCommand(sql, conn);
        conn.Open();
        conn.Close();
    }
    //添加
    private void btnSu_Click(object sender, EventArgs e)
    {
        if (flag == 1)
        {
        string sql = "insert into tb_JobInfo(JobID,JobName,Remark) lalues({0}',{1},{2}'{3}')";
        sql = string.Format(sql, Convert.ToInt16(tbxbh.Text.Trim()), tbxmc.Text.Trim(), tbxms.Text.Trim());
        SqlCommand cnm = new SqlCommand(sql, conn);
        conn.Open();
        conn.Close();
        工种种类_Load_1(null, null);
        a();
        }
        else if (flag == 2)
        {
        string sql = "update tb_JobInfo set JobID='{1}',JobName'{2}'where Remark {0}";
        sql = string.Format(sql, Convert.ToInt32(tbxbh.Text), tbxmc.Text.Trim(), tbxms.Text.Trim());
        SqlCommand cnm = new SqlCommand(sql, conn);
        conn.Close();
        }
        工种种类_Load_1(null, null);
        a();
        tbxbh.Enabled = true;
    }

    private void btncancel_Click(object sender, EventArgs e)
    {
        a();
    }
}

}图片说明


  • 写回答

2条回答 默认 最新

  • 1进击的小白 2019-06-20 09:04
    关注

    gzlx.CurrentRow.Cells[0].Value看下这个有没有值,如果为null会报这个错,没问题的话再看下gzlx.CurrentRow.Cells[0]的值有没有问题

    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建