Coffeehouse Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Can anyone tell me what is wrong with this code?

Back to Forum: Coffeehouse
  • User profile image
    Steve411

    Can anyone tell me what is wrong with this code?
     Thanks..



    <%@ Page Language="VB" %>
    <script runat="server">

        Dim Value2 as text1.value
        Dim None As You didn't enter an e-mail address.
        Private Sub Submit1_Click(sender As Object, e As EventArgs)
          If text1.value="" then
            Response.Write("This is your e-mail address, None")
            Else
           If Text.value="Value2" then
              Response.Write("This is your e-mail address, Value2")
             End If
             End If
             End Sub

    </script>
    <html>
    <head>
    </head>
    <body>
        <p>
        </p>
        <p>
            <asp:Label id="Label1" runat="server">Your E-Mail Address here:</asp:Label>
            <asp:TextBox id="text1" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Button id="Submit1" onclick="Submit1_Click" runat="server" Text="View Demonstration"></asp:Button>
        </p>
        <p>
            <asp:Image id="Image1" runat="server" ImageUrl="C:\Documents and Settings\Lumpy Wafflesniffer\Desktop\Desktop Icons\Snorton Project\Style\Images\file_folder_tasks_up`.bmp" BorderStyle="Dashed" BorderColor="Black" BackColor="Red" BorderWidth="2px"></asp:Image>
        </p>
    </body>
    </html>

  • User profile image
    FluffyDevil​Bunny

    Dim Value2 as String = text1.value
    Dim None As String = "You didn't enter an e-mail address."

    Private Sub Submit1_Click(sender As Object, e As EventArgs)
       If text1.value="" then
          Response.Write("This is your e-mail address, " + None)
       Else
          Response.Write("This is your e-mail address, " + Value2)
       End If
    End Sub

    I am a C# programmer so I hope the syntax is correct. But I would have written this as:



    Private Sub WriteEmail(ByVal email as String)
       Response.Write(String.Format("This is your e-mail address, {0}", email)
    End Sub

    Private Sub Submit1_Click(sender as Object, e As EventArgs)
       If text1.value = "" then
          WriteEmail("Your didn't enter an e-mail address.")
       Else
          WriteEmail(text1.value)
       EndIf
    End Sub


    If I did this wrong and someone else can convert C# to VB this is what I intended:

    private void WriteEmail(string email)
    {
       Context.GetCurrentContext().Response.Write(String.Format("This is your e-mail address, {0}", email));
    }

    private void Submit1_Click(Object sender, EventArgs e)
    {
       WriteEmail( (this.text1.Text.IsEmpty()) ? "You didn't enter an email address" : this.text1.Text );
    }

  • User profile image
    Steve411

    Makes sense to me.... thank you FluffyDevilBunny

  • User profile image
    Steve411

    Though, to send e-mail i would of had used this code instead. ASP



    -----------------------------------------------------------------------


    ' Build a MailMessage
    Dim mailMessage As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
    mailMessage.From = "your-mail@server/host.com"
    mailMessage.To = lopovmecka@msn.com
    mailMessage.Subject = "I hope this here works..."
    mailMessage.BodyFormat = System.Web.Mail.MailFormat.Text

    'TODO: Insert your body E-Mail here!

    System.Web.Mail.SmtpMail.SmtpServer = "localhost"
    System.Web.Mail.SmtpMail.Send(mailMessage)

    -----------------------------------------------------------------------

    Or if you like to send it as HTML style instead of plain text use this:


    ' Build a MailMessage
    Dim mailMessage As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
    mailMessage.From = "your-mail@server/host.com"
    mailMessage.To = "lopovmecka@msn.com"
    mailMessage.Subject = "I hope this here works..."""
    mailMessage.BodyFormat = System.Web.Mail.MailFormat.Html

    'TODO: Insert your body E-Mail here!

    System.Web.Mail.SmtpMail.SmtpServer = "localhost"
    System.Web.Mail.SmtpMail.Send(mailMessage)

    -----------------------------------------------------------------------

    <form runat="server">
            <asp:Panel id="Panel1" runat="server" Height="20px" Width="424px" ForeColor="Black" BorderColor="#404040" BackColor="LightGray">And
    would of had inserted the contents in here.... the two textboxes and the send
    {submit} button <input type="submit" value="Submit" />
            </asp:Panel>

  • User profile image
    Steve411

    Edit:

  • User profile image
    LazyCoder

    There's a ton of things wrong with that code. Sorry. Sad

    Here's how I would rewrite it.

    <%@ Page Language="VB" %>
    <script runat="server">

    Private Sub btnSubmitEmail_Click(sender As Object, e As EventArgs)
          If txtEmail.Text="" then
              Response.Write("You didn't enter an e-mail address.")
          Else
              Response.Write("This is your e-mail address, " & txtEmail.Text)
         End If
     End Sub

    </script>
    <html>
    <head>
    </head>
    <body>
        <p>
        </p>
        <p>
            <span>Your E-Mail Address here:</span>
            <asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Button id="btnSubmitEmail" onclick="btnSubmitEmail_Click" runat="server" Text="View Demonstration"></asp:Button>
        </p>
        <p>
            <asp:Image id="Image1" runat="server" ImageUrl="C:\Documents and Settings\Lumpy Wafflesniffer\Desktop\Desktop Icons\Snorton Project\Style\Images\file_folder_tasks_up`.bmp" BorderStyle="Dashed" BorderColor="Black" BackColor="Red" BorderWidth="2px"></asp:Image>
        </p>
    </body>
    </html>


  • User profile image
    LazyCoder

    Now here's why I rewrote it that way.

    First off, always try to give you WebControls meaningful ID's when you declare them in your HTML markup. Especially if you are using the "Code-inside" model of ASP.NET. Since Visual Studio.net 2K3 doesn't support intellisense in the code-inside model, I always prefix my WebControls using semi-Hungarian prefixes left over from my Access and VB 4/5 programming days. It lets me know just by looking at the ID what kind of control it is. While the ID "Text1" does tell me that it's a TextBox, it doesn't tell me anything about the kind of input I'm expecting to be put in it. txtEmail tells me it's a text box and it's where I should find the email address the user has input. Same with the Button.

    I changed the Label tag to just a regular HTML span tag. You weren't doing anything dynamic with it, you never set or get the text from it, so there wasn't any need to use a WebControl.

    Next, the Subroutine.

          If txtEmail.Text="" then
              Response.Write("You didn't enter an e-mail address.")
          Else
              Response.Write("This is your e-mail address, " & txtEmail.Text)
         End If


    Rather than setting the TextBox Text property to a string variable and then checking the value of the string variable, I changed the code to check the Text property directly. It makes the code a little cleaner. Again, you weren't really doing anything with the Text box value except reading it. I removed the second if-then statement too and condensed it down to one. In general, creating global vars is bad. That's a sweeping statement that isn't always true, but in general if you can avoid it you should avoid using a global variable.

  • User profile image
    Steve411

    Thanks for the explanation..
       I'am new to this you know.

  • User profile image
    LazyCoder

    I figured you were either new to programming or new to ASP.NET. If this was classic ASP or PHP I would have re-written it differently. That's why I included the explanation, so that you wouldn't think it was all just a matter of style. Plus if you ask 6 other developers what's wrong with that code they'd give you 6 (probably 7) other version and explanations. Smiley

  • User profile image
    Steve411

    LazyCoder wrote:
    I figured you were either new to programming or new to ASP.NET. If this was classic ASP or PHP I would have re-written it differently. That's why I included the explanation, so that you wouldn't think it was all just a matter of style. Plus if you ask 6 other developers what's wrong with that code they'd give you 6 (probably 7) other version and explanations. Smiley


     Yes, actually have been programing for about a year now with VB6, am pretty good at VB, though am new to ASP.NET, i thought it was an interesting language to write in so went ahead and tried to write something with it, but i guess it did not work lol. Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.