Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: DataGrid Update Problem - help


Message #1 by "Robert Sindall" <robert@t...> on Tue, 23 Apr 2002 02:27:20 +0100
This is a multi-part message in MIME format.

------=_NextPart_000_004F_01C1EA6E.681F04D0
Content-Type: text/plain;
	charset="US-ASCII"
Content-Transfer-Encoding: 7bit

Hi
 
it worked once... when I was using e.item.itemindex instead of
e.item.cells(0).text
 
whets wrong with this code ?!?
 



Private Sub AdminUserGrid_UpdateCommand(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
AdminUserGrid.UpdateCommand

	Dim UpdateAdminUser As Obsessed.AdminUserDB = New
Obsessed.AdminUserDB()

	Dim Id As String = e.Item.Cells(0).Text

	Dim UsernameTxt As TextBox = CType(e.Item.Cells(1).Controls(0),
TextBox)

	Dim PasswordTxt As TextBox = CType(e.Item.Cells(2).Controls(0),
TextBox)

	UpdateAdminUser.UpdateAdminUser(CInt(Id),
CStr(UsernameTxt.Text), CStr(PasswordTxt.Text))

	AdminUserGrid.EditItemIndex = -1

	BindGrid()

End Sub

 

Best Regards

Robert Sindall
______________________________
Home:  <mailto:robert@t...> robert@t...
Hotmail:  <mailto:robert_sindall@h...> robert_sindall@h...
______________________________
Tel: +44 (0)1242 518955


 



Message #2 by jmnamahoe@h... on Tue, 23 Apr 2002 09:35:51
i was having a problem updating my records in a datagrid. instead of 
updating the records in a dataset. i used the data set to find out what 
the key was, and then i ran a sql statement. the code is written in 
Vb.net, hopefuly this will help:

 Sub MyDataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)

        Dim EditText as TextBox
        Dim Row as Integer
       
        StrSQL = "SELECT * FROM tbl_users;"
        
        StrConn = "Provider=Microsoft.jet.oledb.4.0;" & _
                          "Data Source=c:\begaspnet\db\testdb.mdb"
         ObjConn = New OleDBConnection(StrConn)
         ObjAdapt = New OleDBDataAdapter(StrSQL, ObjConn)
         ObjDS = New DataSet
         
'fill and name the dataset
         ObjAdapt.Fill (ObjDS, "USERS")
         
         Row = CInt(E.Item.ItemIndex)
' this line finds the specified control and assigns the value to the 
variable 'edittext''the value of edit text is then passed to a string.
         EditText = E.Item.FindControl ("txtFname")
         StrFname = EditText.Text
         EditText = E.Item.FindControl ("txtLname")
         StrLname = EditText.Text
         EditText = E.Item.FindControl ("txtUsrID")
         StrUsrID = EditText.Text
' this line searches through the data set looking for the '0' table with 
row the corresponds to the row variable identified earlier
         StrWhereUsrID = ObjDS.Tables(0).Rows(Row).Item("usr_id")
 
' a connection is then created and the string variables previously defined 
are then used in the sql statement. 
         ObjConn.Open()
         StrUpdate = "UPDATE tbl_users SET usr_fname = '" & StrFname & "', 
usr_lname = '" & StrLname & "', usr_id = '" & StrUsrID & "'" & vbCRLF & _
                             "WHERE usr_id = '" & StrWhereUsrID &"';"
         Message.InnerHtml = StrUpdate
         ObjCmd = New OleDBCommand (StrUpdate, ObjConn)

         Try
                ObjCmd.ExecuteNonQuery()
                MyDataGrid.EditItemIndex = -1
                Message.InnerHtml = StrUpdate
            Catch Exp As Exception
                Message.InnerHtml = StrFname & " " &  StrLname & " " & 
StrUsrID
                Message.Style("color") = "red"
         End Try
         
         ObjConn.Close ()
 
        BindGrid()
    End Sub
Message #3 by "Robert Sindall" <robert@t...> on Tue, 23 Apr 2002 17:37:00 +0100
This is a multi-part message in MIME format.

------=_NextPart_000_000B_01C1EAED.79E8C050
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hi
 
it worked once... when I was using e.item.itemindex instead of
e.item.cells(0).text
 
it seems to work, but once its saved it, it shows me the old data. there
is no error msg.
 
the datagrid has columns like this:
 
Id | Username | Password | Edit 
 
Id is readonly.
 
whats wrong with this code ?!?
 



Private Sub AdminUserGrid_UpdateCommand(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
AdminUserGrid.UpdateCommand

	Dim UpdateAdminUser As Obsessed.AdminUserDB = New
Obsessed.AdminUserDB()

	Dim Id As Integer = CType(e.Item.Cells(0).Text, Integer)

	Dim UsernameTxt As TextBox = CType(e.Item.Cells(1).Controls(0),
TextBox)

	Dim PasswordTxt As TextBox = CType(e.Item.Cells(2).Controls(0),
TextBox)

	'this class updates the admin user table
UpdateAdminUser.UpdateAdminUser(CInt(Id), CStr(UsernameTxt.Text),
CStr(PasswordTxt.Text))

	AdminUserGrid.EditItemIndex = -1

	'gets new data and binds it to the datagrid
BindGrid()

End Sub

HELP HELP HELP!!!!

Best Regards

Robert Sindall
______________________________
Home:  <mailto:robert@t...> robert@t...
Hotmail:  <mailto:robert_sindall@h...> robert_sindall@h...
______________________________
Tel: +44 (0)1242 518955


 




  Return to Index