Re: bind data to textboxes

From: John Saunders (johnwsaundersiii)
Date: 12/18/04


Date: Fri, 17 Dec 2004 19:53:35 -0500


"Mike" <Mike@discussions.microsoft.com> wrote in message
news:71B18A09-5556-4320-B006-4FB2CC263004@microsoft.com...
> Now what you show below is not really any different then what i already
> have,
> the only difference is that i created a Component in my project instead of
> a
> class

1) A component is a class
2) If you have a component which exposes the data, then why can't you use it
to fill your textboxes?

John Saunders

> "John Saunders" wrote:
>
>> "Mike" <Mike@discussions.microsoft.com> wrote in message
>> news:2E1EF396-0B22-4CB0-8F49-993C62C3EB7D@microsoft.com...
>> >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:DataGrid runat="server" id="grdChildren" DataSource="<%#
>> _dataLayer.Data %>", DataMember="Child">
>> ....
>> </asp:DataGrid>
>>
>>
>> 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
>>
>>
>>



Relevant Pages

  • Re: weird problem with datareader
    ... > Public Sub New ... > ' some code doing openning connection ... > Protected Overrides Sub Finalize() ...
    (microsoft.public.dotnet.languages.vb)
  • weird problem with datareader
    ... Public Sub New ... ' some code doing openning connection ... Private Function executeSQL_Q(ByVal arg_SQL As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Dispose, Dispose(true), Finalize, IDisposable
    ... Basically, my only issue, is to make sure the Data Base connection ... Private Conn As SqlConnection = Nothing ... Public Sub OpenConnection() ... Public Sub CloseConnection() ...
    (microsoft.public.dotnet.general)
  • VB listView and Null values
    ... Ok i'm trying to populate my list view from a table that has 56 ... Public Sub fill_list ... If rs.RecordCount = 0 Then Exit Sub ...
    (microsoft.public.vb.controls)
  • Re: RE: User Forms
    ... I have the FillCboBoxList sub in the userform module. ... ComboBox named: ComboBox1 ... click on, say, letter A, I want the control to populate my cbo box with ... Right-Click on the Excel icon ...
    (microsoft.public.excel.newusers)

Loading