By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

bind data to textboxes

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
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
Nov 19 '05 #2

P: n/a
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.

"John Saunders" wrote:
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
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

Nov 19 '05 #3

P: n/a
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
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
Nov 19 '05 #4

P: n/a
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

"John Saunders" wrote:
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
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

Nov 19 '05 #5

P: n/a
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:C8**********************************@microsof t.com...
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
Nov 19 '05 #6

P: n/a
In this scenario then how would i do that?

would it be
dim conn as new oracleconnection = dbConn.Function or another way?
"John Saunders" wrote:
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:C8**********************************@microsof t.com...
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

Nov 19 '05 #7

P: n/a
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:0E**********************************@microsof t.com...
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
Nov 19 '05 #8

P: n/a
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.
"John Saunders" wrote:
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:0E**********************************@microsof t.com...
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

Nov 19 '05 #9

P: n/a
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:2E**********************************@microsof t.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
Nov 19 '05 #10

P: n/a
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

"John Saunders" wrote:
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:2E**********************************@microsof t.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

Nov 19 '05 #11

P: n/a
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.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" <Mi**@discussions.microsoft.com> wrote in message
news:2E**********************************@microsof t.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

Nov 19 '05 #12

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.