Results 1 to 8 of 8

Thread: [2005] Unable to insert a record in database!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    150

    [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

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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:
    1. conn.Open()
    2. n=cmd.ExecuteNonQuery()
    3. msgbox(n.tostring)
    4. conn.Close()

    If the message box shows 1 everything it's working fine!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    150

    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 !

  4. #4
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    150

    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?
    Last edited by bomayed; Sep 22nd, 2007 at 05:18 PM.

  6. #6
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    150

    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 !


  8. #8
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    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...

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