System.NullReferenceException”类型的未经处理的异常出现在 WindowsFormsApplication2.exe 中。

时间:2021-05-10 20:32:20
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
  
    public partial class Form2 : Form
    {
        SqlDataReader dr;
        public Form2()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
               if (dr.Read())
               {
                 textBox1.Text = dr.GetString(1);
                 textBox2.Text = dr.GetString(2);
                 textBox3.Text = dr.GetValue(3).ToString();
                 textBox4.Text = dr.GetValue(5).ToString();
                 textBox5.Text = dr.GetValue(6).ToString();
                 textBox6.Text = dr.GetValue(4).ToString();
                }
                else
                {
                   GetTable();
               }
        }
        public SqlConnection GetConnection()
        {
            string connText = ConfigurationManager.ConnectionStrings["MyTestConnectionString"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(connText);
            return sqlconn;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection sqlconn = GetConnection();
            SqlCommand sqlcmd = sqlconn.CreateCommand();
            sqlcmd.CommandText = "delete from students where stuname='" + textBox1.Text.ToString() + "'";
            sqlconn.Open();
            sqlcmd.ExecuteNonQuery();
            sqlconn.Close();
            if (dr.Read())
            {
                textBox1.Text = dr.GetString(1);
                textBox2.Text = dr.GetString(2);
                textBox3.Text = dr.GetValue(3).ToString();
                textBox4.Text = dr.GetValue(5).ToString();
                textBox5.Text = dr.GetValue(6).ToString();
                textBox6.Text = dr.GetValue(4).ToString();
            }
            else
            {
                GetTable();
            }
        }

            public void GetTable()
            {
                SqlConnection sqlconn =GetConnection();
                SqlCommand sqlcmd= sqlconn.CreateCommand();
                sqlcmd.CommandText="select*from Students";
                sqlconn.Open();
                dr = sqlcmd.ExecuteReader();
                if (dr.Read())
               {
                 textBox1.Text = dr.GetString(1);
                 textBox2.Text = dr.GetString(2);
                 textBox3.Text = dr.GetValue(3).ToString();
                 textBox4.Text = dr.GetValue(5).ToString();
                 textBox5.Text = dr.GetValue(6).ToString();
                 textBox6.Text = dr.GetValue(4).ToString();
                }
            }

        private void button1_Click(object sender, EventArgs e)
        {
            GetTable();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //GetTable();
        }
        
    }
}


这个程序哪错了?

5 个解决方案

#1


空指针异常,你打个断点,调试调试不就知道了么?谁会帮你看这么多代码,要学会自己学习

#2


打了,没发现啊,

#3


有地方对象为空,自己检查下。可能是string connText = ConfigurationManager.ConnectionStrings["MyTestConnectionString"].ConnectionString;
;这句

#4


如是那怎么改?

#5


确保配置文件中存在名叫MyTestConnectionString的元素,而且须在connectionstrings元素下。

#1


空指针异常,你打个断点,调试调试不就知道了么?谁会帮你看这么多代码,要学会自己学习

#2


打了,没发现啊,

#3


有地方对象为空,自己检查下。可能是string connText = ConfigurationManager.ConnectionStrings["MyTestConnectionString"].ConnectionString;
;这句

#4


如是那怎么改?

#5


确保配置文件中存在名叫MyTestConnectionString的元素,而且须在connectionstrings元素下。