ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  ASP.NET Data Controls Part 1: DataGrids
Subject:   Problem in Update with Data Grid
Date:   2005-11-11 00:30:56
From:   ad_dev
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



1 to 1 of 1
  1. Problem in Update with Data Grid
    2006-02-27 03:31:10  neeraj_thakur [View]

    • Problem in Update with Data Grid
      2006-11-15 08:37:50  beautifulmind_1978 [View]

1 to 1 of 1