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

问题描述

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元素下。

时间: 2024-09-20 08:02:12

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

“System.ArgumentException”类型的未经处理的异常出现在 mscorlib.dll 中。 其他信息: 实例方法的委托不能具有空“this

问题描述 本人在做数据库课设,没有学过C#,只学过C和C++,做一个简易聊天室今天在连接数据库时,调试出了问题,而错误列表没有出,不知何处下手,求C#大神帮忙以下是代码namespace登录界面{partialclassForm1{///<summary>///必需的设计器变量.///</summary>privateSystem.ComponentModel.IContainercomponents=null;///<summary>///清理所有正在使用的资源.//

急求大神帮助!!C#调用C++dllSystem.AccessViolationException”类型的未经处理的异常出现在 mscorlib.dll 中。

问题描述 "System.AccessViolationException"类型的未经处理的异常出现在mscorlib.dll中.其他信息:尝试读取或写入受保护的内存.这通常指示其他内存已损坏.代码如下//////C++dll代码HWNDhWnd;extern"C"__declspec(dllexport)voidcOSG(HWNDmhWnd){hWnd=mhWnd;}extern"C"__declspec(dllexport)voidInitO

System.NullReferenceException”类型的未经处理的异常

问题描述 各位大神,在按钮中,为什么会报错.但是结果照样会写到txt中,而MessageBox.Show就显示不出来了,直接报错,求如何改进的办法.谢谢privatevoidbutton3_Click(objectsender,EventArgse){using(StreamWritersk=newStreamWriter(textBox4.Text+"Export_To_Text.txt",false,Encoding.GetEncoding("Unicode")

“System.NullReferenceException”类型的未经处理的异常

问题描述 这个情况怎么处理啊是不是调用这边有问题?privatevoidbutton2_Click(objectsender,EventArgse){DataToExcel(dataGridView1);} 解决方案 本帖最后由 amoypower 于 2016-06-01 14:54:44 编辑解决方案二:可能dgv[i,j].Value为null了.解决方案三:privatevoiddataGridView1_RowPostPaint(objectsender,DataGridViewRow

“System.StackOverflowException”类型的未经处理的异常出现在 System.Drawing.dll 中。

问题描述 以上是我的程序,然后一运行出错,错误如下:经常会出现这样的错误System.Data.SQLite.SQLiteException(0x8000405):outofmemory有的时候又能运行成功,但是一直找不到解决的办法?也知道具体哪儿错误了? 解决方案 解决方案二:还是调试一下你的for循环,错误是堆栈溢出,估计for循环出不来了

“System.Data.OleDb.OleDbException”类型的未经处理的异常出现在 LSCommonHelper.dll 中。

问题描述 publicstaticDataSetgetDataSet(stringsqlStr,stringsTable){DataSetset2;OleDbConnectionselectConnection=getConn();try{//selectConnection.Open();OleDbDataAdapteradapter=newOleDbDataAdapter(sqlStr,selectConnection);DataSetdataSet=newDataSet();if(data

System.AccessViolationException类型的未经处理的异常在System.Data.dll中发

System.AccessViolationException"类型的未经处理的异常在 System.Data.dll 中发生.其他信息:尝试读取或写入受保护的内存.这通常指示其他内存已损坏. 错误背景: 操作系统:编程环境:VS2013:  语言:VB.net:  数据库:SQLserver2008 做数据库连接时,发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据库 第一种情况: 连接字符串为"Server=(Local);Database=charge_s

“System.IO.IOException”类型的未经处理的异常出现在 System.dll 中。

问题描述 "System.IO.IOException"类型的未经处理的异常出现在System.dll中.其他信息:连到系统上的设备没有发挥作用."System.IO.IOException"类型的未经处理的异常出现在System.dll中.其他信息:端口"COM1"不存在. 解决方案 解决方案二:不要光丢两个错误信息出来,不放代码,鬼知道你干什么了解决方案三:而且既然报错告诉你端口COM1不存在了,就到设备管理器里看端口号到底是COM几不存在你

未处理的“System.NullReferenceException”类型的异常出现在 Stranded.exe 中,急

问题描述 出现了错误:未处理的"System.NullReferenceException"类型的异常出现在Stranded.exe中.其他信息:未将对象引用设置到对象的实例.为什么?高手们说说.这是有关一个directx的一个例子.提示出错的地方在:m_device->SetProperty(DIPROP_RANGE,&range.diph);/////////CGameController::CGameController(LPDIRECTINPUT8input,HW