你好我试图在一个表单上处理多个数据库连接,我有一些代码,但运气不好。所以我的问题是实现try catch的最佳方法是什么,我将在何处调用该方法来处理连接,并举例说明我的代码。
<script runat="server">
Protected Sub SqlDataSource12_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
End Sub
Protected Sub loadMe(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("BODataConnectionString54").ConnectionString)
conn.Open()
conn.Close()
ListView3.DataSourceID = "SqlDataSource12"
Catch ex As System.Data.SqlClient.SqlException
error10.Visible = True
End Try
End Sub
Protected Sub ListView3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
</script>
我实现了loadMe到身体。
<asp:Content ID="Content2" ContentPlaceHolderID="topContent" Runat="Server" >
<body id="Body1" runat="server" onload="loadMe">
ListView3_SelectedIndexChanged到ListView。
<asp:ListView ID="ListView3" runat="server" ItemPlaceholderID="itemPlaceHolder1" DataSourceID="SqlDataSource12" OnSelectedIndexChanged="ListView3_SelectedIndexChanged">
SqlDataSource12_Selecting to SqlDataSource。
<asp:SqlDataSource ID="SqlDataSource12" runat="server" ConnectionString="
<%$ ConnectionStrings:BODataConnectionString54 %>"
SelectCommand="SELECT..... "
onselecting="SqlDataSource12_Selecting"
ProviderName="<%$ ConnectionStrings:BODataConnectionString54.ProviderName %>">
<SelectParameters>
最后我希望捕获的消息显示何时没有与数据库的连接。
<asp:Label runat="server" ID="error10" Text="Store could not be loaded" Visible="False" />
任何帮助都会非常感谢你。
答案 0 :(得分:0)
所以我发现我只需要一次尝试捕获“loadMe”,并在catch中添加“ListView3.Visible = False”并将“Visible =”True“”添加到“ListView3”。 这是一个例子
<script runat="server">
Protected Sub loadMe(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("BODataConnectionString54").ConnectionString)
conn.Open()
conn.Close()
ListView3.DataSourceID = "SqlDataSource12"
Catch ex As System.Data.SqlClient.SqlException
ListView3.Visible = False
error10.Visible = True
End Try
End Sub
</script>
..
...
....
<body id="Body1" runat="server" onload="loadMe">
..
...
....
<asp:ListView ID="ListView3" runat="server" Visible="True" ItemPlaceholderID="itemPlaceHolder1" DataSourceID="SqlDataSource12" >