如何从代码后面设置嵌套数据源?

时间:2014-05-07 10:42:46

标签: asp.net vb.net gridview repeater sqldatasource

连接字符串(Conn)在页面加载时全局设置。

(如下所示。我对所有sqldatasources都使用它,它可以很好地工作。)

VB.NET(页面加载)

Imports Connections
Dim Conn As New System.Data.SqlClient.SqlConnection

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Conn.ConnectionString = ConfigurationManager.ConnectionStrings(StringConn).ConnectionString
  sqlRepeater.ConnectionString = Conn.ConnectionString
End Sub

如何从代码后面设置嵌套数据源?

我在转发器中有一个嵌套的gridview,我试图在itemdatabound而不是page load上设置sqldatasource的connectionstring。 (如下所示。)

VB.NET(Repeater Item Databound)

Protected Sub rpt_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
  If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    Dim sqlGridview As SqlDataSource = DirectCast(e.Item.FindControl("sqlGridview"), SqlDataSource)
    sqlGridview.ConnectionString = Conn.ConnectionString
  End If
End Sub

ASPX

<asp:Repeater ID="rpt" runat="Server" DataSourceID="sqlRepeater">
  <ItemTemplate>

    <asp:GridView ID="gv" runat="Server" AutoGenerateColumns="False" DataSourceID="sqlGridview">
      ...
    </GridView>

    <asp:SqlDataSource ID="sqlGridview" runat="Server" SelectCommand="sp" SelectCommandType="StoredProcedure"/>

  </ItemTemplate>
</asp:Repeater>

<asp:SqlDataSource ID="sqlRepeater" runat="Server" SelectCommand="sp" SelectCommandType="StoredProcedure"/>

此代码输出错误:

尚未初始化ConnectionString属性。


试图在连接线中创建功能

ASPX

将上面的sqlsource替换为调用函数

<asp:SqlDataSource ID="sqlGridview" runat="Server" SelectCommand="sp" SelectCommandType="StoredProcedure" ConnectionString='<%# GetConnectionString()%>'/>

VB.NET

Protected Function GetConnectionString() As String
  Dim Conn As New System.Data.SqlClient.SqlConnection
  Conn.ConnectionString = ConfigurationManager.ConnectionStrings(StringConn).ConnectionString
  Return Conn.ConnectionString
End Function

2 个答案:

答案 0 :(得分:1)

这只是一个想法,但问题可能是,当ItemDataBound被称为GridView时,数据绑定已经完成,或者试图完成。在此尝试期间,确实未设置连接字符串。

要纠正您可能想做两件事。首先 - 在标记中设置连接字符串,以便数据源在数据绑定完成之前使用它。像这样:

<asp:SqlDataSource ID="sqlGridview" runat="Server"
                   ConnectionString='<%# GetConnectionString(); %>' ... />

其中GetConnectionString是代码隐藏的受保护方法,只返回 连接字符串:

Protected Function GetConnectionString() As String
    Return Conn.ConnectionString
End Function

当然,这意味着不再需要处理ItemDataBound,除非您在那里做其他事情。

其次 - 反转项目模板中控件的顺序。这很可能不是必需的,但我不完全确定GridView将被数据绑定的确切时间,因此这将有助于确定连接字符串已设置:

<ItemTemplate>
    <asp:SqlDataSource ID="sqlGridview" runat="Server"
    <asp:GridView ID="gv" runat="Server" ...
</ItemTemplate>

答案 1 :(得分:0)

在您的班级图书馆

使用它来初始化sqlConnection,然后在使用时执行连接。

Public Class MyLibrary

'-------------------SQL Database Variables----------------
Dim SerialMACDatabase As String = Nothing
Dim SerialMac As SqlConnection
Dim CMD As New SqlCommand
Dim sqlAdapter As SqlDataAdapter

Public Sub New(ByVal lserialMACDatabase As String)
    SerialMACDatabase = lserialMACDatabase
End Sub

Private Sub connectToDB()
    Try
        SerialMac = New SqlConnection(SerialMACDatabase)
        SerialMac.Open()
    Catch ex As Exception
    End Try
End Sub 


Public Sub issueCommand(ByVal sQuery As String)
    Try
        connectToDB()
        CMD = New SqlCommand(sQuery, TestResult)
        CMD.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

结束班

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button1.Click

Dim Libraryy As New MyLibrary(strSQLCONNECTION)

Libraryy.IssueCommand(&#34; INSERT SOMETHING&#34;)

End Sub