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>