How do I get a value from by gridview in C#

I have a web page with a list of divisions in a gridview. I have a separate area on the page that allows the user to enter a division and when the user clicks on the Add to Grid button, it needs to check if the division already exists and display a message to the user.

From everything that I have read, I should be able to do the following but it is not working.  tb is returning null yet it works in my RowUpdating event.  Any help is greatly appreciated!

Here is the code that I am trying to get to work:
                Boolean unique = true;
                TextBox tb;
                // Validate the Division doesn't already exist
                foreach (GridViewRow row in gvDivisions.Rows)

                    tb = row.FindControl("tbEdit") as TextBox;
                    if (tbDivision.Text.ToUpper() == tb.Text.ToUpper())
                        unique = false;

Open in new window

This code in my gridView_RowUpdating event works:
                // UPdate the values in the datatable
                GridViewRow row = gvDivisions.Rows[e.RowIndex];
                // get currently selected value
                TextBox tb = gvDivisions.Rows[e.RowIndex].FindControl("tbEdit") as TextBox;
                dtDivision.Rows[row.DataItemIndex]["Division"] = tb.Text.ToUpper();
                DropDownList ddl = gvDivisions.Rows[e.RowIndex].FindControl("ddlStatus") as DropDownList;
                dtDivision.Rows[row.DataItemIndex]["ActiveStatusFlag"] = ddl.SelectedValue;

                // Reset the edit index
                gvDivisions.EditIndex = -1;

                // Bind the data
                gvDivisions.DataSource = dtDivision;

Open in new window

Here is my gridview markup:
            <asp:GridView ID="gvDivisions" runat="server" CellPadding="5" 
                OnPageIndexChanging="gvDivisions_PageIndexChanging" OnRowCancelingEdit="gvDivisions_RowCancelingEdit"
                OnRowEditing="gvDivisions_RowEditing" OnRowUpdating="gvDivisions_RowUpdating"
                AlternatingRowStyle-BackColor="#0099CC" AlternatingRowStyle-BorderColor="Black" 
                AlternatingRowStyle-BorderStyle="Solid" AlternatingRowStyle-ForeColor="White" 
                AlternatingRowStyle-HorizontalAlign="Left" AlternatingRowStyle-VerticalAlign="Middle" 
                BorderStyle="Solid" HeaderStyle-BackColor="Silver" BorderColor="Black" 
                HeaderStyle-BorderColor="Black" HeaderStyle-BorderStyle="Solid" 
                HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" 
                HorizontalAlign="Left" RowStyle-BorderColor="Black" RowStyle-BorderStyle="Solid" 
                RowStyle-HorizontalAlign="Left" RowStyle-VerticalAlign="Middle" 
                EmptyDataText="No Divisions Found" AllowPaging="True">
                <AlternatingRowStyle HorizontalAlign="Left" VerticalAlign="Middle" BackColor="#0099CC" BorderColor="Black" 
                    BorderStyle="Solid" ForeColor="White"></AlternatingRowStyle>
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="Silver" BorderColor="Black" 
                <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="Black" BorderStyle="Solid"></RowStyle>
                    <asp:BoundField DataField="DivisionID" HeaderText="ID" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Division">
                            <asp:TextBox ID="tbEdit" runat="server" ToolTip="Division" Text='<%# Bind("Division") %>'
                                CssClass="TextBoxFormat" MaxLength="30"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="tbDivisionRequiredFieldValidator2" runat="server" 
                                ErrorMessage="Division is required" BackColor="Red" Font-Bold="True" ForeColor="White"
                                ControlToValidate="tbEdit" Display="Dynamic" validationgroup="Info"
                            <asp:Label ID="lblDivision" runat="server" Text='<%# Bind("Division") %>'></asp:Label>                       
                    <asp:TemplateField HeaderText="Active">
                            <asp:DropDownList ID="ddlStatus" runat="server">
                                <asp:ListItem Value="Y">ACTIVE</asp:ListItem>
                                <asp:ListItem Value="N">INACTIVE</asp:ListItem>
                            <asp:Label ID="lblActiveFlag" runat="server" Text='<%# Bind("ActiveStatusFlag") %>'></asp:Label>

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

From what I understand, you have tbEdit in the EditItemTemplate, so it works in the RowUpdating but in your ItemTemplate there is no tbEdit but just lblDivision. So you might want to find a Label "lblDivision".

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
Such a simple solution!  I can't tell you how long I have been looking at this.  Thanks for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.