Question

Connection String (Conn) is set globally on page load.

(As below. I use this for all sqldatasources and it work perfectly.)

VB.NET (Page Load)

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

How to set a nested datasource from code behind?

I have a nested gridview inside a repeater and am trying to set the sqldatasource's connectionstring on itemdatabound instead of page load. (As below.)

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"/>

This code outputs error:

The ConnectionString property has not been initialized.


TRIED TO CREATE FUNCTION TO PASS IN THE CONNECTION STRING

ASPX

Replaceed above sqlsource with this to call function

<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
Was it helpful?

Solution

This is just an idea, but the problem might be the fact that by the time ItemDataBound is called GridView's data binding is already done, or was attempted to be done. And during this attempt a connection string was not set indeed.

To correct that you might want to do two things. First - set connection string in markup, so that data source has it before data binding is done. Something like this:

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

Where GetConnectionString is a protected method in code behind that just returns the connection string:

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

Of course that means that there is no need to handle ItemDataBound anymore, unless you were doing something else there as well.

Second - reverse the order of controls inside the item template. This most likely is not necessary, but I am not entirely sure about an exact time when the GridView will be data bound, so this will help to ascertain that connection string is set:

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

OTHER TIPS

In Your Class Library

Use this to cast initially the sqlConnection then execute the connection when using it.

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

End Class

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("INSERT SOMETHING")

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top