[2005] Unable to insert a record in database!
Can someone tell me what's wrong with this code ? whenever I run it , I don't get any error messages , but when I go and view the data in the table , I find nothing , Blank !
Code:
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim conn As New SqlConnection
conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\OperationDB.mdf;Integrated Security=True;User Instance=True"
Dim cmd As New SqlCommand
cmd.CommandText = "INSERT INTO COMPLAINS (NAME) VALUES ('TESTING')"
cmd.Connection = conn
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Catch
MessageBox.Show(Err.Description)
End Try
End Sub
End Class
Re: [2005] Unable to insert a record in database!
If you're debugging your application it's normal that the record doesn't "stay" there...
To test if everything it's working, add this to your code:
Create a var Dim n as integer
vb Code:
conn.Open()
n=cmd.ExecuteNonQuery()
msgbox(n.tostring)
conn.Close()
If the message box shows 1 everything it's working fine!
Re: [2005] Unable to insert a record in database!
I tried your suggestion and I got ( 1 ) in the messagebox , but still I don't see anything in the table in the database , it's empty !
What could be wrong ! :confused:
Re: [2005] Unable to insert a record in database!
Nothing is wrong. If you got 1, then the record was inserted.
Like i said, in the debug mode the changes made to the database doesn't persist after you close the application...
Just to confirm you can make a select * from COMPLAINS
Re: [2005] Unable to insert a record in database!
If the record was inserted like you say , then why when I right click on the table name in the Database Explorer and I choose ( Show Table Data ) , I see nothing there ?
yeah , I tried SELECT * FROM TABLE , I got the records
so my question is why the data is not saved in the database?
Re: [2005] Unable to insert a record in database!
i don't know if the problem is my english or what...
but try this and then you'll see the record in the table...
execute the application from the release folder, outside the ide, and the check the database.
Re: [2005] Unable to insert a record in database!
I'm not following , I don't understand how and why I need to do that
anyways , I looked in my projects folder , I found a folder called (release) which is empty !
Need help , I need to see the data in the database . . what's the point of a database that doesn't store any DATA !
:eek:
Re: [2005] Unable to insert a record in database!
I'll try to explain again, in the ide mode (clicking in the green arrow) every change that you made to the database isn't saved...
To save changes to the database you have to run your application outside the ide, in the debug\release folder you have your exe.
To have something in the release folder you have to build your application, menu build, build name_of_your_app. After building is done go to the release\debug folder and execute your application...