|
-
Oct 12th, 2009, 10:16 AM
#1
Thread Starter
Hyperactive Member
[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
-
Oct 12th, 2009, 10:44 AM
#2
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
-
Oct 12th, 2009, 10:48 AM
#3
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
-
Oct 12th, 2009, 10:56 AM
#4
Thread Starter
Hyperactive Member
Re: ExecuteNonQuery not executing
 Originally Posted by danasegarane
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?
 Originally Posted by techgnome
And how do you know this?
-tg
I've "debugged/stepped into" all my code lines and no error was raised.
-
Oct 12th, 2009, 11:42 AM
#5
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
-
Oct 12th, 2009, 11:45 AM
#6
Thread Starter
Hyperactive Member
Re: ExecuteNonQuery not executing
 Originally Posted by techgnome
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
-
Oct 12th, 2009, 11:52 AM
#7
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.
-
Oct 12th, 2009, 11:56 AM
#8
Thread Starter
Hyperactive Member
Re: ExecuteNonQuery not executing
I will read it.
many thanks
-
Oct 13th, 2009, 04:44 AM
#9
Thread Starter
Hyperactive Member
Re: ExecuteNonQuery not executing
 Originally Posted by techgnome
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.
-
Oct 13th, 2009, 08:25 AM
#10
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|