0

美好的一天我对连接字符串有疑问我收到了这个异常

The ConnectionString property has not been initialized.

在外部 gridview 子例程 (VB.NET) 的 RowDataBound 上

尝试将数据绑定到内部网格视图时

编码:

Private Function ChildDataSource(ByVal strCustometId As String, ByVal strSort As String) As SqlDataSource
    Dim strQRY As String = ""

    Dim connString As String = ConfigurationManager.ConnectionStrings("SiteConnectionString").ConnectionString

    Using conn As New SqlConnection(connString)
        conn.Open()

        strQRY = "SELECT [Sortie].[OdvID],[Sortie].[SortieID]," & "[Sortie].[Fuel],[Sortie].[Captain],[Sortie].[Crew] FROM [Sortie]" & " WHERE [Sortie].[OdvID] = '" & strCustometId & "'" & "UNION ALL " & "SELECT '" & strCustometId & "','','','','' FROM [Sortie] WHERE [Sortie].[OdvID] = '" & strCustometId & "'" & "HAVING COUNT(*)=0 " & strSort

        'Initialize command object
        Dim cmd As New SqlCommand(strQRY, conn)
        Dim dsTemp As New SqlDataSource()
        dsTemp.SelectCommand = strQRY
        Return dsTemp
    End Using

End Function

每一行都会发生此事件

Protected Sub gvOdv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    Dim connString As String = ConfigurationManager.ConnectionStrings("MoyensAeriensConnectionString").ConnectionString

    Dim conn As New SqlConnection(connString)
    conn.Open()


    Dim row As GridViewRow = e.Row
    Dim strSort As String = String.Empty

    ' Make sure we aren't in header/footer rows
    If row.DataItem Is Nothing Then
        Return
    End If

    'Find Child GridView control
    Dim gv As New GridView()
    gv = DirectCast(row.FindControl("gvSorties"), GridView)

    'Check if any additional conditions (Paging, Sorting, Editing, etc) to be applied on child GridView
    If gv.UniqueID = gvUniqueID Then
        gv.PageIndex = gvNewPageIndex
        gv.EditIndex = gvEditIndex
        'Check if Sorting used
        If gvSortExpr <> String.Empty Then
            GetSortDirection()
            strSort = " ORDER BY " & String.Format("{0} {1}", gvSortExpr, gvSortDir)
        End If

        'Expand the Child grid
        ClientScript.RegisterStartupScript([GetType](), "Expand", "<SCRIPT LANGUAGE='javascript'>expandcollapse('div" & DirectCast(e.Row.DataItem, DataRowView)("OdvID").ToString() & "','one');</script>")
    End If

    'Prepare the query for Child GridView by passing the Odv ID of the parent row

    gv.DataSource = ChildDataSource(DirectCast(e.Row.DataItem, DataRowView)("OdvID").ToString(), strSort)

    gv.DataBind()

    'Add delete confirmation message for Customer
    Dim l As LinkButton = DirectCast(e.Row.FindControl("linkDeleteCust"), LinkButton)
    l.Attributes.Add("onclick", "javascript:return " & "confirm('Are you sure you want to delete this Customer " & DataBinder.Eval(e.Row.DataItem, "OdvID") & "')")

End Sub

谢谢(过去 3 小时我一直在寻找这个错误)

4

3 回答 3

0

与其返回 SQLDataSource 作为 gridview 的数据源,不如返回一个数据集。

    Private Function ChildDataSource(ByVal strCustometId As String, ByVal strSort As String) As DataSet
    Dim strQRY As String = "SELECT [Sortie].[OdvID],[Sortie].[SortieID]," & "[Sortie].[Fuel],[Sortie].[Captain],[Sortie].[Crew] FROM [Sortie]" & " WHERE [Sortie].[OdvID] = '" & strCustometId & "'" & "UNION ALL " & "SELECT '" & strCustometId & "','','','','' FROM [Sortie] WHERE [Sortie].[OdvID] = '" & strCustometId & "'" & "HAVING COUNT(*)=0 " & strSort

    Dim connString As String = ConfigurationManager.ConnectionStrings("SiteConnectionString").ConnectionString

    Using conn As New SqlConnection(connString)
        conn.Open()
        Using da As New SqlDataAdapter(strQRY, conn)
            Using ds As New DataSet
                If da.Fill(ds) > 0 Then
                    Return ds
                Else
                    Return New DataSet
                End If
            End Using
        End Using
    End Using

End Function

设置子gridview的数据源的方法保持不变。

于 2013-09-07T04:59:40.467 回答
0

当您SqlDataSource在第一个代码片段中动态创建时,您尚未设置其 ConnectionString 属性,这就是出现此错误的原因。

请注意,您还没有为 SqlDataSource 分配任何 ID。最好也这样做。您还需要设置SqlDataSource的ConnectionString属性。

Dim dsTemp As New SqlDataSource()
dsTemp.ID = "mySqlSourceControl"
dsTemp.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionStr").ConnectionString
dsTemp.SelectCommand = strQRY
...

其余的事情也应该没问题,比如:web.config 有一个用于提到的键的连接字符串 [例如ConnectionStr 这里]

于 2013-09-07T01:51:19.803 回答
0

看起来两个代码片段都使用单独的连接字符串。ChildDataSourceuses"SiteConnectionString"gvOdv_RowDataBounduses "MoyensAeriensConnectionString",希望我没有在此处指出明显的内容,但如果是这样,您的配置文件中是否都存在这些内容?

于 2013-09-07T01:08:25.340 回答