none
Declaration expected? RRS feed

  • Question

  • Hi, I'm trying to insert data into a sql database, and I have the following code:

        Dim cnn As New SqlConnection("Data Source=server;Initial Catalog=formcapture;User ID=formcapture;Password='Mypassword'")
        'Dim command As New SqlCommand(sSQL, cnn)
        cnn .open()
        cnn .execute(ssql)
        cnn .Close()

     But the bottom three calls to cnn are erroring out with a declaration expected error.  Its as if Visual Studio isn't reading the cnn sqlconnection that I just set up above it.  Also if I try to work with the sql command on another line, I'll also get a declaration expected error.  Any help would be appreciated.

     

    • Moved by Brittany Behrens Monday, June 21, 2010 11:34 PM (From:Visual Studio Editor)
    • Moved by Barclay Hill Tuesday, June 22, 2010 4:38 PM More appropriate forum (From:Visual Studio Database Development Tools (Formerly "Database Edition Forum"))
    Monday, June 21, 2010 9:39 PM

All replies

  • Hello,

    I do not think you could utilize the syntax like 'cnn.execute(ssql)'. Usually it would be 'SqlCommand.ExecuteReader()' method.

    As I can think of, you should first debug the applicaiton to see if there are errors when it comes to 'cnn.Open()', if it has not errors, it indicates you could open the database correctly, then you could focus on the errors that debug mode found to find the root cause.

    In additon, I think you should post on Visual Basic Language Forum
    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/threads so you can get better and quicker response as there are more experts there.

    Thanks for your understanding.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    Tuesday, June 22, 2010 9:10 AM
  • You should use command.ExecuteNonQuery(); instead of cn.execute(ssql).
    Friday, July 2, 2010 5:52 AM
  • Hi Richard,

    SqlConnection has no Execute member, I presume you ment command.ExecuteReader? Also, if cnn is not defined, you may be missing the Imports System.Data.SqlClient statement  (see code below):

    Imports System.Data.SqlClient
    
    Module Module1
    
      Sub Main()
        Using cnn As New SqlConnection("Data Source=server;Initial Catalog=formcapture;User ID=formcapture;Password='Mypassword'")
          Dim sSQL As String = "SELECT * FROM [YourTableHere]"
          Dim command As New SqlCommand(sSQL, cnn)
          cnn.Open()
          Dim reader As SqlDataReader = command.ExecuteReader()
          While reader.Read()
            Console.WriteLine(reader.GetString(0))
          End While
        End Using
      End Sub
    
    End Module
    

     

    Marcel

    Sunday, July 4, 2010 7:09 AM
  • Hi Friends,

    Please try to understand the requirements of the questioner. Richard has asked for INSERT and you are giving answer for SELECT. Please make gentle use of this forum.

    Monday, July 5, 2010 12:38 PM
  • Hi Chandramohan,

    Oops, you're right! But this doesn't change anything in respect to the cause which leads to the compilation to fail. This is not because he is using a method, that does not exist in SqlConnection class, but because of other things he's missing: a imports directive, a library reference and the sort.

    Marcel

    Monday, July 5, 2010 12:46 PM