Results 1 to 3 of 3

Thread: [RESOLVED] how to refresh a datagridview and prevent rows from getting repeated in the datagrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    198

    Resolved [RESOLVED] how to refresh a datagridview and prevent rows from getting repeated in the datagrid

    when i refresh my datagridview control, the new values gets updated but all the rows get repeated. in the figure 'student_pk ' is the primary key. after entering a new row, a whole new set of rows gets displayed, such as after row 16 , row 1 starts.
    neeedless to say, rows must not be repeated. How?? any help will be appreciated. thanks
    Name:  Uzntitled.png
Views: 364
Size:  64.3 KB

    the code is:

    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    
    Public Class addStudent
        Private da As New SqlDataAdapter(" select * from student", con)
        Private ds As New DataSet
        Private cmb As New SqlCommandBuilder(da)
    
    
    
        Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
            addStudent()
            load_dgStudent()
        End Sub
    
    
    
        Private Function addStudent() As Boolean
    
            Dim cmd As New SqlCommand
            Dim ok As Boolean = False
    
            cmd.Connection = con
            cmd.CommandText = " INSERT INTO Student(student_id, student_firstname, student_lastname, student_institute)" _
            & "VALUES(@sid, @sfn, @sln, @sin)"
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("sid", sidTxt.Text)
            cmd.Parameters.AddWithValue("sfn", sfnTxt.Text)
            cmd.Parameters.AddWithValue("sln", slnTxt.Text)
            cmd.Parameters.AddWithValue("sin", slnTxt.Text)
            MessageBox.Show("record added")
    
            Try
                con.Open()
                cmd.ExecuteNonQuery()
                ok = True
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Add failed")
                ok = False
            End Try
    
            cmd.Dispose()
            con.Close()
            Return ok
        End Function
    
    
    
        Private Sub load_dgStudent()
            If Not con.State = ConnectionState.Open Then
                con.Open()
            End If
    
            Try
                da.Fill(ds, "student")
                dgStudent.DataSource = ds.Tables("student")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            con.Close()
        End Sub
    
    
    
    
        Private Sub addStudent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            load_dgStudent()
        End Sub
    End Class

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: how to refresh a datagridview and prevent rows from getting repeated in the datag

    da.Fill(ds, "student")
    This command is cumulative. Every time you call it, it will add the query results to the existing table. If you don't want this then you must clear the datatable before the fill command.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    198

    Re: how to refresh a datagridview and prevent rows from getting repeated in the datag

    Quote Originally Posted by dunfiddlin View Post
    This command is cumulative. Every time you call it, it will add the query results to the existing table. If you don't want this then you must clear the datatable before the fill command.
    thanks.... ds.clear.
    it works

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