Results 1 to 10 of 10

Thread: [RESOLVED] ExecuteNonQuery not executing

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved [RESOLVED] ExecuteNonQuery not executing

    Hi

    I'm trying to execute a SQL command, but I just can't find out why this is not working. This is how I defined the "execute" function in my class named "clsSQL":
    Code:
        ''#...
        Private m_sConnectionString As String = String.Empty
        ''#...
        Friend WithEvents m_objConnection As SqlConnection
        Friend WithEvents m_objCommand As SqlCommand
        ''#...
    
            Public Function OpenConnection() As Boolean
                Try
                    m_objConnection = New SqlConnection(m_sConnectionString)
                    m_objConnection.Open()
                    Select Case m_objConnection.State
                        Case Data.ConnectionState.Open : Return True
                        Case Else : Return False
                    End Select
                Catch ex As Exception
                     RaiseEvent OnError("OpenConnection", ex)
                End Try
            End Function
    
    
            Public Function Execute(ByVal sQuery As String) As Boolean
                    Try
            #If DEBUG_MODE Then
                        Debug.WriteLine(sQuery)
            #End If
                        m_objCommand = New SqlCommand(sQuery, m_objConnection)
                        m_objCommand.ExecuteNonQuery()
                        m_objCommand = Nothing
                        Return True
                    Catch ex As Exception
                        RaiseEvent OnError("Execute", ex)
                    End Try
                End Function
    ''#..
    ''#...
    This is how I'm calling it:
    Code:
            Using oSQL As New clsSQL(My.Settings.projectConnectionString)
                If oSQL.OpenConnection Then
                    strSQL = "INSERT INTO ficheiros (nome_ficheiro, caminho_ficheiro, checksum) values ('nomefich', 'nomepath', '000')" 
                    oSQL.Execute(strSQL)
                Else
                   MsgBox("fail")
                End If
            End Using
    The code raises no error, it is just not saving the data in the database. The error is not in the SQL command, I've manually tested it

    For instance, I can execute perfectly the next function without any kind of problems:
    Code:
        Public Function ToDataGrid(ByVal oDataGrid As DataGridView, _
                                   ByVal sQuery As String, _
                          Optional ByVal sTable As String = "") As Boolean
            Try
    #If DEBUG_MODE Then
                Debug.WriteLine(sQuery)
    #End If
                Dim objDataSet As New DataSet
                objDataSet = ToDataSet(sQuery, sTable)
                oDataGrid.DataSource = objDataSet.Tables(0)
                objDataSet.Dispose()
                objDataSet = Nothing
                Return True
            Catch ex As Exception
                RaiseEvent OnError("ToDataGrid", ex)
            End Try
        End Function
    And this is how I'm calling it:

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Using oSQL As New clsSQL(My.Settings.projectConnectionString)
            If oSQL.OpenConnection Then
                oSQL.ToDataGrid(Me.DataGridView, "select * from table")
            End If
        End Using
    End Sub
    Probably, I just need another pair of eyes, 'cause I can't see what am I doing wrong :|
    Could this be related to the database and not to my code?

    Thanks

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: ExecuteNonQuery not executing

    I would suggest to create a function which will return object and gets the sql to be executed and the type of data to be return back. And you could cast it back after calling the function
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ExecuteNonQuery not executing

    The code raises no error, it is just not saving the data in the database.
    And how do you know this?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: ExecuteNonQuery not executing

    Quote Originally Posted by danasegarane View Post
    I would suggest to create a function which will return object and gets the sql to be executed and the type of data to be return back. And you could cast it back after calling the function
    Hi

    I'm very sorry, but I didn't understand what you mean with your suggestion.
    (I'm not English native-speaker)

    can you be more explicit in your suggestion, if possible?


    Quote Originally Posted by techgnome View Post
    And how do you know this?

    -tg

    I've "debugged/stepped into" all my code lines and no error was raised.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ExecuteNonQuery not executing

    This is the part I want to know more about:
    "it is just not saving the data in the database"

    HOW do you know it is NOT saving the data?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: ExecuteNonQuery not executing

    Quote Originally Posted by techgnome View Post
    This is the part I want to know more about:
    "it is just not saving the data in the database"

    HOW do you know it is NOT saving the data?

    -tg
    ohh, I'm sorry.
    I've opened the table and I saw that the table is empty (0 rows) and I performed a "select count(..." and it is returning "0", on the Database explorer.

    thanks

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ExecuteNonQuery not executing

    ok... In my signature there is a link titled "I swear I saved my data, where'd it run off to?" ... click it... read the thread, and see if it applies to you in this case. If it does, the answer is provided in there. If it does not apply to this case, then let us know.

    -tg

    Also - it might help to post the connection string you are using.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: ExecuteNonQuery not executing

    I will read it.

    many thanks

  9. #9

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: ExecuteNonQuery not executing

    Quote Originally Posted by techgnome View Post
    ok... In my signature there is a link titled "I swear I saved my data, where'd it run off to?" ... click it... read the thread, and see if it applies to you in this case. If it does, the answer is provided in there. If it does not apply to this case, then let us know.

    -tg

    Also - it might help to post the connection string you are using.
    You were right. I was saving my data, just was looking in the wrong place.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] ExecuteNonQuery not executing

    It's a common problem. 9 times out of 10, if there are no errors... you're simply checking the data in the wrong place. It's gotten most of us at some point.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width