problem updating in datagrid ??

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default problem updating in datagrid ??

    The guts of the below asp.net vb code was pieced together from another
    thread - all due credit to it's original author. Thank you!


    I've modified it to maintain a small local Microsoft 2000 access DB
    via a datagrid control.


    The add and delete functions work great, but the edit does not actually
    update the database. I put in some displays and apparently on the first
    item in the grid gets set during editing, all others come back empty???



    Any help or information is appreciated..


    <code snippet below>


    %@ Page Language="VB" Debug=true%>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.OleDb" %>
    <%@ import Namespace="System.String" %>
    <%@ import Namespace="System.Web.Mail" %>


    <script runat="server">


    Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
    BindData()
    End If
    End Sub


    Public Sub BindData()
    Dim objConn as new
    OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
    Source=c:\webroot\mydb.mdb")
    objConn.Open()
    Dim oaUser As OleDbDataAdapter
    Dim UserDS as DataSet = New DataSet()
    oaUser = New OleDbDataAdapter("Select * FROM appworx", objConn)


    oaUser.Fill(UserDS,"appworx")
    objConn.Close


    UserGrid.DataSource = UserDS.Tables("appworx")
    UserGrid.DataBind()
    End Sub


    Public Sub UserGrid_Edit (Source As Object, E As
    DataGridCommandEventArgs)
    UserGrid.EditItemIndex = E.Item.ItemIndex
    BindData()
    End Sub


    Public Sub UserGrid_Cancel (Source As Object, E As
    DataGridCommandEventArgs)
    UserGrid.EditItemIndex = -1
    BindData()
    End Sub


    Public Sub UserGrid_Update (Source As Object, E As
    DataGridCommandEventArgs)
    Dim objConn as new
    OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
    Source=c:\webroot\mydb.mdb")
    Dim cmd As OleDbCommand = new OleDbCommand ("UPDATE appworx SET

    responsible=@responsible, status=@status, waitingon=@waitingon,
    comments=@comments WHERE chain = @chain", objConn)


    Dim schain As String = e.Item.Cells(2).Text
    Dim sresponsible As String = e.Item.Cells(3).Text
    Dim sstatus As String = e.Item.Cells(4).Text
    Dim swaitingon As String = e.Item.Cells(5).Text
    Dim scomments As String = e.Item.Cells(6).Text


    response.write(schain+"-"+sresponsible+"-"+sstatus+"-"+swait*ingon+"-"+scomments)



    cmd.Parameters.Add(new OleDbParameter("@chain", schain))
    cmd.Parameters.Add(new OleDbParameter("@responsible",
    sresponsible))
    cmd.Parameters.Add(new OleDbParameter("@status", sstatus))
    cmd.Parameters.Add(new OleDbParameter("@waitingon", swaitingon))



    cmd.Parameters.Add(new OleDbParameter("@comments", scomments))


    objConn.Open()
    cmd.ExecuteNonQuery()
    objConn.Close


    UserGrid.EditItemIndex = -1
    BindData()


    End Sub


    Public Sub UserGrid_Command(sender As Object, e As
    DataGridCommandEventArgs)
    Select (CType(e.CommandSource, LinkButton)).CommandName


    Case "Delete"
    Dim objConn as new
    OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
    Source=c:\webroot\mydb.mdb")
    Dim cmd As OleDbCommand = new OleDbCommand("DELETE FROM
    appworx WHERE chain = @chain", objConn)


    cmd.Parameters.Add(new OleDbParameter("@chain",
    e.Item.Cells(2).Text))
    objConn.Open()
    cmd.ExecuteNonQuery()
    objConn.Close
    Case Else
    ' Do Nothing


    End Select


    BindData()
    End Sub


    Public Sub UserGrid_ItemCreated(sender As Object, e As
    DataGridItemEventArgs)
    Select Case e.Item.ItemType
    Case ListItemType.Item, ListItemType.AlternatingItem,
    ListItemType.EditItem


    ' Add confirmation to Delete button
    Dim tblCell As TableCell
    Dim btnDelete As LinkButton


    tblCell = e.Item.Cells(1)
    btnDelete = tblCell.Controls(0)
    btnDelete.Attributes.Add("onclick", "return confirm('Are you
    sure you want to delete?');")
    End Select
    End Sub


    Public Sub AddUser_Click(sender As Object, e As EventArgs)


    chainLabel.Visible = true
    responsibleLabel.Visible = true
    statusLabel.Visible = true
    waitingonLabel.Visible = true
    commentsLabel.Visible = true
    addchain.Visible = true
    addresponsible.Visible = true
    addstatus.Visible = true
    addwaitingon.Visible = true
    addcomments.Visible = true
    AddNewUser.Visible = true
    AddCancel.Visible = true


    End Sub


    Public Sub AddNewUser_Click(sender As Object, e As EventArgs)
    Dim objConn as new
    OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data
    Source=c:\webroot\mydb.mdb")
    Dim cmd As OleDbCommand = new OleDbCommand("INSERT INTO appworx
    (chain, responsible,status,waitingon,comments)
    values(@chain,@responsible,@status,@waitingon,@com ments)", objConn)


    cmd.Parameters.Add(new OleDbParameter("@responsible",
    Addresponsible.Text))
    cmd.Parameters.Add(new OleDbParameter("@chain", Addchain.Text))
    cmd.Parameters.Add(new OleDbParameter("@status", Addstatus.Text))



    cmd.Parameters.Add(new OleDbParameter("@waitingon",
    Addwaitingon.Text))
    cmd.Parameters.Add(new OleDbParameter("@comments",
    Addcomments.Text))


    objConn.Open()
    cmd.ExecuteNonQuery()
    objConn.Close


    chainLabel.Visible = false
    responsibleLabel.Visible = false
    statusLabel.Visible = false
    waitingonLabel.Visible = false
    commentsLabel.Visible = false


    BindData()
    End Sub


    Public Sub AddCancel_Click(sender As Object, e As EventArgs)


    chainLabel.Visible = false
    responsibleLabel.Visible = false
    statusLabel.Visible = false
    waitingonLabel.Visible = false
    commentsLabel.Visible = false


    addchain.Visible = false
    addresponsible.Visible = false
    addstatus.Visible = false
    addwaitingon.Visible = false
    addcomments.Visible = false


    AddNewUser.Visible = false
    AddCancel.Visible = false


    ' Reset text fields (for next time)
    addchain.text = ""
    addresponsible.text = ""
    addstatus.text = ""
    addwaitingon.text = ""
    addcomments.text = ""


    End Sub


    </script>


    <html>
    <head>
    </head>
    <body>
    <form method="post" runat="server">
    <asp:button id="AddUser_Button" Text="Add New User" runat="server"
    onClick="AddUser_Click" />
    <table border=0>
    <tr><td>
    <asp:datagrid id="UserGrid" runat=server AutoGenerateColumns=false
    BorderStyle="Dotted" BorderWidth="2"
    BackgroundColor="red"
    CellPadding="5"
    Font-Name="Arial" Font-Size="8pt"
    OnEditCommand="UserGrid_Edit"
    OnCancelCommand="UserGrid_Cancel"
    OnUpdateCommand="UserGrid_Update"
    OnItemCommand="UserGrid_Command"
    OnItemCreated="UserGrid_ItemCreated">


    <HeaderStyle BackColor="#aaaadd">
    </HeaderStyle>


    <EditItemStyle BackColor="yellow">
    </EditItemStyle>


    <ItemStyle Wrap="false">
    </ItemStyle>


    <Columns>
    <asp:EditCommandColumn
    ButtonType ="LinkButton"
    CancelText = "Cancel"
    EditText = "Edit"
    UpdateText = "Update">
    </asp:EditCommandColumn>


    <asp:ButtonColumn
    HeaderText="Delete?"
    ButtonType="LinkButton"
    Text="Delete"
    CommandName="Delete"/>
    <asp:BoundColumn
    DataField = "chain"
    HeaderText = "chain"
    ReadOnly = true />
    <asp:BoundColumn
    DataField = "responsible"
    HeaderText = "responsible"/>
    <asp:BoundColumn
    DataField = "status"
    HeaderText = "status"/>
    <asp:BoundColumn
    DataField = "waitingon"
    HeaderText = "waitingon"/>
    <asp:BoundColumn
    DataField = "comments"
    HeaderText = "comments"/>
    </Columns>


    </asp:datagrid>
    </td><td valign="top">
    <table border=0 bgcolor=yellow>
    <tr><td><asp:label id="chainLabel" Text="chain:"
    visible="false" runat="server" /></td>
    <tr><td><asp:textbox id="Addchain" runat="server"
    visible="false" /></td>
    <tr><td><asp:label id="responsibleLabel" Text="responsible:"
    visible="false" runat="server" /></td>
    <tr><td><asp:textbox id="Addresponsible" runat="server"
    visible="false" /></td>
    <tr><td><asp:label id="statuslabel" Text="status:"
    visible="false" runat="server" /></td>
    <tr><td><asp:textbox id="Addstatus" runat="server"
    visible="false"/></td>
    <tr><td><asp:label id="waitingonLabel" Text="waiting on:"
    visible="false" runat="server" /></td>
    <tr><td><asp:textbox id="Addwaitingon" runat="server"
    visible="false" /></td>
    <tr><td><asp:label id="commentsLabel" Text="comments:"
    runat="server" visible="false" /></td>
    <tr><td><asp:textbox id="Addcomments" runat="server"
    visible="false" /></td>
    <tr><td><asp:button id ="AddNewUser" Text="Add New Record"
    runat="server" visible="false" onClick="AddNewUser_Click" />
    <td><asp:button id ="AddCancel" Text="Cancel" runat="server"
    visible="false" onClick="AddCancel_Click"/>
    </table>


    </td></table>
    </form>

    jason@cyberpine.com Guest

  2. Similar Questions and Discussions

    1. updating datagrid with JOIN query
      When making an editable datagrid, I've gotten pretty good and handling the function I call in the OnUpdateCommand event. I can update a table easily...
    2. Problem keeping the datagrid view intact after updating
      >NET 2003, vb code behind. I am having a problem with the look of a page with a datagrid. I have the grid populated, and it can be a couple of...
    3. Datagrid not updating database
      I am using a dropdown box to query a database and populate a datagrid. Everything works except the update button. When the update button is...
    4. Problem when updating a datagrid
      Using the sample walkthrough: Walkthrough: Using a DataGrid Web Control to Read and Write Data This is the Update command code: Private Sub...
    5. Updating database through a datagrid using checkboxes
      I need to update items in a database queue, so I am attempting to write a ASP.NET utility to do this instead of the users just typing SQL. I am...
  3. #2

    Default Re: problem updating in datagrid ??

    Your update is wrong. The reason it works for the first one is because
    your boundcolumn is set to readonly = "true."

    "Dim schain As String = e.Item.Cells(2).Text "
    will work only if the cell is readonly.

    The remainder should be:
    "Dim sresponsible As String = CType(e.Item.Cells(3).Controls(0),
    TextBox).Text"

    Good luck!

    Roy Guest

  4. #3

    Default Re: problem updating in datagrid ??


    Roy wrote:
    > Your update is wrong. The reason it works for the first one is
    because
    > your boundcolumn is set to readonly = "true."
    >
    > "Dim schain As String = e.Item.Cells(2).Text "
    > will work only if the cell is readonly.
    >
    > The remainder should be:
    > "Dim sresponsible As String = CType(e.Item.Cells(3).Controls(0),
    > TextBox).Text"
    >
    > Good luck!

    Thank you . The response.write display now matched what I changed, but
    the record still does update.

    jason@cyberpine.com Guest

  5. #4

    Default Re: problem updating in datagrid ??

    Good to hear it's all better now!

    Roy Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139