Mike said:
I wrote both pieces the component i'm using and the aspx app i'm writing.
Why do i need to have the connection string in the aspx (code behind)if
the
component is already has the connection string in it and works?
I don't see why databinding textboxes in asp.net is hell like this. the
old
way it was just using <%=dataitem%> and i was done. It appears in .NET is
like going through and act of congress to pop textboxes in .NET
If i'm calling a function that already populates a datagrid and has the
connection string in it, why do i need to create another connection string
to
populate textboxes from the same function. I'm lost on this one.
I haven't been discussing connection strings, I've been discussing
connections.
I think part of this is bad design. You've got a function that populates the
data grid with some data, then you need to use some of that data outside of
the function, in order to populate some text boxes. Do I have that right?
If so, you should encapsulate some of this into a class. The class would
have a method which would populate a DataSet object with all of the data you
need. It would then expose the DataSet as a read-only property of itself.
You could use the exposed DataSet to populate the DataGrid and also to
populate the text boxes:
Public Class MyDataLayer
Private _dataSet As New DataSet
Private _connectionString As String
Private _connection as OracleConnection
Public Sub New
_connectionString = Configuration.AppSettings("connectionString")
End Sub
Public Sub LoadData(parentId As Integer)
_connection.Open
Dim cmdFillParent As New OracleCommand("Select * from Parent where
Id=@Id", _connection)
cmdFillParent.Parameters("@Id").Value = parentId
Dim da As New OracleDataAdapter(cmdFillParent)
da.Fill(_dataSet, "Parent")
'
Dim cmdFillChildren As New OracleCommand("Select * from Child where
ParentId=@Id", _connection)
cmdFillChildren.Parameters("@Id").Value = parentId
da = New OracleDataAdapter(cmdFillChildren)
da.Fill(_dataSet, "Child")
End Sub
Public ReadOnly Property Data As DataSet
Get
Return _dataSet
End Get
End Property
End Class
In your .aspx.vb:
Protected _dataLayer As New MyDataLayer
Public Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
Dim id As Integer = Integer.Parse(Request("Id"))
_dataLayer.LoadData(id)
'
DataBind()
End If
End Sub
In your .aspx page:
<asp:TextBox runat="server" id="txtName"><%#
_dataLayer.Data.Tables("Parent").Rows(0)("Name") %></asp:TextBox>
....
<asp
ataGrid runat="server" id="grdChildren" DataSource="<%#
_dataLayer.Data %>", DataMember="Child">
....
</asp
ataGrid>
How's that? You only maintain the connection and the connection string in
one place, but you can use the data in multiple places. And if you need to
do something else to the data, you've still got the dataset and the
connection to the database inside of the MyDataLayer class, so you can just
add, for instance, an UpdateParent method.
John Saunders