Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 28th, 2004, 07:45 AM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't read values from editable datagrid

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




 
Old June 28th, 2004, 07:56 AM
Authorized User
 
Join Date: Oct 2003
Posts: 89
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I solved the problem. I was rebinding the datagrid everytime on page load. I forgot to add ' If Not Page.IsPostBack Then.....'

 
Old August 27th, 2004, 01:31 PM
Registered User
 
Join Date: Aug 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vnagol
Default

Hi,
I am doing the same thing on the grid. When I am trying to update a column, its not getting the value I entered in the textbox.
Getting NullReference error.
Referenced object 'txtNewDescription' has a value of 'Nothing'.
What should I do? Please reply to question.

Here is my code:
--------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UltraWebGrid1.Visible = False
        If Not Page.IsPostBack Then
            LoadData()
        End If
    End Sub
-----------
Sub dgTasks_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim objTask As New CELib.Master
        DataGrid1.ShowFooter = True
        Dim index As Integer = e.Item.ItemIndex
        Dim txtNewDescription As TextBox = e.Item.FindControl("txtNewDescription")
        'Dim txtNewDescription As TextBox = e.Item.Cells(2).FindControl("txtNewDescription")

        taskID = DataGrid1.DataKeys(e.Item.ItemIndex)

        Try
            objTask.Task_ID = taskID
            'If txtNewDescription.Text <> "" Then
            objTask.Task_Desc = txtNewDescription.Text
            'End If
            Dim retTaskID As Integer = objTask.SaveTask

        Catch ex As Exception
            'Catch Exception
        End Try
        DataGrid1.EditItemIndex = -1
        LoadData()
End Sub

=============Form=============
<asp:datagrid id="DataGrid1" runat="server" Width="464px" AutoGenerateColumns="False" ShowFooter="True"
                            OnEditCommand="dgTasks_Edit" OnCancelCommand="dgTasks_Cancel" OnUpdateCommand="dgTasks_Update"
                            OnItemCommand="doInsert" OnDeleteCommand="doDelete" DataKeyField="Task_ID">
                            <EditItemStyle BackColor="#FFFFFF"></EditItemStyle>
                            <AlternatingItemStyle BackColor="LightGray"></AlternatingItemStyle>
                            <HeaderStyle BackColor="LightGray"></HeaderStyle>
                            <Columns>
                                <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
                                    EditText="Edit" HeaderStyle-Font-Bold="True" />
                                <asp:TemplateColumn ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" HeaderText="Task" HeaderStyle-Font-Bold="True"
                                    FooterStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <%# Container.DataItem("Task_ID") %>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button Text="Add" CommandName="Insert" runat="server" />
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Description" HeaderStyle-Font-Bold="True">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem,"Task_Desc") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Task_Desc") %>'
                                                ID="Textbox1"/>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox id="txtNewDescription" runat="server" />
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Delete" HeaderStyle-Font-Bold="True">
                                    <ItemTemplate>
                                        <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:datagrid>
 
Old August 27th, 2004, 02:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'm not seeing a SQL statement. Here is how I do an update:
OleDbCommand objCommand = new OleDbCommand();
            TextBox txtTitle = new TextBox();
            TextBox txtNews = new TextBox();
            //string strTitle;
            string strNews;
            int intID;

            string strConn = System.Configuration.ConfigurationSettings.AppSett ings["newsDB"];
            OleDbConnection objConn = new OleDbConnection(strConn);
            string strSQL = "UPDATE [zNews] SET [the news]=@News WHERE ([znews].[the id] = @ID)";

            objCommand = new OleDbCommand(strSQL, objConn);

            txtNews = (TextBox)e.Item.Cells[2].Controls[0];
            strNews = txtNews.Text;
            objCommand.Parameters.Add("@News", OleDbType.VarChar, 49);
            objCommand.Parameters["@News"].Value = strNews;

            intID = (int)dgNews.DataKeys[e.Item.ItemIndex];
            objCommand.Parameters.Add("@ID", OleDbType.Integer);
            objCommand.Parameters["@ID"].Value = intID;

            objConn.Open();
            objCommand.ExecuteNonQuery();
            objConn.Close();
            objConn.Dispose();
            dgNews.EditItemIndex = -1;

            ShowDataGrid();

 
Old August 27th, 2004, 03:23 PM
Registered User
 
Join Date: Aug 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vnagol
Default

Never mind, I fixed it.

Quote:
quote:Originally posted by vnagol
 Hi,
I am doing the same thing on the grid. When I am trying to update a column, its not getting the value I entered in the textbox.
Getting NullReference error.
Referenced object 'txtNewDescription' has a value of 'Nothing'.
What should I do? Please reply to question.

Here is my code:
--------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UltraWebGrid1.Visible = False
        If Not Page.IsPostBack Then
            LoadData()
        End If
    End Sub
-----------
Sub dgTasks_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim objTask As New CELib.Master
        DataGrid1.ShowFooter = True
        Dim index As Integer = e.Item.ItemIndex
        Dim txtNewDescription As TextBox = e.Item.FindControl("txtNewDescription")
        'Dim txtNewDescription As TextBox = e.Item.Cells(2).FindControl("txtNewDescription")

        taskID = DataGrid1.DataKeys(e.Item.ItemIndex)

        Try
            objTask.Task_ID = taskID
            'If txtNewDescription.Text <> "" Then
            objTask.Task_Desc = txtNewDescription.Text
            'End If
            Dim retTaskID As Integer = objTask.SaveTask
    
        Catch ex As Exception
            'Catch Exception
        End Try
        DataGrid1.EditItemIndex = -1
        LoadData()
End Sub

=============Form=============
<asp:datagrid id="DataGrid1" runat="server" Width="464px" AutoGenerateColumns="False" ShowFooter="True"
                            OnEditCommand="dgTasks_Edit" OnCancelCommand="dgTasks_Cancel" OnUpdateCommand="dgTasks_Update"
                            OnItemCommand="doInsert" OnDeleteCommand="doDelete" DataKeyField="Task_ID">
                            <EditItemStyle BackColor="#FFFFFF"></EditItemStyle>
                            <AlternatingItemStyle BackColor="LightGray"></AlternatingItemStyle>
                            <HeaderStyle BackColor="LightGray"></HeaderStyle>
                            <Columns>
                                <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
                                    EditText="Edit" HeaderStyle-Font-Bold="True" />
                                <asp:TemplateColumn ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" HeaderText="Task" HeaderStyle-Font-Bold="True"
                                    FooterStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <%# Container.DataItem("Task_ID") %>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button Text="Add" CommandName="Insert" runat="server" />
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Description" HeaderStyle-Font-Bold="True">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem,"Task_Desc") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Task_Desc") %>'
                                                ID="Textbox1"/>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox id="txtNewDescription" runat="server" />
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Delete" HeaderStyle-Font-Bold="True">
                                    <ItemTemplate>
                                        <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:datagrid>
VN
 
Old May 9th, 2007, 11:16 AM
Registered User
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can you tell me how you fixed this?

Thanks


Quote:
quote:Originally posted by vnagol
 Never mind, I fixed it.

Quote:
quote:Originally posted by vnagol
 Hi,
I am doing the same thing on the grid. When I am trying to update a column, its not getting the value I entered in the textbox.
Getting NullReference error.
Referenced object 'txtNewDescription' has a value of 'Nothing'.
What should I do? Please reply to question.

Here is my code:
--------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UltraWebGrid1.Visible = False
        If Not Page.IsPostBack Then
            LoadData()
        End If
    End Sub
-----------
Sub dgTasks_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim objTask As New CELib.Master
        DataGrid1.ShowFooter = True
        Dim index As Integer = e.Item.ItemIndex
        Dim txtNewDescription As TextBox = e.Item.FindControl("txtNewDescription")
        'Dim txtNewDescription As TextBox = e.Item.Cells(2).FindControl("txtNewDescription")

        taskID = DataGrid1.DataKeys(e.Item.ItemIndex)

        Try
            objTask.Task_ID = taskID
            'If txtNewDescription.Text <> "" Then
            objTask.Task_Desc = txtNewDescription.Text
            'End If
            Dim retTaskID As Integer = objTask.SaveTask
    
        Catch ex As Exception
            'Catch Exception
        End Try
        DataGrid1.EditItemIndex = -1
        LoadData()
End Sub

=============Form=============
<asp:datagrid id="DataGrid1" runat="server" Width="464px" AutoGenerateColumns="False" ShowFooter="True"
                            OnEditCommand="dgTasks_Edit" OnCancelCommand="dgTasks_Cancel" OnUpdateCommand="dgTasks_Update"
                            OnItemCommand="doInsert" OnDeleteCommand="doDelete" DataKeyField="Task_ID">
                            <EditItemStyle BackColor="#FFFFFF"></EditItemStyle>
                            <AlternatingItemStyle BackColor="LightGray"></AlternatingItemStyle>
                            <HeaderStyle BackColor="LightGray"></HeaderStyle>
                            <Columns>
                                <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
                                    EditText="Edit" HeaderStyle-Font-Bold="True" />
                                <asp:TemplateColumn ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Center" HeaderText="Task" HeaderStyle-Font-Bold="True"
                                    FooterStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <%# Container.DataItem("Task_ID") %>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button Text="Add" CommandName="Insert" runat="server" />
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Description" HeaderStyle-Font-Bold="True">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem,"Task_Desc") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Task_Desc") %>'
                                                ID="Textbox1"/>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox id="txtNewDescription" runat="server" />
                                    </FooterTemplate>
                                </asp:TemplateColumn>
                                <asp:TemplateColumn HeaderText="Delete" HeaderStyle-Font-Bold="True">
                                    <ItemTemplate>
                                        <asp:Button CommandName="Delete" Text="Delete" ID="btnDel" Runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:datagrid>
VN





Similar Threads
Thread Thread Starter Forum Replies Last Post
editable datagrid column sarah lee ASP.NET 1.0 and 1.1 Basics 3 September 7th, 2006 02:45 PM
editable datagrid .............help maddy137 ASP.NET 1.0 and 1.1 Professional 0 May 17th, 2006 07:33 PM
DataKey error in Editable datagrid hoailing22 ASP.NET 1.0 and 1.1 Basics 2 June 10th, 2005 10:51 PM
Convert Fully Editable DataGrid To F Edit Gridview gtjr92 ASP.NET 2.0 Basics 0 May 4th, 2005 12:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.