Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.Net Datagrid Update Has Old Values

Status
Not open for further replies.

byrne1

Programmer
Aug 7, 2001
415
US
I have a datagrid that includes an Edit command button which allows users to edit some of the fields. When they click on the Update command, however, the Datagrid_Update sub still shows the old value for the field. I've included my code and would be very appreciative if someone could help me out.


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Conn = New SqlConnection()
Config = New AppSettingsReader()
Conn.ConnectionString = CType(Config.GetValue("ConnString", GetType(System.String)), String)
Conn.Open()

strSQL = "select * from Users"
DA1 = New SqlDataAdapter(strSQL, Conn)
DS1 = New DataSet()
i = DA1.Fill(DS1, "Users")
DataGrid1.DataSource = DS1.Tables("Users").DefaultView()

DataGrid1.DataBind()
End Sub


Public Sub DataGrid_Edit(ByVal Source As Object, ByVal E As DataGridCommandEventArgs)
DataGrid1.EditItemIndex = CInt(E.Item.ItemIndex)
DataGrid1.DataBind()
End Sub


Public Sub DataGrid_Update(ByVal Source As Object, ByVal E As DataGridCommandEventArgs)
Dim txtUserName As String = CType(E.Item.Cells(1).Controls(0), TextBox).Text
Dim index As Integer
Dim cmd As New SqlCommand()
index = E.Item.ItemIndex
Dim CaseNbr As String
CaseNbr = DataGrid1.DataKeys(index)

strSQL = "update Users set UserName='" & txtUserID & "' where UserID=123
cmd.CommandText = strSQL
cmd.Connection = Conn
cmd.ExecuteNonQuery()

DataGrid1.EditItemIndex = -1
DataGrid1.Databind
End Sub
 
Hmm, I think your problem is that you've got the wrong variable name in:

strSQL = "update Users set UserName='" & txtUserID & "' where UserID=123

Don't you want "txtUserName" and not "txtUserID"

This is the kinda mistake I'd make, too. I've been using ASP.NET for a few weeks now. It's cool, but I'm still pretty confused about a lot of stuff.
 
Sorry, that was a typo when transposing the code to this forum. The value I receive in the Update event is the old value and not the new value. I still have the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top