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 %>
RegedekAsked:
Who is Participating?
 
Jens FiedererTest Developer/ValidatorCommented:
rs needs to be a datarow or similar, so if you did:

                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)
                reader = dbcomm.ExecuteReader()
                 If reader.HasRows Then
                        Do While reader.Read()
                                If IsDBNull(reader("client_website")) Or IsDBNull(reader("client_website"))= "" Then
                                              ''' whatever
                                end if
                        Loop
                  end if


0
 
Jens FiedererTest Developer/ValidatorCommented:
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
0
 
RegedekAuthor Commented:
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jens FiedererTest Developer/ValidatorCommented:
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.
0
 
Jens FiedererTest Developer/ValidatorCommented:
isdbnull is ONLY used for columns in a row from a table you got back from the database, not for other objects.
0
 
RegedekAuthor Commented:
These are in fact columns being pulled from a database table, so I should swap isNull with isDBNull correct?
0
 
Jens FiedererTest Developer/ValidatorCommented:
Correct.
0
 
RegedekAuthor Commented:
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.
0
 
RegedekAuthor Commented:
Worked perfectly!
Thanks for your help... Much appreciated!
0
 
Jens FiedererTest Developer/ValidatorCommented:
My pleasure, have fun with .NET.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.