[VB]请问“未将对象引用设置到对象的实例”是什么样的错误?

singit3 2006-06-19 11:31:47
我按照书的测试一下如何读取config.web中的内容,但出错。
config.web中的内容
++++++++++++++++++++++++++++++
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<configsections>
<add name="test" type="System.Web.Configuration.DictionarySectionHandler" />
</configsections>
<test>
<add key="key" value="just a configure test" />
</test>
</configuration>
+++++++++++++++++++++++++++
读取页面的内容
+++++++++++++++++++++++++
<%@ Page Language="vb" Debug="true" %>


<script runat="server">
sub page_load(s as object ,e as eventargs)
Dim CfgSection As Hashtable = Context.GetConfig("test")
Dim Msg As String = cstr(CfgSection("key"))
response.write(Msg )
end sub
</script>
++++++++++++++++++++++++
提示错误: System.NullReferenceException: 未将对象引用设置到对象的实例。
指向:Dim Msg As String = cstr(CfgSection("key"))
求教了!!

...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
singit3 2006-06-27
  • 打赏
  • 举报
回复
怎么把config.web命名成web.config就可以?奇怪,这个命名有关系吗?
singit3 2006-06-27
  • 打赏
  • 举报
回复
谢谢,照你的代码,我编了个页面teamp.aspx
++++++++++++++++++++++
<%@ Page Language="vb" Debug="true" %>

haha

<script runat="server">
sub page_load(sender as object ,e as eventargs)
dim a as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString1")
dim b as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString2")
response.write(b)
end sub
</script>
+++++++++++++++++
可是为什么写不出来呢,是空白啊。
Eddie005 2006-06-19
  • 打赏
  • 举报
回复
正确的写法是:

Dim CfgSection As System.Collections.Specialized.NameValueCollection = System.Configuration.ConfigurationSettings.GetConfig("test")

Dim Msg As String = cstr(CfgSection("key"))
Eddie005 2006-06-19
  • 打赏
  • 举报
回复
System.Web.HttpContext.Current.GetConfig("test")得到的并不是一个hashtable对象,所以不能实现这个类型转换

那么CfgSection就是个为定义的对象,不能访问
Eddie005 2006-06-19
  • 打赏
  • 举报
回复
config.web中的内容
++++++++++++++++++++++++++++++
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<appSettings>
<add key="ConnectionString1" value="Provider=sqloledb;Data source=10.2.10.10;initial catalog=northwind;password=as;user id=sa;"/>
<add key="ConnectionString2" value="Provider=sqloledb;Data source=10.2.11.10;initial catalog=master;password=as;user id=sa;"/>
</appSettings>
</configuration>
++++++++++++++++++++++++++++++++++++

dim a as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString1")
dim b as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString2")
singit3 2006-06-19
  • 打赏
  • 举报
回复
这样吧,您就举个简单的例子,就是如何设置和读取连结数据库的语句就可以。
好像公用的项目也只有这项吧。
singit3 2006-06-19
  • 打赏
  • 举报
回复
谢谢您的回复。不过把您的语句原封不动放进去,还是会出错,还是第二行。
错误是一样的。
不知道您有没有试一下。

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧