CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2015
    Posts
    2

    Question ASP and MySQL - Creating a Mailing List Form

    I have been trying for several dys now to just get the code to work on a form to add a record to y MySQL database. I've been trying to use various samples online as my experience with ASP and .NET is limited. I am, however, an advanced VB and VBA programmer, but using VS2013 Premium sems to have some major coding differences.

    I need help. My code is at the bottom of the page. There are two files. This is the error I get in Visual Studio:

    VS Error.jpg

    I do not get an error in the browser, rather it keeps spinning because VS breaks at the point of the error.

    Here is the code for the Contact Us form:

    <%@ Page aspcompat=true %>
    <html>
    <head>
    <title>Mailing List Sign-Up</title>
    <style type="text/css">
    .auto-style1 {
    width: 256px;
    }
    </style>
    </head>
    <body>
    <form action="asp_mysql_insert_cust.aspx" name="frmContactUs" method="post">
    <table border="1">
    <tr>
    <th class="auto-style1"> <div>First Name </div></th><td><div><input type="text" name="txtFName" size="30"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Last Name </div></th><td><div><input type="text" name="txtLName" size="30"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Company </div></th><td><div><input type="text" name="txtCompany" size="100"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Address 1 </div></th><td><div><input type="text" name="txtAddr1" size="100"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Address 2 </div></th><td><div><input type="text" name="txtAddr2" size="100"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>City </div></th><td><div><input type="text" name="txtCity" size="30"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>2-Character State Code </div></th><td><div><input type="text" name="txtState" size="2" style="width: 50px"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Postal </div></th><td><div><input type="text" name="txtPostal" size="10"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Primary Phone </div></th><td><div><input type="text" name="txtPhone1" size="14"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Secondary Phone/Fax </div></th><td><div><input type="text" name="txtPhone2" size="14"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Email </div></th><td><div><input type="text" name="txtEmail" size="80"></div></td>
    </tr>
    <tr>
    <th class="auto-style1"> <div>Website URL </div></th><td><div><input type="text" name="txtWebsite" size="120"></div></td>
    </tr>
    </table>
    <input type="submit" name="submit" value="Add me to the list!">
    </form>
    </body>
    </html>
    Here is the code with the SQL:

    <%@ Page aspcompat=true %>

    <html>
    <head>
    <title>Mailing List Sign-Up</title>
    </head>
    <body>
    <%
    Dim Conn As Object, strSQL As String, objExec, ConnString As String
    Conn = Server.CreateObject("ADODB.Connection")
    Conn.Provider = "MySQL ODBC 5.1 Driver"
    ConnString = "SERVER='MYSQL5002.Smarterasp.net';UID=USERID;pwd='PASSWORD';database='DATABASE';"
    Conn.Open(ConnString)
    strSQL = ""
    strSQL = strSQL & "INSERT INTO customers "
    strSQL = strSQL & "(FName,LName,Company,Addr1,Addr2,City,State,Postal,Phone1,Phone2,Email,Website,Mailing_List) "
    strSQL = strSQL & "VALUES "
    strSQL = strSQL & "('" & Request.Form("txtFName") & "','" & Request.Form("txtLName") & "', '" & Request.Form("txtCompany") & "' "
    strSQL = strSQL & "'" & Request.Form("txtAddr1") & "','" & Request.Form("txtAddr2") & "', '" & Request.Form("txtCity") & "' "
    strSQL = strSQL & "'" & Request.Form("txtState") & "','" & Request.Form("txtPostal") & "', '" & Request.Form("txtPhone1") & "' "
    strSQL = strSQL & "'" & Request.Form("txtPhone2") & "','" & Request.Form("txtEmail") & "', '" & Request.Form("txtWebsite") & "' "
    strSQL = strSQL & ", 1);"
    objExec = Conn.Execute(strSQL)
    If Err.Number = 0 Then
    Response.write("Save completed.")
    Else
    Response.Write("Error in SQL statement: " & strSQL & ". Error: " & Err.Description)
    End If
    Conn.Close()
    objExec = Nothing
    Conn = Nothing
    %>
    </body>
    </html>

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ASP and MySQL - Creating a Mailing List Form

    Most likely, your connection string is invalid. This doesn't look like it's valid:

    Code:
    ConnString = "SERVER='MYSQL5002.Smarterasp.net';UID=USERID;pwd='PASSWORD';database='DATABASE';"
    Is USERID a variable? If so, does it contain the correct user id? Is PASSWORD really the password? Is DATABASE the database name?

    Put a break point on this line (click on the line with the mouse and press F9) and step through the code in the debugger (start debugging by pressing F5). The you can look at the value of ConnString to make sure everything looks correct.

  3. #3
    Join Date
    Jan 2015
    Posts
    2

    Re: ASP and MySQL - Creating a Mailing List Form

    Quote Originally Posted by Arjay View Post
    Most likely, your connection string is invalid. This doesn't look like it's valid:

    Code:
    ConnString = "SERVER='MYSQL5002.Smarterasp.net';UID=USERID;pwd='PASSWORD';database='DATABASE';"
    Is USERID a variable? If so, does it contain the correct user id? Is PASSWORD really the password? Is DATABASE the database name?

    Put a break point on this line (click on the line with the mouse and press F9) and step through the code in the debugger (start debugging by pressing F5). The you can look at the value of ConnString to make sure everything looks correct.
    LOL, no, USERID should've had single quotes around it, I must have edited them out by accident when I changed that, the password, and database name. I changed those as well since remote access is enabled on the host, for security reasons.

    Needless to say, I've triple checked the parameters and they're fine.

    UPDATE: I've successfully managed to create the form to insert a new record, and it works! I will post code later.

    However, I'm still trying to figure out the best way to create a page that I can view, edit, and delete all in one for the management portal. I was hoping there was an easy to use asp control for this bug I can't figure it out. I wish I could simply transform my Access application to asp - I can make Access do anything. Not having much luck with asp in VS2013. I could probably do it in a windows application easier but really need it Web based...

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: ASP and MySQL - Creating a Mailing List Form

    Rather than coding a .net ASP web app, I would start with an MVC/Razor 5 application. You'll see this option as a new project (not a new web project).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured