Results 1 to 5 of 5

Thread: [RESOLVED] Data isnt getting Saved, using oledb cmd

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Resolved [RESOLVED] Data isnt getting Saved, using oledb cmd

    well this is the code i have to save the data to the db, but it isnt giving any error nor the data is been saved
    vb Code:
    1. Private Conn As OleDb.OleDbConnection
    2.     Private cmd As OleDb.OleDbCommand
    3.     Private sql As String
    4.  
    5.     Private Sub Members_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Try
    7.             Conn = New OleDb.OleDbConnection(My.Settings.DMSConnectionString)
    8.             cmd = New OleDb.OleDbCommand(sql, Conn)
    9.             Conn.Open()
    10.         Catch ex As Exception
    11.             MessageBox.Show("Error Connecting to Database", "DMS", MessageBoxButtons.OK)
    12.             Me.Close()
    13.         End Try
    14.     End Sub
    15.  
    16.     Private Sub CmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSave.Click
    17.         sql = "INSERT INTO Members ( MemberName, FatherName, Age, Gender, BloodGroup, WorkSection, Occupation, PhotoLink ) "
    18.         sql &= "Values (@MemberName, @FatherName, @Age, @Gender, @BloodGroup, @WorkSection, @Occupation, @PhotoLink ) "
    19.         cmd.CommandText = sql
    20.         cmd.Parameters.AddWithValue("@MemberName", Me.MemberName.Text.Trim)
    21.         cmd.Parameters.AddWithValue("@FatherName", Me.FatherName.Text.Trim)
    22.         cmd.Parameters.AddWithValue("@Age", Me.Age.Text.Trim)
    23.         cmd.Parameters.AddWithValue("@Gender", Me.Gender.Text)
    24.         cmd.Parameters.AddWithValue("@BloogGroup", Me.BloodGroup.Text)
    25.         cmd.Parameters.AddWithValue("@WorkSection", Me.WorkSection.Text.Trim)
    26.         cmd.Parameters.AddWithValue("@Occupation", Me.Occupation.Text.Trim)
    27.         cmd.Parameters.AddWithValue("@PhotoLink", "")
    28.         cmd.ExecuteScalar()
    29.     End Sub
    30.  
    31.     Private Sub Members_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    32.         Conn.Close()
    33.     End Sub

    plz help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Data isnt getting Saved, using oledb cmd

    First up, ExecuteScalar is for retrieving a single value. Use ExecuteNonQuery and test its return value. If it's not zero then the data is being saved
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Data isnt getting Saved, using oledb cmd

    one more thing:Its not a good idea to open a connection at the form load and closing the connection at the form closing

    for more security to the database,just open the connection,perform executenonquery and then close the connection immediately


    If you find my reply helpful , then rate it

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Data isnt getting Saved, using oledb cmd

    well thanks my original problem is solved, but

    one question with respect to
    one more thing:Its not a good idea to open a connection at the form load and closing the connection at the form closing

    for more security to the database,just open the connection,perform executenonquery and then close the connection immediately
    well if say every minute i want to get some data from the db, then should i connect and disconnect from the db every minute.

    i also had an doubt on the same topic

    If i have around 20 to 30 form which will access the same db , then do i need to create the connection object in each and every form

  5. #5
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: Data isnt getting Saved, using oledb cmd

    Quote Originally Posted by aashish_9601 View Post
    well if say every minute i want to get some data from the db, then should i connect and disconnect from the db every minute.
    you have to....whenever you are performing some database operations only for that moment of time you should be connected with the database else not ; though i dont know whether you will be performing some cryptographic security on the data present in your database or not

    Quote Originally Posted by aashish_9601 View Post
    If i have around 20 to 30 form which will access the same db , then do i need to create the connection object in each and every form
    You have to

    If you find my reply helpful , then rate it

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