我有以下代码:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Dynamic Reports</title>
<script language="javascript" src="js/DynamicReports.js" type="text/javascript"></script>
</head>
<body onload="doSelect()">
<asp:DropDownList ID="ddlDynReports" runat="server" DataSourceID="sdsDynReports" DataTextField="DynReportName" DataValueField="DynReportID">
</asp:DropDownList>
<asp:SqlDataSource ID="sdsDynReports" runat="server" SelectCommand="select v.object_id as DynReportID, substring(v.name, 11, LEN(v.name)) as DynReportName from sys.views v where v.name like 'DynReport[_]%'">
</asp:SqlDataSource>
</body>
</html>
它在运行时抛出以下错误:
The ConnectionString property has not been initialized.
我的web.config文件似乎没问题,因为我在.vb代码中执行了许多其他sql调用。这是我第一次在.aspx代码中执行sql。
我认为我的.vb代码中可用的连接需要暴露给.aspx代码。
我愿意将一个将select移动到.vb代码的解决方案。
编辑:我发现我应该将ConnectionString="<%$ ConnectionStrings:constr %>"
添加到SqlDataSource元素,但问题是我的web.config文件中没有连接字符串。我的连接字符串位于名为ApplicationConfig.vb的文件中。
那么,如何将ApplicationConfig.vb中的连接字符串从.aspx代码中获取?
编辑:我这样做是因为它是我从select语句中填充下拉列表时找到的示例代码。我是一个vb.net菜鸟,所以我基本上是通过切割和粘贴来生存的。我们乐于接受对更好方法的建议,但如果示例代码不完整,我会迷失方向。示例:我在SO上找到的一些示例代码看起来就像我想要做的那样,但它是一系列剪辑 - 它并且我不知道将这些剪辑放在哪里。编辑:添加.vb代码:
Partial Class XXX_CommonPages_DynamicReports
Inherits System.Web.UI.Page
Protected Sub dynreport_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dynreports_run.ServerClick
Dim ViewObjectId As String = ddlDynReports.SelectedValue
Dim DataOut As String = "some,data,out"
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=DynReport_" + ViewObjectId + ".csv")
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Length", DataOut.Length())
Response.Write(DataOut)
' Response.End ' Causes ThreadAbortException.
End Sub
End Class
编辑:这是包含连接字符串的.vb代码:
Public Class ApplicationConfig
Public Class AppConfig
Implements IConfigurationSectionHandler
Private Const PRODUCTION_DATAACCESS_CONNECTIONSTRING_DEFAULT As String = "serve ..."
Public Shared ReadOnly Property ConnectionString() As String
Get
If fieldConnectionString Is Nothing Then fieldConnectionString = PRODUCTION_DATAACCESS_CONNECTIONSTRING_DEFAULT
Return fieldConnectionString
End Get
End Property
End Class
End Class