Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a label a link

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
How can I make a asp:label web Control a link thats value comes from a database field?
 
why?? Use the hyperlink object That'l do donkey, that'l do
[bravo] Mark
 
yeah I just noticed it
When I try to set the text for that hyperlink object like this

superemail1.text = empDS.tables("employeeinfo").Rows(0).Item("supervisoremail")

it tells me I haven't declared superemail1

but in my page.
<asp:hyperlink ID=&quot;superemail1&quot; NavigateUrl=&quot;mailto:&quot; & empDS.tables(&quot;employeeinfo&quot;).Rows(0).Item(&quot;supervisoremail&quot;) & &quot;&quot;></asp:hyperlink>

I am guessing my NavigateUrl is formated wrong to. Basically I need to have the words mailto: followed by whatever is in that database field.


 
this is in your code-behind??
If you are using VS then this following line is auto added when you drag the hyperlink object on the page. If you manually added it by typing you need this in your code behind.

Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
That'l do donkey, that'l do
[bravo] Mark
 
nope, I am not using a code-behind or VS. I am using DreamweaverMX to work on my pages
 
This is what I have changed it too

<asp:hyperlink ID=&quot;superemail1&quot; Text=&quot;'<%# empDS.tables(&quot;employeeinfo&quot;).Rows(0).Item(&quot;supervisoremail&quot;)%>'&quot; NavigateUrl=&quot;'<%# String.Format(&quot;mailto:&quot;, empDS.tables(&quot;employeeinfo&quot;).Rows(0).Item(&quot;supervisoremail&quot;))%>'&quot;></asp:hyperlink>

but it says empDS is not declared.
 
Have you declared it? Did you make a function that declares and loads it on the Page_onload event? That'l do donkey, that'l do
[bravo] Mark
 
I don't have any type of function, but I call the field from the database in the Page_load sub.
 
Ya that is what I meant as long as you are loading the dataset somewhere.

Now I am guessing that you have your dataset declared in the Page_load sub? If you have this is why you get the error message. Make sure to declare it outside of the sub so that it still exists once you get down to the data binding bit. That'l do donkey, that'l do
[bravo] Mark
 
Where would you put the code then? Does it have to be in a sub? Or can you just put it anywhere between the script tags?
 
I haven't actually worked in code that isn't code-behind, however the same concepts do apply. And yes you are on the right track. Simply declare the dataset at the top of you page within some script tags. Don't put the declaration in a sub or the variable only lasts for the duration of the sub or function. Then fill your dataset in the page_load event and it should work fine in the databinding bit. That'l do donkey, that'l do
[bravo] Mark
 
If I am understanding you correctly I put the code like this
<script runat=&quot;server&quot;>

Dim dbconn1 As SqlConnection = New SqlConnection(&quot;server=AM1ST_FS1;database=HRINFO;uid=sa;&quot;)
Dim selectCMD5 As SqlCommand = New SqlCommand()

selectCMD5.CommandText = &quot;Select * from employeeinfo where userID = '&quot; & request.servervariables(&quot;AUTH_USER&quot;) & &quot;'&quot;
selectCMD5.Connection = dbconn1
dbconn1.open
if selectCMD1.ExecuteNonQuery() = 0 then
selectCMD5.CommandText = &quot;Select * from employeeinfo where userID = 'AM1ST_DOMAIN\&quot; & request.servervariables(&quot;AUTH_USER&quot;) & &quot;'&quot;
end if
dbconn1.close
selectCMD5.CommandTimeout = 30
Dim empDA As SqlDataAdapter = New SqlDataAdapter
empDA.SelectCommand = selectCMD5
dbconn1.Open()
Dim empDS As DataSet = New DataSet
empDA.Fill(empDS, &quot;employeeinfo&quot;)
dbconn1.Close()


Public Sub Page_Load(Source as Object, E As EventArgs)

'code here

End Sub

This produces the following error though, Why? this code works when enclosed in a sub?

Compiler Error Message: BC30188: Declaration expected.

Source Error:



Line 10: Dim selectCMD5 As SqlCommand = New SqlCommand()
Line 11:
Line 12: selectCMD5.CommandText = &quot;Select * from employeeinfo where userID = '&quot; & request.servervariables(&quot;AUTH_USER&quot;) & &quot;'&quot;
Line 13: selectCMD5.Connection = dbconn1
Line 14: dbconn1.open


Source File: E:\Intranet\Mystuff\Mystuff\personal.aspx Line: 12
 
Nevermind, I got it figured out, thanks for the help
 
I was trying to make better code out of my mess that I had and wondered why this doesn't return anything? I get no errors, and no text is shown. Am I calling the function wrong?

Function MyFunction() As String

Dim dbconn1 As SqlConnection = New SqlConnection(&quot;server=AM1ST_FS1;database=HRINFO;uid=sa;&quot;)
Dim selectCMD5 As SqlCommand = New SqlCommand()
selectCMD5.CommandText = &quot;Select * from employeeinfo where userID = '&quot; & Session(&quot;whosonline&quot;) & &quot;'&quot;
selectCMD5.Connection = dbconn1
selectCMD5.CommandTimeout = 30
Dim empDA1 As SqlDataAdapter = New SqlDataAdapter
empDA1.SelectCommand = selectCMD5
dbconn1.Open()
Dim empDS1 As DataSet = New DataSet
empDA1.Fill(empDS1, &quot;employeeinfo&quot;)
dbconn1.Close()

return empDS1.tables(&quot;employeeinfo&quot;).Rows(0).Item(&quot;supervisoremail&quot;)

End Function

<asp:hyperlink Text=&quot;<%# MyFunction()%>&quot; NavigateUrl=&quot;<%# MyFunction()%>&quot; runat=&quot;server&quot;></asp:hyperlink> </p>
 
do you have access to query analyer for that sql server? if so try runningt the select command you have there on the server. The code looks fine, it looks like there is no data there.

The other thing is your where clause. Should you have spelt the session variable whosonline differently when you assigned it you would get the following select.
Select * from employeeinfo where userID = ''
which will return a blank dataset also giving your described results. That'l do donkey, that'l do
[bravo] Mark
 
I ran the select statement through the query analyer by changing the session variable to a value I knew was in the database and it returned a result. When I changed it to the same statement in the function it returns nothing to the page.
 
somewhere on the page as a test put Response.Write(session(&quot;whosonline&quot;)

If there is no values on the page then you know your session variable isn't being set. That'l do donkey, that'l do
[bravo] Mark
 
I added page.databind to the page load sub and it works now.
 
[cry][rofl][cry]

David Copperfield:
&quot;Abracadabra&quot;

.NET Programmers:
&quot;object.DataBind()&quot;
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top