locked
ExecuteNonQuery: Connection property has not been initialized. RRS feed

  • Question

  • User2003303885 posted

    Hey Everyone,

     I'm not sure why this code isn't working...Perhaps it's because I'm using parameters incorrectly with Inline SQL??? Otherwise, as far as I can tell, it's identical to other pieces of (working) code that I've written.

      

    Imports System.Data.SqlClient
    
    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub btSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btSubmit.Click
            Dim conn As New SqlConnection
            conn.ConnectionString = Web.Configuration.WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
            Dim cmdCheckDB As New SqlCommand
            cmdCheckDB.Connection = conn
            cmdCheckDB.CommandType = Data.CommandType.Text
            cmdCheckDB.CommandText = "SELECT EmailAddress from CollectedData WHERE EmailAddress = @EmailAddress"
            cmdCheckDB.Parameters.Add("EmailAddress", Data.SqlDbType.Char, 50).Value = txtEmail.Text
    
            Dim cmdInsertCommand As New SqlCommand
            cmdInsertCommand.CommandType = Data.CommandType.Text
            cmdInsertCommand.CommandText = "INSERT INTO CollectedData (EmailAddress, Name, HowDidYouHearSelectionIndex, WhatAreYouPassionateSelectionIndex, Comments, DateTimeStamp, IPAddress) VALUES (@EmailAddress, @Name, @HowDidYouHearSelectionIndex, @WhatAreYouPassionateSelectionIndex, @Comments, @DateTimeStamp, @IPAddress)"
            cmdInsertCommand.Parameters.Add("EmailAddress", Data.SqlDbType.Char, 50).Value = txtEmail.Text
            cmdInsertCommand.Parameters.Add("Name", Data.SqlDbType.Char, 50).Value = txtName.Text
            cmdInsertCommand.Parameters.Add("HowDidYouHearSelectionIndex", Data.SqlDbType.Int).Value = ddlHowDidYouHear.SelectedValue
            cmdInsertCommand.Parameters.Add("WhatAreYouPassionateSelectionIndex", Data.SqlDbType.Int).Value = ddlWhatAreYouPassionate.SelectedValue
            cmdInsertCommand.Parameters.Add("Comments", Data.SqlDbType.Char, 255).Value = txtComments.Text
            cmdInsertCommand.Parameters.Add("DateTimeStamp", Data.SqlDbType.DateTime).Value = DateTime.Now()
            cmdInsertCommand.Parameters.Add("IPAddress", Data.SqlDbType.Char, 12).Value = Request.UserHostAddress.ToString()
    
            conn.Open()
    
            If conn.State = Data.ConnectionState.Open Then
                Dim rdrcheckDb As SqlDataReader = cmdCheckDB.ExecuteReader()
                If rdrcheckDb.HasRows Then
                    lblError.Visible = True
                    lblError.Text = "You have already registered."
                Else
                    cmdInsertCommand.ExecuteNonQuery()
                End If
            End If
    
            conn.Close()
    
        End Sub
    End Class
    
     

    I also tried to use a Try/Catch statment with no avail.

    Any help would be greatly appreciated!

     

    Thursday, August 30, 2007 4:08 PM

Answers

  • User-221752185 posted

    You made your parameter in your sqlcommand start with an @,

    @EmailAddress.
     You neeed to also add the parameter with @ like so...
     
    cmdInsertCommand.Parameters.AddWithValue("@EmailAddress",strEmail); 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 30, 2007 4:26 PM