问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsFormsApplication2{publicpartialclassForm2:Form{SqlDataReaderdr;publicForm2(){InitializeComponent();}privatevoidbutton2_Click(objectsender,EventArgse){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();}}publicSqlConnectionGetConnection(){stringconnText=ConfigurationManager.ConnectionStrings["MyTestConnectionString"].ConnectionString;SqlConnectionsqlconn=newSqlConnection(connText);returnsqlconn;}privatevoidbutton3_Click(objectsender,EventArgse){SqlConnectionsqlconn=GetConnection();SqlCommandsqlcmd=sqlconn.CreateCommand();sqlcmd.CommandText="deletefromstudentswherestuname='"+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();}}publicvoidGetTable(){SqlConnectionsqlconn=GetConnection();SqlCommandsqlcmd=sqlconn.CreateCommand();sqlcmd.CommandText="select*fromStudents";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();}}privatevoidbutton1_Click(objectsender,EventArgse){GetTable();}privatevoidForm2_Load(objectsender,EventArgse){//GetTable();}}}这个程序哪错了?
解决方案
解决方案二:
空指针异常,你打个断点,调试调试不就知道了么?谁会帮你看这么多代码,要学会自己学习
解决方案三:
打了,没发现啊,
解决方案四:
有地方对象为空,自己检查下。可能是stringconnText=ConfigurationManager.ConnectionStrings["MyTestConnectionString"].ConnectionString;;这句
解决方案五:
如是那怎么改?
解决方案六:
确保配置文件中存在名叫MyTestConnectionString的元素,而且须在connectionstrings元素下。