|
I am using following code for Data Grid Update & Delete, but update command is not working properly. When i select a row and edit it's content and then i press update button then it is taking old value at the time of update not new. Please help me in this regard.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim con As New OleDb.OleDbConnection()
Dim com As New OleDb.OleDbCommand()
Dim str3 As String
con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security
Info=False;User ID=sa;Initial Catalog=sis;Data Source=ROSHAN"
con.Open()
str3 = "select * from student"
com.CommandText = str3
com.Connection = con
Dim dataadapter As New OleDb.OleDbDataAdapter(com)
Dim ds As New Data.DataSet()
dataadapter.Fill(ds, "student")
studentgrid.DataSource = ds.Tables("student")
studentgrid.DataBind()
End Sub
Public Sub studentgrid_edit(ByVal sender As Object, ByVal E As
DataGridCommandEventArgs)
studentgrid.EditItemIndex = E.Item.ItemIndex
studentgrid.DataBind()
End Sub
Sub studentgrid_Cancel(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
studentgrid.EditItemIndex = -1
studentgrid.DataBind()
End Sub
Sub studentgrid_Update(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
'=============================================================
'------------Retrive Data From Row To Be Updated-----------
Dim nmbox As TextBox
Dim stname, enrid As String
nmbox = E.Item.Cells(3).Controls(0)
stname = nmbox.Text
'-------------Retrive the key for the row------------------
enrid = studentgrid.DataKeys(E.Item.ItemIndex)
'-------------Update The Database--------------------------
Dim con As New OleDb.OleDbConnection()
Dim com As New OleDb.OleDbCommand()
Dim str3 As String
str3 = "update student set stdname='" & stname & "' where
enrcode='" & enrid & "'"
con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security
Info=False;User ID=sa;Initial Catalog=sis;Data Source=ROSHAN"
con.Open()
com.CommandText = str3
com.Connection = con
com.ExecuteNonQuery()
con.Close()
End Sub
Sub studentgrid_delete(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
Dim enrid As String
'-------------Retrive the key for the row------------------
enrid = studentgrid.DataKeys(E.Item.ItemIndex)
'-------------Delete The Database--------------------------
Dim con As New OleDb.OleDbConnection()
Dim com As New OleDb.OleDbCommand()
Dim str3 As String
str3 = "delete from student where enrcode='" & enrid & "'"
con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security
Info=False;User ID=sa;Initial Catalog=sis;Data Source=ROSHAN"
con.Open()
com.CommandText = str3
com.Connection = con
'com.ExecuteNonQuery()
con.Close()
End Sub
End Class
|