|
Subject:
|
Bind a Label
|
|
Posted By:
|
cp75
|
Post Date:
|
1/3/2007 7:37:47 AM
|
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
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/3/2007 8:31:49 AM
|
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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/3/2007 3:32:51 PM
|
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
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/3/2007 4:13:10 PM
|
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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/9/2007 11:31:54 AM
|
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.
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/9/2007 11:36:23 AM
|
Can i bind a label without being in a datagrid, datalist or repeater
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/9/2007 12:15:51 PM
|
Now I'm getting
BC30311: Value of type 'System.Data.OleDb.OleDbDataReader' cannot be converted to 'String'.
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/9/2007 1:21:45 PM
|
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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/10/2007 8:27:22 AM
|
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.
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/10/2007 8:36:45 AM
|
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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/10/2007 8:42:33 AM
|
I'm getting the following error BC30408: Method 'Public Function GetSalesman() As Object' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/10/2007 8:45:41 AM
|
Are you using Addhandler somewhere?
------------------------- 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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/10/2007 8:47:09 AM
|
No, What is that?
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/10/2007 9:01:38 AM
|
On your front end do this:
<asp:Label id=lbl2 runat=server />
On your backend do this: Private Sub Page_Load(ByVal sender as Object, ByVal e as EventArgs) If not Page.IsPostBack lbl2.Text = GetSalesman() End If End Sub
Ill explain Addhandler after we deal with this problem.
------------------------- 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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/10/2007 9:05:00 AM
|
Now I'm getting System.InvalidCastException: Cast from type 'DataSet' to type 'String' is not valid
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/10/2007 9:11:37 AM
|
....so change the signature of your function to [function declaration] as string and return a value from your dataset.
------------------------- 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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/10/2007 9:26:35 AM
|
Something like this:
Function GetSalesman (ByVal FieldName As String) As String
End Function
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/10/2007 9:29:21 AM
|
Yes
------------------------- 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
|
|
Reply By:
|
cp75
|
Reply Date:
|
1/10/2007 9:44:06 AM
|
I'm getting:
BC30311: Value of type 'System.Data.DataSet' cannot be converted to 'String'
Dim a As String Sub Page_Load(sender As Object, e As EventArgs) If not Page.IsPostBack a = Context.User.Identity.Name lbl2.Text = GetVendedor(a) End If End Sub
Function GetVendedor (ByVal FieldName As String) As String //some code End Function
|
|
Reply By:
|
dparsons
|
Reply Date:
|
1/10/2007 9:53:16 AM
|
You cannot just return your dataset as I imagine you are doing something like this:
Return ds
That is why you are getting the error. You have to access your Dataset, then the datatable contained inside the dataset, and then retrieve the value that you want to work with.
ds.Tables[0].Rows[0].Item["columnname"]
------------------------- 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
|