Results 1 to 2 of 2

Thread: My record is not saving on ms sql table

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2022
    Posts
    15

    My record is not saving on ms sql table

    I create a table named dbo.InfoTable using Ms SQL server.
    I encode the 1st item manually after creating the table. (see image below)

    Name:  TableData.jpg
Views: 241
Size:  19.1 KB


    When I run my program the Item no. 1 successfully shown on datagridview, (see image below)
    But the problem is when add a new record it is successfully shown on datagridview, but
    it doesn't appear on table (on the 1st image)

    Name:  Datagridrecord.jpg
Views: 196
Size:  24.9 KB

    my code on add button is:

    Code:
    Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
            Dim IDno As String = TextBox1.Text & "-" & TextBox2.Text
            Dim dr As SqlClient.SqlDataReader
            If TextBox2.Text <> "" And TextBox3.Text <> "" And StatusCMB.Text <> "" And DateHired.Text <> " " Then
                cmd.CommandType = System.Data.CommandType.Text
                cmd.CommandText = "Select * from INfotable Where Id ='" + IDno + "'"
                cmd.Connection = sqlConnection1
                sqlConnection1.Open()
                dr = cmd.ExecuteReader
                If dr.HasRows Then
                    MsgBox("ID Already exist")
                    sqlConnection1.Close()
                Else
                    sqlConnection1.Close()
                    cmd.CommandType = System.Data.CommandType.Text
                    cmd.CommandText = "INSERT INTO INfotable (Id, Employee_Name, Employee_Status, Hired_Date) VALUES ('" + IDno + "', '" + TextBox3.Text + "', '" + StatusCMB.Text + "', '" + DateHired.Text + "')"
                    cmd.Connection = sqlConnection1
                    sqlConnection1.Open()
                    cmd.ExecuteNonQuery()
                    sqlConnection1.Close()
                    ClearAll()
                    RefreshInfoView()
                End If
            Else
                MsgBox("Kindly fill up all boxes to proceed")
            End If
        End Sub
    This is the code on RefreshInfoView the one i'm using to show the value on datagridview

    Code:
    Me.InfoTableTableAdapter.Fill(Me.InfoTableDataset.InfoTable)

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

    Re: My record is not saving on ms sql table

    There's no "refreshing" required. You already have the data - you just added it - so why would you need to get it from the database again?

    If you have a typed DataSet then use it. No SqlDataReader. No ExecuteNonQuery. You call Fill on the table adapter to retrieve the existing data, you make changes to the DataTable, you call Update on the table adapter to save the changes. That's it, that's all.

    If you already have all the data then you don't need to query the database to see whether an ID already exists. If you don't have all the data and you do need to query the database then you use the table adapter. If you need a query other than the default "SELECT *" then you add one. That's why there's a DataSet designer.

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