GridView RowUpdating Problem

innocent1973
innocent1973 used Ask the Experts™
on
Hello Experts,
There is a problem on my commandtext string. Because it cannot find TextBox2 object.

Please help...

Regards
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection MyNewConnection3 = new SqlConnection();
        SqlCommand MyNewQuery3 = new SqlCommand();


        MyNewConnection3.ConnectionString = "Data Source=.;Initial Catalog=FOREVER;User ID=sa;Password=1";
        MyNewConnection3.Open();
        MyNewQuery3.Connection = MyNewConnection3;

        


       
        
       


        MyNewQuery3.CommandText = "SET DATEFORMAT DMY UPDATE IAS_ALLOCATIONS" +
                 " SET [AllQty] =" + GridView2.SelectedRow.FindControl("TextBox2").GetType() +
          " WHERE AllocationID='" + GridView1.SelectedRow.Cells[1].Text.Trim() + "' " +
          "AND Barcode='" + GridView2.SelectedRow.Cells[2].Text.Trim() + "' ";


        //SqlCommand cmd = new SqlCommand(strSQL, cn);
        //cmd.CommandTimeout = 0;
        //if (cn.State == ConnectionState.Closed)
        //{
        //    cn.Open();
        //}
        MyNewQuery3.ExecuteNonQuery();
        MyNewConnection3.Close();

    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:

I think you want to get the value from the TextBoc2 control...then instead of using GetType, you should use .Text

Change this => GridView2.SelectedRow.FindControl("TextBox2").GetType()
to this => ((TextBox)GridView2.SelectedRow.FindControl("TextBox2")).Text

It would be helpful if you could post the asp code as well...For an example of the asp code with FindControl usage, you can refer this link => http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx
Top Expert 2010
Commented:

If [AllQty] is a text column, then you have to place the Text value inside single quotes as below:

Line 20: " SET [AllQty] ='" + ((TextBox)GridView2.SelectedRow.FindControl("TextBox2")).Text + "'" +

Author

Commented:
Thanks for your reply.
AllQty is an integer column.

Author

Commented:
My complete source code for this page is like below:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditAllocations.aspx.cs" Inherits="EditAllocations" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="top: 3px; left: 2px; position: absolute; height: 657px; width: 1299px">
    
        <asp:Panel ID="Panel1" runat="server" BackColor="#000040" Height="655px" 
            Width="256px">
            <asp:Panel ID="Panel2" runat="server" BorderColor="#000040" 
        BorderStyle="Outset" Font-Bold="True" Font-Size="24pt" ForeColor="#000040" 
        HorizontalAlign="Center" 
        
        
        
    style="top: 2px; left: 260px; position: absolute; height: 77px; width: 988px">
                <asp:Image ID="Image1" runat="server" Height="42px" 
            ImageUrl="~/Images/FNlogo.png" Width="240px" />
                <br />
                EDIT/UPDATE ALLOCATIONS
            </asp:Panel>
            <asp:Panel ID="Panel3" runat="server" BorderStyle="Solid" ScrollBars="Vertical" 
                style="top: 90px; left: 260px; position: absolute; height: 236px; width: 403px">
                <asp:Label ID="Label1" runat="server" BackColor="#000040" Font-Bold="True" 
                    ForeColor="White" Height="29px" Text="PLANNED ALLOCATIONS" Width="386px"></asp:Label>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                    AutoGenerateDeleteButton="True" AutoGenerateSelectButton="True" 
                    BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" 
                    CellPadding="4" Font-Bold="True" Font-Size="9pt" 
                    onrowdeleting="GridView1_RowDeleting" onselectedindexchanged="GridView1_SelectedIndexChanged" 
                    >
                    <RowStyle BackColor="White" ForeColor="#003399" />
                    <Columns>
                        <asp:BoundField DataField="AllocationID" HeaderText="Allocation ID" />
                        <asp:BoundField DataField="StoreID" HeaderText="StoreID" />
                        <asp:BoundField DataField="AllocationDate" HeaderText="Allocation Date" 
                            HtmlEncodeFormatString="False" />
                        <asp:TemplateField HeaderText="Quantity">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Height="23px" Width="46px"></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Eval("AllQty") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                </asp:GridView>
            </asp:Panel>
            <asp:Panel ID="Panel4" runat="server" BorderStyle="Solid" ScrollBars="Both" 
                
                
                
                style="top: 93px; left: 675px; position: absolute; height: 504px; width: 627px">
                <asp:Label ID="Label3" runat="server" BackColor="#000040" Font-Bold="True" 
                    ForeColor="White" Height="25px" Text="ALLOCATION ITEM DETAILS" Width="549px"></asp:Label>
                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                    AutoGenerateEditButton="True" AutoGenerateSelectButton="True" BackColor="White" 
                    BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
                    onrowcancelingedit="GridView2_RowCancelingEdit" 
                    onrowdatabound="GridView2_RowDataBound" 
                    onrowediting="GridView2_RowEditing1" Font-Bold="True" Font-Size="8pt" onrowupdating="GridView2_RowUpdating"  
                    >
                    <RowStyle BackColor="White" ForeColor="#003399" />
                    <Columns>
                        <asp:BoundField DataField="smodel" HeaderText="Style" ReadOnly=true />
                        <asp:BoundField DataField="barcode" HeaderText="Barcode" ReadOnly=true />
                        <asp:BoundField DataField="class" HeaderText="Class" ReadOnly=true />
                        <asp:BoundField DataField="sbedentipi" HeaderText="Size Type" ReadOnly=true />
                        <asp:BoundField DataField="sbeden" HeaderText="Item Size" ReadOnly=true />
                        <asp:BoundField DataField="srenk" HeaderText="Color" ReadOnly=true />
                        <asp:TemplateField HeaderText="Qty">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("AllQty") %>' 
                                    Width="40px"></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("AllQty") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                </asp:GridView>
            </asp:Panel>
        </asp:Panel>
    
    </div>
    </form>
</body>
</html>

Open in new window

Author

Commented:
I have applied your code below:
 ((TextBox)GridView2.SelectedRow.FindControl("TextBox2")).Text

It worked perfect. But after update operation EditItemTempate textbox object still appears. When I click the cancel button then I get the proper ItemTemplate label value.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial