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: 77
Size: 64.3 KB]()
the code is:
neeedless to say, rows must not be repeated. How?? any help will be appreciated. thanks
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