Datagrid Update

ahptintantnoun

New Member
I have been through several examples and this still doesn't work. I want to update a row in the datagrid. It updates the row but with the original values from the db not the values in the cell. I don't know why. Here is the code. Any Ideas<BR><BR>Imports sd = System.Data<BR>Imports sql = System.Data.SqlClient<BR><BR><BR>Public Class WebForm1<BR> Inherits System.Web.UI.Page<BR> Protected WithEvents dgInventory As System.Web.UI.WebControls.DataGrid<BR> Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents TextBox4 As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents lblstatus As System.Web.UI.WebControls.Label<BR> Protected WithEvents lblDepCode As System.Web.UI.WebControls.Label<BR> Protected WithEvents lblType As System.Web.UI.WebControls.Label<BR> Protected WithEvents lblDescription As System.Web.UI.WebControls.Label<BR> Protected WithEvents Button1 As System.Web.UI.WebControls.Button<BR> Protected WithEvents Label1 As System.Web.UI.WebControls.Label<BR> <BR><BR><BR>#Region " Web Form Designer Generated Code "<BR><BR> 'This call is required by the Web Form Designer.<BR> <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()<BR><BR> End Sub<BR><BR> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<BR> 'CODEGEN: This method call is required by the Web Form Designer<BR> 'Do not modify it using the code editor.<BR> InitializeComponent()<BR> End Sub<BR><BR>#End Region<BR><BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR> Dim DS As DataSet<BR> Dim strConn As String = "Server=dev01; Database=exhibitManagement; UID=sa; PWD=moremoney;"<BR> Dim myConnection As sql.SqlConnection = New sql.SqlConnection(strConn)<BR> Dim MyCommand As sql.SqlDataAdapter<BR> Dim sqlStr As String = "SELECT invID, Description, Type, Status from inventory where clientid = 1"<BR> MyCommand = New sql.SqlDataAdapter(sqlStr, myConnection)<BR> <BR> DS = New DataSet()<BR> MyCommand.Fill(DS, "Inventory")<BR> dgInventory.DataSource = DS<BR> dgInventory.DataBind()<BR> myConnection.Close()<BR> End Sub<BR><BR> Private Sub DataGrid1_EditCommand(ByVal source As Object, _<BR> ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) _<BR> Handles dgInventory.EditCommand<BR> dgInventory.EditItemIndex = e.Item.ItemIndex<BR> dgInventory.DataBind()<BR> End Sub<BR><BR> Private Sub DataGrid1_CancelCommand(ByVal source As Object, _<BR> ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) _<BR> Handles dgInventory.CancelCommand<BR> dgInventory.EditItemIndex = -1<BR> dgInventory.DataBind()<BR> End Sub<BR><BR> Public Sub DataGrid1_UpdateCommand(ByVal source As Object, _<BR> ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) _<BR> Handles dgInventory.UpdateCommand<BR><BR> Dim txtDescription As TextBox = e.Item.Cells(2).Controls(0)<BR> Dim txtType As TextBox = e.Item.Cells(3).Controls(0)<BR> Dim txtStatus As TextBox = e.Item.Cells(4).Controls(0)<BR> Dim txtinvID As TextBox = e.Item.Cells(1).Controls(0)<BR><BR> 'take values and update the db<BR> Dim sqlStr As String = "UPDATE Inventory Set Description = '" & txtDescription.Text & "', Type = '" & txtType.Text & "', Status = '" & txtStatus.Text & "' where invID = '" & txtinvID.Text & "'"<BR> Response.Write(sqlStr)<BR> Dim strConn As String = "Server=dev01; Database=exhibitManagement; UID=sa; PWD=moremoney;"<BR> Dim myConnection As sql.SqlConnection = New sql.SqlConnection(strConn)<BR> Dim MyCommand As sql.SqlCommand<BR><BR> MyCommand = New sql.SqlCommand(sqlStr, myConnection)<BR> myConnection.Open()<BR> MyCommand.ExecuteNonQuery()<BR> myConnection.Close()<BR> dgInventory.EditItemIndex = -1<BR> dgInventory.DataBind()<BR><BR><BR> End Sub<BR><BR> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<BR><BR> End Sub<BR><BR> Private Sub dgInventory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgInventory.SelectedIndexChanged<BR><BR> End Sub<BR>End Class<BR>Just for the hell of it...<BR><BR>In your Page_Load function cap the database read with a <BR>if(!IsPostBack)<BR>{<BR> //All of your original Page_Load code goes here.<BR>}<BR><BR>It may be an order of execution problem you're looking at. Just give it a shot, who knows. (I'm pretty new to this myself, but I've found that ASP will persist your data across postbacks, allowing you to modify it incrementally)I put this code in<BR> If Not IsPostBack Then<BR> BindDataGrid()<BR> End If<BR>And Everytime I click a button the whole grid disappears. I am guessing because it goes through the whole load_page funciton and since its a post back it doesn't rebind the grid. So maybe that is my problem if that data is persisting with the postback but how can I see my grid? Or maybe this isn't the problem at all. I am not sure. I know the db is being updated but not with the edited values in the text boxes on the grid.OK I have the ispostback checking I found out if I take the whold binding process like the one below and put it in its own procedure. Then I call this whole procedure after every event it works. The new data updates. This seems very costly in resources to rebind the whold grid every time. Maybe that is exactly what the typical databind command does but it wasn't working in this case. Any other way to save on the resources. I can just imagine what this piece of code would cost me when several hundred people hit this page. <BR>Sub BindDataGrid()<BR> Dim DS As DataSet<BR> Dim strConn As String = "Server=dev01; Database=exhibitManagement; UID=sa; PWD=moremoney;"<BR> Dim myConnection As sql.SqlConnection = New sql.SqlConnection(strConn)<BR> Dim MyCommand As sql.SqlDataAdapter<BR> Dim sqlStr As String = "SELECT invID, Description, Type, Status from inventory where clientid = 1 order by invID"<BR> MyCommand = New sql.SqlDataAdapter(sqlStr, myConnection)<BR><BR> DS = New DataSet()<BR> MyCommand.Fill(DS, "Inventory")<BR> dgInventory.DataSource = DS<BR> dgInventory.DataBind()<BR> myConnection.Close()<BR> DS = Nothing<BR> End Sub
 
Back
Top