简体   繁体   English

ConnectionString未初始化

[英]The ConnectionString not initialized

Good day All i have an issue with connection string I'm getting this exception 美好的一天,我的连接字符串有问题我遇到了这个异常

The ConnectionString property has not been initialized.

on the RowDataBound of the outer gridview sub routine (VB.NET) 在外部gridview子例程(VB.NET)的RowDataBound上

when trying to bind data to inner gridview 尝试将数据绑定到内部gridview时

the code: 编码:

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

This event occurs for each row 每行都会发生此事件

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

thanks (I'v been hunting this error for last 3 hours) 谢谢(我最近3个小时一直在寻找这个错误)

It looks like both code snippets use a separate connection string. 看起来这两个代码段都使用单独的连接字符串。 ChildDataSource uses "SiteConnectionString" and gvOdv_RowDataBound uses "MoyensAeriensConnectionString" , hopefully I'm not pointing out the obvious here, but if so, are both of those present in your config file? ChildDataSource使用"SiteConnectionString"gvOdv_RowDataBound使用"MoyensAeriensConnectionString" ,希望这里我没有指出明显的地方,但是如果是这样,您的配置文件中是否都存在这两个?

When you have created the SqlDataSource dynamically in your first code snippet, You haven't set its ConnectionString property, that's why this error is coming up. 在第一个代码段中动态创建了SqlDataSource ,您尚未设置其ConnectionString属性,这就是出现此错误的原因。

Note that you also haven't assigned any ID to your SqlDataSource. 请注意,您还没有为SqlDataSource分配任何ID。 Its better to do this too. 最好也这样做。 You also need to set the ConnectionString property of SqlDataSource . 您还需要设置SqlDataSourceConnectionString属性。

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

Rest of things should also be fine like: web.config has a connection string for the key mentioned [ eg ConnectionStr here] 其余的事情也应该像这样:web.config具有提到的键的连接字符串[例如,这里的ConnectionStr ]

Instead of returning a SQLDataSource as the gridview's datasource, perhaps return a dataset. 而不是返回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

The method to set the datasource of the child gridview remains the same. 设置子gridview的数据源的方法保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM