Link to home
Start Free TrialLog in
Avatar of Regedek
Regedek

asked on

Need help migrating If..Then.. Else statement from Classic ASP to .NET

How would I replicate the following Classic ASP code in VB.NET?

<% If isNull(rs("client_website")) OR rs("client_website") = "" Then %>
<br />
<% Else %>
<a target="_blank" href="<%=rs("client_website")%>">view website</a><br />
<% End If %>
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

One way of doing this would be to include a literal control, call it literal1

In the code behind, do

If isNull(rs("client_website")) OR rs("client_website") = "" Then
   literal1.Text = "<br/>"
else
   literal1.text = "<a target='_blank' href='<" + rs("client_website") + ">'>view website</a><br />"
end if
Avatar of Regedek
Regedek

ASKER

I'm VERY new to .NET (Only my second day of exposure)...  So I have some additional questions...

To include a literal control would the vb code behind look something like this?
Dim Literal1 As Object
Literal1 = New Project1.Class1

Or am I doing this incorrectly?

Another question...
Can I still use isNull - or should I use IsDBNull (for instance):
If IsDBNull("client_website") OR ("client_website") = "" Then


And lastly...
What would the HTML markup look like - could/would I use an asp label to display it?
<asp:label id="Literal1" runat="server"></asp:label>

Not sure if it will help to answer my question, but I'm including the odbc connection code just in case:

Dim connStr As String = ConfigurationManager.AppSettings("DSN1").ToString()
Dim dbconn As New OdbcConnection(connStr)

    Sub Page_Load()
        Select Request("ToDo")

            '***********
            Case "ViewEvent"
            '***********
               
                Dim sql As Object
                Dim dbcomm As Object
                Dim dbread As Object
                dbconn.Open()
                sql = "SELECT * FROM Events WHERE id=" & Int(Request("eventid"))
                dbcomm = New OdbcCommand(sql, dbconn)
                dbread = dbcomm.ExecuteReader()
                ViewMyEvent.DataSource = dbread
                ViewMyEvent.DataBind()
                dbread.Close()
                dbconn.Close()

Any help is appreciated...  Thanks in advance.
You can literally just declare an <asp:literal  in your .aspx file.

<asp:Literal ID="Literal1" runat="server"></asp:Literal>

Or you can do in your code behind


        Dim literal1 As New Literal()
        Controls.Add(literal1)

But it's much easier to control the placement of your literal the first way.
isdbnull is ONLY used for columns in a row from a table you got back from the database, not for other objects.
Avatar of Regedek

ASKER

These are in fact columns being pulled from a database table, so I should swap isNull with isDBNull correct?
Correct.
Avatar of Regedek

ASKER

I apologize for my high level of .NET ignorance here...  Please bear with me...

I made the changes you suggested.  When I attempt to load the page however, I get the following error:
'rs' is not declared. It may be inaccessible due to its protection level.

So my next question is: What do I use in place of rs() in .NET (as below)?
If IsDBNull(rs("client_website")) Or rs("client_website") = "" Then
          literal1.Text = "<br/>"
Else
          literal1.Text = "<a target='_blank' href='<" + rs("client_website") + ">'>view website</a><br />"
End If

If I remove the "rs" from the above code and just leave the field names - the page will load up, but the asp:literal does not display for those records in which the client_website field contains data.
ASKER CERTIFIED SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Regedek

ASKER

Worked perfectly!
Thanks for your help... Much appreciated!
My pleasure, have fun with .NET.