Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 3rd, 2007, 07:37 AM
Authorized User
 
Join Date: Aug 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Bind a Label

I'm new in ASP. Can I bind a Label without be inside datalist, datagrid?. I wrote a function that retrieves "salesman name" from an access database but somehow retrieves "System.Data.OleDb.OleDbDataReader" and not the data.

<asp:Label id="lbl2" runat="server" text=<%# GetSalesman()%>/>

Can I do this without the use of datalist, repeater or datagrid.????

Thanksss.
C



  #2 (permalink)  
Old January 3rd, 2007, 08:31 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

What does your function return?

-------------------------
I will only tell you how to do it, not do it for you.††
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
  #3 (permalink)  
Old January 3rd, 2007, 03:32 PM
Authorized User
 
Join Date: Aug 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Public Function GetSalesman()

Dim myConn AS OleDbConnection
Dim myCommand As OleDbCommand
Dim myReader As OleDbDataReader
Dim query As String
'specify the data source
myConn = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("/db/uoi.mdb"))
'define the command query
query = "SELECT Distinct Nro FROM Clients,Salesman WHERE Salesman.Username='" & Context.User.Identity.Name &"'"

'create a command object and set its
myCommand = new OleDbCommand ( query, myConn )

'open the connection and instantiate a datareader
myConn.Open ( )
myReader = myCommand.ExecuteReader ( )

return myReader
'close the reader and the connection
myReader.Close ( )
myConn.Close ( )

End Function

  #4 (permalink)  
Old January 3rd, 2007, 04:13 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

First, your function is wrote incorrectly as these 2 lines

myReader.Close ( )
myConn.Close ( )

Will never get called. Move them above your return statement. If you want to bind the label to the result of that function, return a string. So something like:

Dim sString as string = myReader("column")

return sString

-------------------------
I will only tell you how to do it, not do it for you.††
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
  #5 (permalink)  
Old January 9th, 2007, 11:31 AM
Authorized User
 
Join Date: Aug 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm still getting

Object reference not set to an instance of an object.


I move around those 2 lines you told me and add the string variable.


  #6 (permalink)  
Old January 9th, 2007, 11:36 AM
Authorized User
 
Join Date: Aug 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can i bind a label without being in a datagrid, datalist or repeater

  #7 (permalink)  
Old January 9th, 2007, 12:15 PM
Authorized User
 
Join Date: Aug 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now I'm getting

BC30311: Value of type 'System.Data.OleDb.OleDbDataReader' cannot be converted to 'String'.



  #8 (permalink)  
Old January 9th, 2007, 01:21 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Here is the problem: a datareader only contains data while you have a connection open to the database so returning a datareader from a function does you no good at all. You either A) have to return a datatable/set or B) Return a string value from your query. You will NOT be able to access any data outside of the above function using a datareader.

-------------------------
I will only tell you how to do it, not do it for you.††
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
  #9 (permalink)  
Old January 10th, 2007, 08:27 AM
Authorized User
 
Join Date: Aug 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I create a function with a dataset but i'm getting Compiler Error Message: BC30408: Method 'Public Function GetSalesman(sender As Object, e As System.EventArgs) As Object' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.
--------------------------------------
Should i call the method as
Option1)
<asp:Label id="lbl2" runat="server" Text=<%#"GetSalesman"%>
or
Option2)
<asp:Label id="lbl2" runat="server" OnInit="GetVendedor">
If i do as option2 I get the above compiler error.

  #10 (permalink)  
Old January 10th, 2007, 08:36 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Whats wrong with just doing:

Private Sub Page_Load(ByVal sender as Object, ByVal e as EventArgs)
If not Page.IsPostBack
     lbl2.Text = GetSalesman()
End If
End Sub

-------------------------
I will only tell you how to do it, not do it for you.††
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Bind Data in DataGrid akumarp2p C# 2005 2 December 30th, 2006 11:11 AM
Grid Bind msbsam ASP.NET 2.0 Basics 1 December 19th, 2006 06:03 AM
Bind Data to a Single Label Control: Possible? jumpseatnews ASP.NET 2.0 Basics 1 April 6th, 2006 11:12 PM
how to bind a data() lsxx Classic ASP Components 4 April 20th, 2005 02:35 AM
bind variable htummala Oracle 1 February 15th, 2005 04:32 AM



All times are GMT -4. The time now is 07:42 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.