Link to home
Start Free TrialLog in
Avatar of ColoMtns
ColoMtnsFlag for United States of America

asked on

Won't repopulate datagrid after attempting to edit a record

Hi,
I have been writing classic asp for a few years and am just now starting my (what is turning out to be a painful) transition to .net.  I guess I say that to admit this may be a simple oversight on my part.

I will include the code here, but here's what happens:
1. the initial page load works correctly, filling the datagrid with the proper SQL data
2. when I click an "edit" button, the page refreshes, but with no datagrid.
3. there are no errors at all, it just doesn't build the datagrid.

Any help would be greatly appreciated.

=============================
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat=server>
Sub Page_Load(Sender As Object, E As EventArgs)
      If not Page.IsPostBack Then
            dataBuild()
      End If
End Sub

Sub dataBuild()
      '************************
      '***
      '*** Connect to the SQL database using the connection string defined in web.config
      '*** then select records from the database
      '***
      '*************************
      Dim strConnection As String
      strConnection = ConfigurationSettings.AppSettings("connectionstring")
      Dim myConnection As New SqlConnection(strConnection)
    Const strSQL as String = "SELECT LoginID, Name, Roles FROM UserLogins ORDER BY Name"
    Dim myCommand as New SqlCommand(strSQL, myConnection)
    myConnection.Open()
    datagrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    datagrid1.DataBind()
    myConnection.Close()
End Sub

Sub datagrid1_Edit(sender As Object, e As DataGridCommandEventArgs)
      datagrid1.EditItemIndex = e.Item.ItemIndex
      dataBuild()
End Sub

Sub datagrid1_Cancel(sender As Object, e As DataGridCommandEventArgs)
      datagrid1.EditItemIndex = -1
      dataBuild()
End Sub

Sub datagrid1_Update(sender As Object, e As DataGridCommandEventArgs)
      '*********************
      '*** Read in the values of the update row
      '*********************
      Dim iLoginID as Integer = e.Item.Cells(1).Text
      Dim strUsername as String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
      Dim strRole as String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
      '*********************
      '*** Construct the SQL statement using Parameters
      '*********************
      Dim strSQL as String = "UPDATE UserLogins SET " & _
            "Name = '" & strUsername & "', " & _
            "Roles = '" & strRole & "' " & _
            "WHERE LoginID = '" & iLoginID & "'"
      '*********************
      '*** Make the database connection and execute the Update
      '*********************
      Dim strConnection As String
      strConnection = ConfigurationSettings.AppSettings("connectionstring")
      Dim myConnection As New SqlConnection(strConnection)
    Dim myCommand as New SqlCommand(strSQL, myConnection)
    myConnection.Open()
      myCommand.ExecuteNonQuery()
      myConnection.Close()
      '*********************
      '*** Set the EditItemIndex to -1 and rebind the DataGrid
      '*********************
      datagrid1.EditItemIndex = -1
      dataBuild()        
End Sub
</script>

<html>
<head>
      <title>Edit Users</title>
      <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body class="body">
<form runat="server">
<h1 class="header1">
Edit Users page</h1>
<p class="para1">
<b>Users</b>
<br>
<asp:datagrid id="datagrid1" runat="server"
      Width="500"
    BackColor="#ccccff"
    BorderColor="#000000"
    ShowHeader="True"
    ShowFooter="False"
    CellPadding=2
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="11px"
      EditItemStyle-BackColor="#eeeeee"
      OnEditCommand="datagrid1_Edit"
      OnUpdateCommand="datagrid1_Update"
      OnCancelCommand="datagrid1_Cancel"
    EnableViewState="False"
      AutoGenerateColumns="False">
      <HeaderStyle BackColor="#000099" ForeColor="#ffffff" Font-Bold="True" HorizontalAlign="left" />
      <AlternatingItemStyle BackColor="#ffffff" />
      <Columns>
            <asp:EditCommandColumn EditText="Edit" ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" />
            <asp:BoundColumn DataField="LoginID" ReadOnly="True" HeaderText="ID" ItemStyle-HorizontalAlign="Left" />
            <asp:BoundColumn DataField="Name" ReadOnly="False" HeaderText="User Name" ItemStyle-HorizontalAlign="Left" />
            <asp:BoundColumn DataField="Roles" ReadOnly="False" HeaderText="Assigned Roles" ItemStyle-HorizontalAlign="Left" />
      </Columns>
</asp:datagrid>
</p>
</form>
</body>
</html>
Avatar of wtconway
wtconway

I've never really used the editcolumns but with ViewState enabled, couldn't you just set the EditItemIndex and not have to call the dataBuild() sub again?

Just humor me and give that a shot. Set EnableViewState="True" on the grid and comment the dataBuild() line on the edit handler.
ASKER CERTIFIED SOLUTION
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ColoMtns

ASKER

Hi wtconway,
I tried setting the EnableViewState to True, then commented out the dataBuild() line (just in the datagrid1_Edit sub... it refreshed the page and left the datagrid there this time, but not with any of the records in an "editable" mode.

I'll read through the article from nauman and see where that leads.

I'm certainly not committed to inline datagrid editing, I just need to find a way to list database records from a query, then be able to choose one to edit/update/delete.  From some other articles I had read (including the Quickstart method from Microsoft) this inline datagrid was being presented as the "right" way to do it... frankly, I like to do things the right way when it is practical, but I need this to work first.

Thanks
Ok. Figures. When I first started learning I read up on M$'s built-in editing features and thought the idea was cool but didn't like the implementation one bit. I would suggest reading that article. If you have any questions about that article I'll give a quick review and try to answer some of those questions.
Inline datagrid editing has some limitations:

1. Data is presented left to right and top to bottom. You are at risk of going out of space and an ugly editing interface if your page has multiple fields like textbox, radio button, textarea etc.

2. It is complex.

3. Your options are limited. You can not present a nicely designed form in-inlne editing.

3. Microsoft dont tell you what the right way is,; they tell you the options and it soley depends on you to choose the best. I dont mind using the inline datagrid editing if I have around 4-5 varchar fields in my table. However, in most cases you have fields of different types and it is always better to use the technique mentioned in the first article.

HTH, Nauman.
Hi Nauman,
Yes, this has definitely helped (the first, Real-World article).  I have just been putting little pieces together in between about 13 other projects, but it is going well so far.  I have a reader building the datagrid with an "edit" link in the right column on every row.  It passes the userID in the querystring to an EditUser page, selects that record from the database, pre-fills all of my text boxes and drop-downs like a charm.  I just have to write the Update sub now.

This is working much more like the classic ASP logic I'm used to anyway.

Thanks again.
ColoMtns,

Glad I was able to help :). Editing a record in this way is a standard style and widely used in many web languages. The main advantage of using this style is it provides an simple programming and user interface. Editing in DataGrid is very efficient and looks good but it can get really complex sometimes. Another thing that you can do is allow the editing of most common column in the datagrid and if user want to edit the detail then they can simply select the link from the first column and it will display the detailed edit form.

-Nauman.