ASPX/SQL connection example

I am attempting my very first SQL connection with an aspx page.....   I am very new to aspx... here is the code.

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

<%
If Request.Form("Submit") <> "" Then
  Dim UserName
  Dim UserMessage
  Dim State
  Dim Company
  State = Request.Form("State")
  Company = Request.Form("Company")
  UserName = Request.Form("Name")
  UserMessage = Request.Form("Message")
  Dim Message
  Message = Message & "SpiritCheer.Com User " & UserName & " from " & Company & " in " & State & " left the following message: " & UserMessage

  Const cdoSendUsingPort = 2

  Dim oConfiguration
  Dim oMessage
  Dim oFields
  Dim connectionString
  oMessage = CreateObject("CDO.Message")
  oConfiguration = CreateObject("CDO.Configuration")

  oFields = oConfiguration.Fields
    With oFields
     .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
       .Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "cowensby"
.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxx"
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")= "xxxxxxxxx"
     .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
     .Update
    End With

  With oMessage
    .Configuration = oConfiguration
    .To = "esykes@spiritcheer.com"
    .From = "information@spiritcheer.com"
    .Subject = "Web Contact"
    .HTMLBody = Message
    .Send
  End With
 
 connectionString = ConfigurationManager.ConnectionStrings["SCDBConnectionString"].ConnectionString;
 
 
using (SqlConnection conn = new SqlConnection(SCDBConnectionString))
        {
           
        }
END using            
 
 
  Response.Redirect("success.html")
End If
-----------------------------------------------------------------------------------------------------------

to say the least this doesnt work.  Can someone lead me into the right direction?


%>
clayoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clayoAuthor Commented:
I have a web.config file in the root of my site with the following code.

<configuration>
      <appSettings/>
      <connectionStrings>
            <add name="SCDBConnectionString"
                           connectionString="Server=cowensby.db.4291945.hostedresource.com;Database=cowensby;User
                                                      ID=cowensby;Password=xxxxxxxx;Trusted_Connection=False;"
                           providerName="System.Data.SqlClient" />

      </connectionStrings>
</configuration>
Gagan_JauraCommented:
Hey are you coding in VB or C#. I can see code in both the language, the varaibles are declared in VB.net syntax and connection is declared in c#. Please be clear.
Your approach looks good to me. Can you please post the error message that you are getting. That will help to diagnose the problem.
SriVaddadiCommented:
Clayo,

ASP.Net is different from  classic ASP. You have done inline coding like in classic ASP here.

For ASP.Net, this is not a good approach. You should be writing the server side code in the code behind file. like default.aspx.vb or default.aspx.cs depending on your language.

Any code in the scriplet tags <%%> is supposed to go into code behind.

Check this link for better understanding

http://msdn.microsoft.com/en-us/magazine/cc163675.aspx

Coming to your issue,  the below code is incorrect

connectionString = ConfigurationManager.ConnectionStrings["SCDBConnectionString"].ConnectionString;
 
 
using (SqlConnection conn = new SqlConnection(SCDBConnectionString))
        {
           
        }
END using            

replace "[ ] " with "()"

example: connectionString = ConfigurationManager.ConnectionStrings("SCDBConnectionString").ConnectionString;

replace

using (SqlConnection conn = new SqlConnection(SCDBConnectionString))
with
Using conn As New SqlConnection(connectionString)
end Using            


{} does not apply in vb.net


Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

clayoAuthor Commented:
I followed your instrctions... My code looks like this:

 connectionString = connectionString = ConfigurationManager.ConnectionStrings("SCDBConnectionString").ConnectionString;

Using conn As New SqlConnection(connectionString)
end Using

But I get the following error:

Compiler Error Message: BC30037: Character is not valid.

Source Error:

 

Line 42:  
Line 43:  
Line 44:  connectionString = connectionString = ConfigurationManager.ConnectionStrings("SCDBConnectionString").ConnectionString;

 
clayoAuthor Commented:
I think it maybe my initial connection.  I know that it is not the best practice to connect on every page.  but this is the only page that will use this connection.  I do have a web.config file in the root and the code is posted above.  Is that code correct?
drypzCommented:
Hi! Please remove the semi-colon on it since you are using vb.

 connectionString = connectionString = ConfigurationManager.ConnectionStrings("SCDBConnectionString").ConnectionString;

to

connectionString = ConfigurationManager.ConnectionStrings("SCDBConnectionString").ConnectionString
clayoAuthor Commented:
Ok did that now Im getting an error that the SQLconnection is not defined.

Compiler Error Message: BC30002: Type 'SqlConnection' is not defined.

Source Error:

Line 46:  
Line 47:  
Line 48: Using conn As New SqlConnection(connectionString)
Line 49: end Using            
Line 50:  
Gagan_JauraCommented:
You have not imported the SQlClient class. Write the below code on the top of your file
Imports System.Data.SqlClient

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.