Results 1 to 3 of 3

Thread: [RESOLVED] Data not inserted in the Database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2021
    Posts
    71

    Resolved [RESOLVED] Data not inserted in the Database

    Hi,

    The following code executes without error but refreshing the database record not inserted. It does tell me that 'New Tenant has been added'

    Code:
      Dim command As New SqlCommand("INSERT INTO dbo.tbltenants (firstName, lastName, tenantfullname, telephone, email, nationalid, copyID) VALUES (@firstName, @lastName, @tenantfullname, @telephone, @email, @nationalid, @copyID)", conn)
    
                    Dim ms As New MemoryStream
                    PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
    
                    command.Parameters.Add("@firstname", SqlDbType.VarChar).Value = TxtFName.Text
                    command.Parameters.Add("@lastname", SqlDbType.VarChar).Value = TxtLName.Text
                    command.Parameters.Add("@tenantfullname", SqlDbType.VarChar).Value = TxtFName.Text & " " & TxtLName.Text
                    command.Parameters.Add("@telephone", SqlDbType.NVarChar).Value = TxtTel.Text
                    command.Parameters.Add("@email", SqlDbType.NVarChar).Value = TxtEmail.Text
                    command.Parameters.Add("@nationalID", SqlDbType.NVarChar).Value = TxtNationalID.Text
                    command.Parameters.Add("@copyID", SqlDbType.Image).Value = ms.ToArray()
    
                '    cmd.ExecuteNonQuery()
                'Catch ex As Exception
                '    MsgBox(ex.Message, MsgBoxStyle.Information, "RCMS")
                'End Try
    
                MsgBox("New Tenant has been added", MsgBoxStyle.Information, "RCMS")
                Me.Close()
    
            End If
    
            conn.Close()
            conn.Dispose()
    
    End Sub
    At the start of the Dub I have:

    Code:
            Dim conn As SqlConnection = GetDbConnection()
            Dim query As String
            Dim cmd As New SqlCommand
    Where am I doing wrong ?

    Thanks,

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Data not inserted in the Database

    In the code you posted, you have the cmd.ExecuteNonQuery commented out, which would mean that nothing ever happens. I'm kind of thinking that was just an error in this question, but it's certainly a problem if that's your actual code.

    If you ARE calling the cmd.ExecuteNonQuery command, that will return an integer showing the number of records impacted. In this case, it should return 1. If it is returning 1, then the record was added. If you still aren't seeing it after that, then it is because you are looking in the wrong place. There are a few ways that can happen, but first make sure that you are actually calling the ExecuteNonQuery, and that it is returning 1.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2021
    Posts
    71

    Re: Data not inserted in the Database

    Thanks Sorted!

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