'/'应用程序中的服务器错误。你调用的对象是空的。 [英] Server Error in '/' Application. Object reference not set to an instance of an object.

查看:83
本文介绍了'/'应用程序中的服务器错误。你调用的对象是空的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序无法从web.config文件中选择连接字符串。

引用:

服务器错误在'/'应用程序。



对象引用未设置为对象的实例。



描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.NullReferenceException:对象引用未设置为实例一个对象。





 <   connectionStrings  >  
< add name = myConnectionString connectionString = server = XYZ; database = nDatabase; uid = sa; password = sa1234; / >
< / connectionStrings >





请帮助

解决方案

对于网站或网络应用程序默认配置文件是web.config文件。当您调用 ConfigurationManager.ConnectionStrings 时,它将从您的默认配置文件中获取连接字符串,即web.config。如果你还没有在web.config中定义 myConnectionString ,请添加它并检查你的网站。


把你的连接字符串

 dbManager.ConnectionString = ConfigurationManager.ConnectionStrings [myConnectionString]。ConnectionString.ToString(); 


在Web中.Config

< connectionstrings> 
< add name = MyConnectionString connectionstring = 数据源= MyPc-PC;初始目录= MyDb;持久安全信息= True;用户ID = sa;密码= 12345 providername = System.Data.SqlClient />
< / connectionstrings >



你可以使用



  string  constr = WebConfigurationManager.ConnectionStrings [  webshopConnectionString]。的ConnectionString; 


My application is not able to pick connection string from web.config file.

Quote:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.



<connectionStrings>
      <add name="myConnectionString" connectionString="server=XYZ;database=nDatabase;uid=sa;password=sa1234;" />
  </connectionStrings>



Please help

解决方案

For web site or web application default configuration file is web.config file. When you call ConfigurationManager.ConnectionStrings it will get the connection strings from your default configuration file, that means web.config. if you haven't define myConnectionString in web.config, add it and check your site.


Put your connection string

dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString.ToString();


In Web.Config

<connectionstrings>
         <add name="MyConnectionString" connectionstring="Data Source=MyPc-PC;Initial Catalog=MyDb;Persist Security Info=True;User ID=sa;Password=12345" providername="System.Data.SqlClient" />
</connectionstrings>


And you can access using

string constr = WebConfigurationManager.ConnectionStrings["webshopConnectionString"].ConnectionString;


这篇关于'/'应用程序中的服务器错误。你调用的对象是空的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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