NULL参考异常是未处理的 [英] NULL REFERENCE EXCEPTION WAS UNHANDLED

查看:85
本文介绍了NULL参考异常是未处理的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个app.config文件并将数据源放在那里,因为它在不久的将来会发生变化,并且我一直得到空引用异常未处理。



使用new关键字作为对象实例



但我已经导入了使用系统。配置



但仍然。我试图赶上但似乎没有工作



I created an app.config file and put the datasource in there incase it changes in the near future and i keep getting the null reference exception was unhandled.

use the new keyword as an object instance

but i have already imported the using system.configuration

and yet still. i have tried to catch but it seems not to work

public partial class LoginForm : Form
    {
        //Main mnFrm;
        SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());//the exception is thrown on this line when I run the program
        
        SqlDataReader sdr;
        SqlCommand sqlCmd = new SqlCommand();

        public LoginForm(Main mnFrm)
        {
            //this.mnFrm = mnFrm;
            InitializeComponent();
        }





我将非常感谢帮助人员提前感谢



I would appreciate the help guys thanks in advance

推荐答案

请参阅:如何获取c#中App.Config的连接字符串 [ ^ ]。



另请查看:

MSDN:存储和检索连接字符串 [ ^ ]

在App.Config文件中配置连接字符串在运行时期间对于C#Windows应用程序 [ ^ ]





- Amit
Please see : How to get Connection String from App.Config in c#[^].

Also check :
MSDN : Storing and Retrieving Connection Strings [^]
Configuring Connection String in App.Config File During Runtime For a C# Windows Application[^]


--Amit


qlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());//the exception is thrown on this line when I run the program




你写的这行中的
你的配置文件中不存在密钥DbConn你是否在那里写错了。



in this line you write Key "DbConn" dose not exist in your config file are you write wrong spell there.


您的配置文件的ConnectionStrings部分中是否有名为DbConn的连接?
Do you have a connection called "DbConn" in the ConnectionStrings section of your config file?


这篇关于NULL参考异常是未处理的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆