bind data to textboxes

G

Guest

I'm calling a component to get my data. The component is returning a
dataset. I need to populate text boxes on my aspx page with the data within
the datalist.
Where can I find an example that does this?

Here is the code i'm using and i'm gettting an error:
code:

Try
dbConn.GetCarInformation(CarID)
Dim cmd As Oracle.DataAccess.Client.OracleCommand
Dim da As Oracle.DataAccess.Client.OracleDataReader
da = cmd.ExecuteReader(CommandBehavior.SingleRow)
If da.Read Then
txtCarName.Text = da("Model").ToString
End If
Catch ex As Exception
lblError.Text = "Error: " & ex.Message
End Try

error:

Object reference not set to an instance of an object.


what am i doing wrong? I don't want to use a datalist i just want to
populate the textboxes
 
J

John Saunders

Mike said:
I'm calling a component to get my data. The component is returning a
dataset. I need to populate text boxes on my aspx page with the data
within
the datalist.
Where can I find an example that does this?

Here is the code i'm using and i'm gettting an error:
code:

Try
dbConn.GetCarInformation(CarID)
Dim cmd As Oracle.DataAccess.Client.OracleCommand

You never created an instance. Try:

Dim cmd As New Oracle.DataAccess.Client.OracleCommand

John Saunders
 
G

Guest

I have this now:
Try
dbConn.GetCarInformation(CarID)
Dim cmd As Oracle.DataAccess.Client.OracleCommand
Dim da As Oracle.DataAccess.Client.OracleDataReader
cmd = new Oracle.DataAccess.Client.OracleCommand
da = cmd.ExecuteReader(CommandBehavior.SingleRow)
If da.Read Then
txtCarName.Text = da("Model").ToString
End If
Catch ex As Exception
lblError.Text = "Error: " & ex.Message
End Try

and now i'm getting this error:
Error: Operation is not valid due to the current state of the object.
 
J

John Saunders

Mike said:
I have this now:
Try
dbConn.GetCarInformation(CarID)
Dim cmd As Oracle.DataAccess.Client.OracleCommand
Dim da As Oracle.DataAccess.Client.OracleDataReader
cmd = new Oracle.DataAccess.Client.OracleCommand
da = cmd.ExecuteReader(CommandBehavior.SingleRow)
If da.Read Then
txtCarName.Text = da("Model").ToString
End If
Catch ex As Exception
lblError.Text = "Error: " & ex.Message
End Try

and now i'm getting this error:
Error: Operation is not valid due to the current state of the object.

Don't you need an open OracleConnection in cmd.Connection?

John Saunders
 
G

Guest

the dbConn.function name has the connection string in it.

the code works when i want to populate a datagrid or datalist, its when i
need to populate text boxes it breaks. I can also create a data set from the
function call and write out the dataset to the screen in XML form. I'm
missing something to populate text boxes with this
 
J

John Saunders

Mike said:
the dbConn.function name has the connection string in it.

That may be, but you need cmd.Connection to be set to an open connection.

John Saunders
 
G

Guest

In this scenario then how would i do that?

would it be
dim conn as new oracleconnection = dbConn.Function or another way?
 
J

John Saunders

Mike said:
In this scenario then how would i do that?

would it be
dim conn as new oracleconnection = dbConn.Function or another way?

You'll have to talk to your people to ask them where to get the connection
string from. You said that there was a connection inside of
dbConn.something. But once you get an OracleConnection, you have to set
cmd.Connection to that connection, before executing the command with
cmd.Execute<whatever>.

Setting the Connection is how the OracleCommand object knows which database
to execute the command on.

John Saunders
 
G

Guest

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.
 
J

John Saunders

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: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
 
G

Guest

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
 
J

John Saunders

Mike said:
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
 
R

Ravichandran J.V.

It has got nothing to do with a datalist. You have simply not
instantiated the command object.

Dim cmd As Oracle.DataAccess.Client.OracleCommand

Modify it to

Dim cmd As New Oracle.DataAccess.Client.OracleCommand

and specify the command and the connection for this object.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top