can't update data using gridview

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
In my page_load event, I have the following code:

<code>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try
If Not Page.IsPostBack Then
BindGrid()
Else
GridView1.DataSource = DirectCast(Session.Item("dtCustomers"), DataTable)
End If
Catch ex As Exception
Response.Write(ex.Message)
End Try

End Sub

</code>

While debugging, I have the following in the Immediate Window after the postback:

?GridView1.Rows.Count
10



Once I clicked the Edit button, it fired the RowEditing event which I have the following code:

<code>
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As

System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing

GridView1.EditIndex = e.NewEditIndex
GridView1.DataBind()

End Sub

</code>

Right at the End Sub, I had another breakpoint to examine the rows in the gridview to make sure they

were still there as follows:

?GridView1.Rows.Count
10



My html of the gridview looks like this:

<code>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="CustomerID" SortExpression="CustomerID">
<ItemTemplate>
<asp:HyperLink ID="hylCustomerID" runat="server" Target="_blank" Text='<%#

Bind("CustomerID") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"

SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Region" HeaderText="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" />
</Columns>
</asp:GridView>

</code>



Inside the RowUpdating event, I have the following code (which was shortened for brevity):

<code>

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As

System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

Dim dtCustomers As New DataTable

Try
Dim dr As DataRow = dtCustomers.NewRow
dr.Item("CustomerID") = GridView1.Rows(e.RowIndex).Cells(0).Text
dr.Item("CompanyName") = GridView1.Rows(e.RowIndex).Cells(1).Text
........
dtCustomers.Rows.Add(dr)
dtCustomers.Rows(0).SetModified()
Dim cls1 As New Class1
Dim intRecdsAffected As Int16 = cls1.UpdateCustomer(dtCustomers)
cls1 = Nothing
GridView1.EditIndex = -1
BindGrid()
Catch ex As Exception
Response.Write(ex.Message)
End Try

End Sub

</code>


When I try to examine the following line of code in the above event

"GridView1.Rows(e.RowIndex).Cells(0).Text", I get an empty string. Note below:

?gridview1.Rows(e.RowIndex).Cells(1).Text
""
?gridview1.Rows(e.RowIndex).Cells(0).Text
""
?gridview1.Rows(e.RowIndex).Cells(2).Text
""
?gridview1.Rows(e.RowIndex).Cells(3).Text
""
?gridview1.Rows(e.RowIndex).Cells(4).Text
""
?gridview1.Rows(e.RowIndex).Cells(5).Text
""

?GridView1.Rows.Count
10



Notice that I still have the 10 records in the gridview count at this point. I am not using any

DataSource controls, so I can't use the "NewValues" property of the gridview.

How can I properly retrieve the values inside the grid at this point? It seems very simple, but I am

obviously missing something here.
 
Do you want to update the existing row or add a new row?
If you are trying to update, then you should use
Dim dr As DataRow = dtCustomers.Rows[dtCustomers.EditItemIndex]
which returns the row which you are editing
Right now you are using
Dim dr As DataRow = dtCustomers.NewRow
which returns the newly added row and of course in case of update, cells have no values.
 
You are correct. However, the following code snippet:
<code>
GridView1.Rows(e.RowIndex).Cells(0).Text
</code>
should return the row being edited in the actual gridview. The above code gives me an empty string when it should give me the value in the gridview cell.

How come I am getting an empty string value for the gridview cell?
 
Now, I can update the data as expecting.
But I still have one problem.
When using ntext type and updating, an unhandled exception occurred: The data types ntext and nvarchar are incompatible in the equal to operator.
But when I change ntext type to nvarchar(max) everything goes fine.
How can I solve this problem without changing ntext to nvarchar(max)
 
Back
Top