 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 3rd, 2007, 08:37 AM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

January 3rd, 2007, 09:31 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|

January 3rd, 2007, 04:32 PM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

January 3rd, 2007, 05:13 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|

January 9th, 2007, 12:31 PM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

January 9th, 2007, 12:36 PM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Can i bind a label without being in a datagrid, datalist or repeater
|
|

January 9th, 2007, 01:15 PM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Now I'm getting
BC30311: Value of type 'System.Data.OleDb.OleDbDataReader' cannot be converted to 'String'.
|
|

January 9th, 2007, 02:21 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|

January 10th, 2007, 09:27 AM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

January 10th, 2007, 09:36 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
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
|
|
 |