Hi,
I have an editable datagrid. For some reason the value that is entered in the textbox during edit mode/insert return an empty string.
When I run debug then name in both Update and Insert returns "".
I can't see the problem with the code. Can someone please help?
Thanks
ASPX:
<asp:datagrid id="Datagrid1" runat="server" ForeColor="DarkSlateBlue" BackColor="#CCCCCC" BorderWidth="3px"
BorderColor="#999999" BorderStyle="Solid" HeaderStyle-Font-Bold="True" ItemStyle-Wrap="False"
HorizontalAlign="Right" OnItemCreated="ItemCreated" AutoGenerateColumns="False"
CellPadding="4" Height="100%" Width="700px" Font-Size="Smaller" DataKeyField="Classify_ID"
OnEditCommand="EditRow" CellSpacing="2" OnCancelCommand="CancelRow" OnUpdateCommand="UpdateRow"
ShowFooter="True" HeaderStyle-CssClass="Size">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#000099"></SelectedItemStyle>
<AlternatingItemStyle Font-Size="Smaller"></AlternatingItemStyle>
<ItemStyle Font-Size="Smaller" Wrap="False" BackColor="White"></ItemStyle>
<HeaderStyle Font-Size="Smaller" Font-Bold="True" ForeColor="MediumTurquoise" CssClass="Size" BackColor="MidnightBlue"></HeaderStyle>
<FooterStyle BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="ID" Visible="true">
<ItemTemplate>
<%#container.dataitem ("Classify_ID")%>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="Linkbutton2" Runat="server" CommandName="Insert">Insert Class </asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="CLASSIFY">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Classify") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewClassify" Runat="server"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" id="txtClassify" TextMode="singleline" Text='<%# DataBinder.Eval(Container.DataItem, "classify") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="UPDATE CLASSIFY" CancelText="cancel"
EditText="edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="DELETE" HeaderText="DELETE" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
ASPX.
VB CODE:
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles Datagrid1.ItemCommand
Dim IDCCell As TableCell = e.Item.Cells(0)
Dim IClassifyId As String = IDCCell.Text
If e.CommandSource.commandname = "Delete" Then
DeleteCONTENT(e)
End If
If e.CommandSource.CommandName = "Insert" Then
Dim name As TextBox = e.Item.Cells(1).FindControl("txtNewClassify")
Dim strIn As String
strIn = "INSERT INTO CLASSIFY (CLASSIFY) VALUES(@CLASSIFY) " + "SET @Idc = @@Identity"
Dim CMD As New SqlCommand(strIn, oConn)
CMD.Parameters.Add("@CLASSIFY", name.Text.Trim)
' used for getting new record ID
CMD.Parameters.Add("@Idc", SqlDbType.Int)
CMD.Parameters("@Idc").Direction = ParameterDirection.Output
oConn.Open()
CMD.ExecuteNonQuery()
oConn.Close()
fillTable()
End If
End Sub
Sub UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim name As TextBox = e.Item.Cells(1).FindControl("txtClassify")
Dim IClassifyId As Integer = Datagrid1.DataKeys(e.Item.ItemIndex)
Dim strUpdate As String = "UPDATE CLASSIFY SET Classify=@Classify WHERE Classify_ID=@ID"
Dim CMD As New SqlCommand(strUpdate, oConn)
CMD.Parameters.Add("@Classify", name.Text)
CMD.Parameters.Add("@ID", IClassifyId)
oConn.Open()
CMD.ExecuteNonQuery()
oConn.Close()
Datagrid1.EditItemIndex = -1
Datagrid1.Visible = False
End Sub