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("") = cdoSendUsingPort
       .Item ("") = "cowensby"
.Item ("") = "xxxxxx"
     .Item("")= "xxxxxxxxx"
     .Item("") = 10
    End With

  With oMessage
    .Configuration = oConfiguration
    .To = ""
    .From = ""
    .Subject = "Web Contact"
    .HTMLBody = Message
  End With
 connectionString = ConfigurationManager.ConnectionStrings["SCDBConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(SCDBConnectionString))
END using            
End If

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

Who is Participating?

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

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.

            <add name="SCDBConnectionString"
                           providerName="System.Data.SqlClient" />

Hey are you coding in VB or C#. I can see code in both the language, the varaibles are declared in 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.

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

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;


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

{} does not apply in

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?
Hi! Please remove the semi-colon on it since you are using vb.

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


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:  
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

From novice to tech pro — start learning today.