Answered by:
'SqlConnection' Error

Question
-
Within the button code (Default.aspx.vb), there is an undefined 'SqlConnection.'
What is the cause of this error?Go to the following URL link, download the 'High Res' Visual Studio snapshots at:
The button code is written in VB code and reads as follows:
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Public Function strLong(ByVal pstrText As String) As String '''/ Extract just the 0-9 chars from a string Dim strTemp As String = Nothing Dim lngI As Long = 0 strTemp = "" For lngI = 1 To Strings.Len(pstrText) If Strings.InStr(1, "1234567890", Strings.Mid(pstrText, lngI, 1)) > 0 Then strTemp = strTemp & Strings.Mid(pstrText, lngI, 1) End If Next Return Strings.Left(strTemp, 20) End Function ' strLong Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("sqlapplicationConnectionString").ToString()) Try Dim cmd As SqlCommand = conn.CreateCommand() cmd.CommandText = "INSERT INTO Application (" & " First, Last, City, State)" & ") VALUES (" & " @First, @Last, @City, @State" cmd.Parameters.AddWithValue(New SqlParameter() With { _ Key .ParameterName = "@First", _ SqlDbType.NVarChar, _ Key .Value = strLong(Request("First")) _ }) cmd.Parameters.AddWithValue(New SqlParameter() With { _ Key .ParameterName = "@Last", _ SqlDbType.NVarChar, _ Key .Value = strLong(Request("Last")) _ }) cmd.Parameters.AddWithValue(New SqlParameter() With { _ Key .ParameterName = "@City", _ SqlDbType.NVarChar, _ Key .Value = strLong(Request("City")) _ }) cmd.Parameters.AddWithValue(New SqlParameter() With { _ Key .ParameterName = "@State", _ SqlDbType.NVarChar, _ Key .Value = strLong(Request("State")) _ }) conn.Open() cmd.ExecuteNonQuery() conn.Close() Catch ex As Exception Finally If conn.State = ConnectionState.Open Then conn.Close() End If conn.Dispose() End Try End Sub End Class
Thursday, September 2, 2010 6:46 PM
Answers
-
>>there is an undefined 'SqlConnection.'
Very first line of code in you code behind should be
Imports System.Data
Imports System.Data.SQLClientright before
Partial Class _Default
Inherits System.Web.UI.Pageso in the end it should look like
Imports System.Data
Imports System.Data.SQLClientPartial Class _Default
Inherits System.Web.UI.Page............
- Edited by Chirag Shah Thursday, September 2, 2010 11:02 PM
- Marked as answer by Jens K. Suessmeyer -Microsoft employee Friday, September 3, 2010 7:20 AM
Thursday, September 2, 2010 7:54 PM
All replies
-
>>there is an undefined 'SqlConnection.'
Very first line of code in you code behind should be
Imports System.Data
Imports System.Data.SQLClientright before
Partial Class _Default
Inherits System.Web.UI.Pageso in the end it should look like
Imports System.Data
Imports System.Data.SQLClientPartial Class _Default
Inherits System.Web.UI.Page............
- Edited by Chirag Shah Thursday, September 2, 2010 11:02 PM
- Marked as answer by Jens K. Suessmeyer -Microsoft employee Friday, September 3, 2010 7:20 AM
Thursday, September 2, 2010 7:54 PM -
Thanks,Thursday, September 2, 2010 8:14 PM