Problem with update database in datagrid
I face a problem in update database while using datagrid. When i press the Edit button, textbox will display and let me edit the text inside the textbox, but when i press update button i can't update the data at the database. Below is my coding, can someone help me? Thanks.
Sub DataGrid1_EditCommand(sender As Object, e As DataGridCommandEventArgs)
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub
Sub DataGrid1_UpdateCommand(sender As Object, e As DataGridCommandEventArgs)
Dim id As String = CType(e.Item.Cells(0).Controls(0), TextBox).Text
Dim str As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Dim myConnection As New SqlConnection (ConfigurationSettings.AppSettings("ConnectionStri ng"))
Dim cmdStr As String = "update Student set stuName=@stuName where stuId='"+id+"'"
Dim cmd As New SqlCommand (cmdStr, myConnection)
cmd.Parameters.Add("@stuName", SqlDbType.VarChar, 50).Value = str
myConnection.Open()
cmd.ExecuteNonQuery()
myConnection.Close()
DataGrid1.EditItemIndex = -1
DataGrid1.DataBind()
End Sub
|