locked
EditItemTemplate value returns null when looping on rows in the grid RRS feed

  • Question

  • User1832772108 posted

    Hello,

    I am a newbie in C# and this is my first project with ASP .NET GridView (or with anything C#).

    I have been struggling with this issue for several days and read many articles and forum posts and answers, as well as tried many solutions but nothing works.

    The solution flow:

    • Query from SQL Server table on page load into gvPM (grid1) while gvPMgrp (grid2) is not visible.
    • Only the first 3 columns (a checkbox, Account number and Account name) are visible, the rest are not visible.  The table is very wide so the grid columns are grouped by numbers appended to their header name and in RowDataBound the logic turns visibility of the columns on / off based on the headers' suffix
    • The user checks rows to be selected and selects a group number from a drop down list.
    • After SELECT button click, the (checked) selected rows are displayed in EDIT mode in a gvPMgrp (grid2) and gvPM (grid1) is not visible.
    • On UPDATE button click, I loop in foreach (GridViewRow row in gvPMgrp.Rows) to get the values from the text boxes that were edited and send them as parameters to a stored procedure for update.

    Everything works except that this point I always get nulls in the cell and the following error:

    System.NullReferenceException: Object reference not set to an instance of an object

    I tried to get viewState into data tables that were initiated in Page_load.

    For the sake of shortening this post, I have pasted only a subset of the columns in both grids in the markup, the whole code behind is pasted though.

    I also removed all of my failed attempts remnants (they were commented out but it would have appeared to messy)

    Something basic is wrong but I can't find it for many days, I hope that someone can pinpoint the issue, any help is much appreciated!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Configuration;
    
    public partial class _PM : System.Web.UI.Page
    {
        string conStr = @"Data Source="";Initial Catalog=""; Integrated Security=true";   
        string grp = string.Empty;
        string acctNum = string.Empty;
        string acctName = string.Empty;
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {         
                showData();
                btnSelect.Visible = true;
            }        
        }    
    
        private void showData()
        { 
            string qry = string.Empty;
            
            if ((acctNum.Length > 0) | (acctName.Length > 0)) //Grid for selected rows 
            {
                qry = "dbo.uspPMgetAcctData";
                SqlConnection con = new SqlConnection(conStr);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = qry;
                cmd.Parameters.Add("@acctNum", SqlDbType.Text).Value = acctNum.Trim();
                cmd.Parameters.Add("@acctName", SqlDbType.Text).Value = acctName.Trim();
    
                con.Open();
                gvPMgrp.EmptyDataText = "No Records Found";
    
                gvPM.Visible = false;
                gvPMgrp.Visible = true;
        
    
                gvPMgrp.DataSource = cmd.ExecuteReader();
                gvPMgrp.DataBind();           
                con.Close();            
            }
            else  //Grid for all rows
            {  
                ddlGrpList.SelectedValue = "x";
                qry = "dbo.uspPMgGetAllData";
                SqlConnection con = new SqlConnection(conStr);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = qry;
    
                con.Open();
                gvPM.EmptyDataText = "No Records Found";
    
                gvPM.Visible = true;
                gvPMgrp.Visible = false;
    
                gvPM.DataSource = cmd.ExecuteReader();           
                gvPM.DataBind();            
                con.Close();
    
                btnSelect.Visible = true;
            }
        }
         
        protected void btnUpdateClick(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(conStr);
            con.Open();
            SqlCommand cmd = new SqlCommand("dbo.[uspPMupdtAcctData]", con);
            cmd.CommandType = CommandType.StoredProcedure;
    
            foreach (GridViewRow row in gvPMgrp.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    string AcctNum = gvPMgrp.DataKeys[row.RowIndex].Value.ToString();
    				
    				cmd.Parameters.Add("@Account_0", SqlDbType.VarChar).Value = AcctNum;
    				cmd.Parameters.AddWithValue("@Cost_to_Liquidate_0", (gvPMgrp.Rows[row.RowIndex].FindControl("txtCost_to_Liquidate_0"))).ToString().Trim();
    				cmd.Parameters.AddWithValue("@Credit_Rating_040820_0", (gvPMgrp.Rows[row.RowIndex].FindControl("txtCredit_Rating_040820_0")));
    				cmd.ExecuteNonQuery();
                }
            }
            con.Close();
            gvPMgrp.EditIndex = -1;
            showData();
        }
    
        protected void btnSelectClick(object sender, EventArgs e)
        {
            acctNum = "''";
            
            btnSelect.Visible = false;
            btnUpdate.Visible = true;
            btnCancel.Visible = true;
            
            grp = ddlGrpList.SelectedIndex.ToString().Trim();        
    
            foreach (GridViewRow row in gvPM.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox cb = (CheckBox)(row.FindControl("cbSelect"));
    
                    if (cb.Checked == true)
                    {
                        acctNum += "','" + row.Cells[1].Controls.OfType<Label>().FirstOrDefault().Text;
                    }
                }
            }
    
            string AcctNum  = this.txtAcctNum.Text.Trim();
            string AcctName = this.txtAcctName.Text.Trim();
    
            if( (!string.IsNullOrEmpty(AcctNum) ) && (AcctNum != "Account Number") )
            {
                if (AcctNum.Contains(","))
                {
                    AcctNum = AcctNum.Replace(",", "','");
                }
                acctNum += "','" + AcctNum;
            }
    
            if (!string.IsNullOrEmpty(AcctName))
            {
                if (AcctName == "Account Name")
                {
                    AcctName = "";
                }
            }
    
            acctName = AcctName;    
            showData();       
        }
    
        protected void btnDisplayClick(object sender, EventArgs e)
        {
            // Reset groups and accounts info
            grp = "x";
            txtAcctNum.Text = string.Empty;
            txtAcctName.Text = string.Empty;
    
            btnSelect.Visible = true;
            btnUpdate.Visible = false;
            btnCancel.Visible = false;        
            showData();
        }
    
        protected void btnCancelClick(object sender, EventArgs e)
        {
            grp = "x";
            btnSelect.Visible = true;
            btnUpdate.Visible = false;
            btnCancel.Visible = false;
            //gvPMgrp.Columns[0].Visible = true;
            gvPMgrp.EditIndex = -1;        
            showData();
        }
        protected void gvPM_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < gvPM.Columns.Count; i++)
            {
                string header = gvPM.Columns[i].HeaderText;
                string cleanHeader = string.Empty;
                string colGrp = string.Empty;
    
                if ((!string.IsNullOrEmpty(header)) && (header.Length > 1))
                {
                    if (e.Row.RowType == DataControlRowType.Header)
                    {
                        colGrp = header.Substring(header.Length - 1, 1).Trim();
    
                        if (colGrp == grp)
                        {
                            gvPM.Columns[i].Visible = true;
                        }
                        else if (grp == "x")
                        {
                            if (i <= 2)
                            {
                                gvPM.Columns[i].Visible = true;
                            }
                            else
                            {
                                gvPM.Columns[i].Visible = false;
                            }
                        }
    
                        cleanHeader = header.Substring(0, header.Length - 2).Trim().Replace("_", " ");
    
                        if (cleanHeader.ToLower().Contains(" date "))
                        {
                            cleanHeader += "<br/>(Date: MM/dd/yyyy)";
                        }
                        else if (cleanHeader.ToLower().Contains("cost")
                                || cleanHeader.ToLower().Contains("balance")
                                || ((cleanHeader.ToLower().Contains("value")) && (!cleanHeader.ToLower().Contains("no value")))
                                || cleanHeader.ToLower().Contains("total")
                                )
                        {
                            cleanHeader += "<br/>(Currency: $0.00)";
                        }
                        else if ( (cleanHeader.ToLower().Contains(" to ")) && (!cleanHeader.ToLower().Contains("cost")) )
                        {
                            cleanHeader += "<br/>(Percentage: 0.0%)";
                        }
    
                        e.Row.Cells[i].Text = cleanHeader;
                    }
                }
            }
        }
        protected void gvPMgrp_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {           
                for (int i = 0; i < gvPMgrp.Columns.Count; i++)
                {
                string header = gvPMgrp.Columns[i].HeaderText;
                string cleanHeader = string.Empty;
                string colGrp = string.Empty;
    
                if ((!string.IsNullOrEmpty(header)) && (header.Length > 1))
                {
                        colGrp = header.Substring(header.Length - 1, 1).Trim();
    
                        if (colGrp == grp)
                        {
                            gvPMgrp.Columns[i].Visible = true;
                        }
                        else if (grp == "x")
                        {
                            if (i <= 2)
                            {
                                gvPMgrp.Columns[i].Visible = true;
                            }
                            else
                            {
                                gvPMgrp.Columns[i].Visible = false;
                            }
                        }
    
                        cleanHeader = header.Substring(0, header.Length - 2).Trim().Replace("_", " ");
    
                        if (cleanHeader.ToLower().Contains(" date "))
                        {
                            cleanHeader += "<br/>(Date: MM/dd/yyyy)";
                        }
                        else if (cleanHeader.ToLower().Contains("cost")
                                || cleanHeader.ToLower().Contains("balance")
                                || ((cleanHeader.ToLower().Contains("value")) && (!cleanHeader.ToLower().Contains("no value")))
                                || cleanHeader.ToLower().Contains("total")
                                )
                        {
                            cleanHeader += "<br/>(Currency: $0.00)";
                        }
                        else if( (cleanHeader.ToLower().Contains(" to ")) && (!cleanHeader.ToLower().Contains("cost")) )
                        {
                            cleanHeader += "<br/>(Percentage: 0.0%)";
                        }
    
                        e.Row.Cells[i].Text = cleanHeader;
                    }
                }
            }
        }
    }
    <%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" CodeFile="PMgrp.aspx.cs" Inherits="_PM" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">  
                <h1>Portfolio Monitoring</h1>
                    <table>
                        <tr>
                            <th style="text-align: left; line-height: 20px;">
                                Account Number
                            </th>
                            <th style="text-align: left; line-height: 20px;">
                                Account Name
                            </th>
                        </tr>
    				    <tr>
    					    <td>
    						    <asp:TextBox ID="txtAcctNum" Text="Account Number" label="Account Number" runat="server" Width="145px" Height="20px"></asp:TextBox>
    					    </td>
    					    <td>
    						    <asp:TextBox ID="txtAcctName" runat="server" Text="Account Name" Width="145px" Height="20px"></asp:TextBox>					    
                            </td>
                        </tr>
                        <tr>
                            <td>
    						    <asp:Button ID="btnSelect" runat="server" Text="Select Account(s)" CommandName="Edit" onclick="btnSelectClick" Width="145px" Height="20px" style="margin-right: 1px"/>
    					    </td>                    
                            <td>
    						    <asp:Button ID="btnDisplay" runat="server" Text="Display All Accounts" onclick="btnDisplayClick" Width="145px" Height="20px"/>
    					    </td>
                        </tr>
                        <tr>
                        <td>
    						<asp:Button ID="btnUpdate" runat="server" Text="Update" onclick="btnUpdateClick" Width="145px" Visible = "false" Height="20px"/>					
    					</td>
                        <td>
    						<asp:Button ID="btnCancel" runat="server" Text="Cancel Edit" onclick="btnCancelClick" Width="145px" Visible = "false" Height="20px"/>
    					</td>
                    </tr>
                    <tr>
                        <td>
    					    <asp:DropDownList ID="ddlGrpList" runat="server" DataSourceID="GrpsList"                             
                                DataTextField="GroupName" DataValueField="GroupNum" AutoPostBack="True" 
                                Height="20px" Width="145px" >
                                <asp:ListItem Value="GroupNum">GroupName</asp:ListItem>
                            </asp:DropDownList>
                        
                            <asp:SqlDataSource ID="GrpsList" runat="server" 
                                ConnectionString="Data Source="";Initial Catalog="";Integrated Security=True" 
                                ProviderName="System.Data.SqlClient" 
                                SelectCommand="EXEC uspPMgridGrps">    
                            </asp:SqlDataSource>
                            
                        </td>                    
                    </tr>
    			</table>       
           <div style="width: 100%; height: 700px; overflow: scroll"">
            <asp:GridView 
    			ID="gvPM" 
    			runat="server" 
    			autogeneratecolumns="False" 
    			datakeynames="Account_0"    
                onRowDataBound="gvPM_RowDataBound"          
                EnableViewState="true"
    
                BackColor="White" 
                BorderColor="#CCCCCC" 
    			BorderStyle="Solid" 
    			BorderWidth="1px" 
    			CellPadding="3" 
    			HorizontalAlign="Left">			
    			
                <FooterStyle BackColor="White" ForeColor="#000066" />
                <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" VerticalAlign="Top" />
               
                <RowStyle ForeColor="#000066" HorizontalAlign="Left" VerticalAlign="Top" />
                <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />			
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#007DBB" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#00547E" />
    			            
                <Columns>
                    <asp:templatefield HeaderText="">
                        <itemtemplate>
                            <asp:checkbox ID="cbSelect" runat="server"></asp:checkbox>
                        </itemtemplate>
                    </asp:templatefield>
    
               		<asp:TemplateField HeaderText="Account_x">
    			        <ItemTemplate>
    				        <asp:Label Text='<%# Eval("Account_0")%>' runat="server" />
    			        </ItemTemplate>
    			        <EditItemTemplate>
    				        <asp:Label ID="txtAccount_0" Text='<%# Eval("Account_0") %>' runat="server" />
    			        </EditItemTemplate>
    		        </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Customer Name CFMAST_x">
    			        <ItemTemplate>
    				        <asp:Label Text='<%# Eval("Customer_Name_CFMAST_0")%>' runat="server" />
    			        </ItemTemplate>
    			        <EditItemTemplate>
    				        <asp:Label ID="txtCustomer_Name_CFMAST_0" Text='<%# Eval("Customer_Name_CFMAST_0") %>' runat="server" />
    			        </EditItemTemplate>
    		        </asp:TemplateField>
                 
                <asp:TemplateField HeaderText="Cost_to_Liquidate_0" visible="false">
    					<ItemTemplate>
    						<asp:TextBox Text='<%# Eval("Cost_to_Liquidate_0","{0:c}")%>' runat="server" />
    					</ItemTemplate>
    					<EditItemTemplate>
    						<asp:TextBox ID="txtCost_to_Liquidate_0" Text='<%# Eval("Cost_to_Liquidate_0","{0:c}") %>' runat="server" />
    					</EditItemTemplate>
    				</asp:TemplateField>
    
    <asp:TemplateField HeaderText="Credit_Rating_040820_0" visible="false">
    					<ItemTemplate>
    						<asp:TextBox Text='<%# Eval("Credit_Rating_040820_0")%>' runat="server" />
    					</ItemTemplate>
    					<EditItemTemplate>
    						<asp:TextBox ID="txtCredit_Rating_040820_0" Text='<%# Eval("Credit_Rating_040820_0") %>' runat="server" />
    					</EditItemTemplate>
    				</asp:TemplateField>
    				 </Columns>
            </asp:GridView>        
    		   <br />
               <br />
               <br />
               <br />
               <br />
               <br />
               <asp:GridView ID="gvPMgrp"
    			runat="server" 
    			autogeneratecolumns="False" 
    			datakeynames="Account_0"    
                onRowDataBound="gvPMgrp_RowDataBound"        
                EnableViewState="true"
                visible="false"
                BackColor="White" 
                BorderColor="#CCCCCC" 
    			BorderStyle="Solid" 
    			BorderWidth="1px" 
    			CellPadding="3" 
    			HorizontalAlign="Left">			
    			
                <FooterStyle BackColor="White" ForeColor="#000066" />
                <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" VerticalAlign="Top" />
               
                <RowStyle ForeColor="#000066" HorizontalAlign="Left" VerticalAlign="Top" />
                <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />			
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#007DBB" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#00547E" />
    			            
                <Columns>
               		<asp:TemplateField HeaderText="Account_x">
    			        <ItemTemplate>
    				        <asp:Label Text='<%# Eval("Account_0")%>' runat="server" />
    			        </ItemTemplate>
    			        <EditItemTemplate>
    				        <asp:Label ID="txtAccount_0" Text='<%# Eval("Account_0") %>' runat="server" />
    			        </EditItemTemplate>
    		        </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="Customer Name CFMAST_x">
    			        <ItemTemplate>
    				        <asp:Label Text='<%# Eval("Customer_Name_CFMAST_0")%>' runat="server" />
    			        </ItemTemplate>
    			        <EditItemTemplate>
    				        <asp:Label ID="txtCustomer_Name_CFMAST_0" Text='<%# Eval("Customer_Name_CFMAST_0") %>' runat="server" />
    			        </EditItemTemplate>
    		        </asp:TemplateField>
                 
                <asp:TemplateField HeaderText="Cost_to_Liquidate_0" visible="false">
    					<ItemTemplate>
    						<asp:TextBox Text='<%# Eval("Cost_to_Liquidate_0","{0:c}")%>' runat="server" />
    					</ItemTemplate>
    					<EditItemTemplate>
    						<asp:TextBox ID="txtCost_to_Liquidate_0" Text='<%# Eval("Cost_to_Liquidate_0","{0:c}") %>' runat="server" />
    					</EditItemTemplate>
    				</asp:TemplateField>
    
    <asp:TemplateField HeaderText="Credit_Rating_040820_0" visible="false">
    					<ItemTemplate>
    						<asp:TextBox Text='<%# Eval("Credit_Rating_040820_0")%>' runat="server" />
    					</ItemTemplate>
    					<EditItemTemplate>
    						<asp:TextBox ID="txtCredit_Rating_040820_0" Text='<%# Eval("Credit_Rating_040820_0") %>' runat="server" />
    					</EditItemTemplate>
    				</asp:TemplateField>
    				  </Columns>
            </asp:GridView>       
    		</div>
    			
        </form>
    </body>
    </html>



    </form>
    </body>
    </html>

    Wednesday, July 1, 2020 6:37 PM

Answers

  • User-1330468790 posted

    Hi aspdotnet1984,

    The problem is what the error message illustrates: System.NullReferenceException: Object reference not set to an instance of an object.

    Problem:

    You are trying to access the textbox "txtCost_to_Liquidate_0" in the grid view row.

     <asp:TemplateField HeaderText="Cost_to_Liquidate_0" visible="false">
            <ItemTemplate>
                    <asp:TextBox Text='<%# Eval("Cost_to_Liquidate_0","{0:c}")%>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
    	        <asp:TextBox ID="txtCost_to_Liquidate_0" Text='<%# Eval("Cost_to_Liquidate_0","{0:c}") %>' runat="server" />
    	</EditItemTemplate>
    </asp:TemplateField>

    However, you have only assigned an ID for the text box in the "EditItemTemplate" but not for the text box in the "ItemTemplate". When you fetch the data from that textbox, note that now the grid view is not in edit mode, you could only access the text box in the normal mode. Therefore, you could not find the text box "txtCost_to_Liquidate_0".

    Solution: 

    There are two approaches that you coud adopt to solve the problem.

    1. Assign the text box in the "ItemTemplate" with an ID and use this ID to access the text box.
    2. Change the mode of the GridView control "gvPMgrp" to "Edit" when you display this grid view.

    What I suggest is the first way since it is a common way we take. Each control should have an ID so that you could access it whenever you need to.

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 2, 2020 7:13 AM

All replies

  • User-1330468790 posted

    Hi aspdotnet1984,

    The problem is what the error message illustrates: System.NullReferenceException: Object reference not set to an instance of an object.

    Problem:

    You are trying to access the textbox "txtCost_to_Liquidate_0" in the grid view row.

     <asp:TemplateField HeaderText="Cost_to_Liquidate_0" visible="false">
            <ItemTemplate>
                    <asp:TextBox Text='<%# Eval("Cost_to_Liquidate_0","{0:c}")%>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
    	        <asp:TextBox ID="txtCost_to_Liquidate_0" Text='<%# Eval("Cost_to_Liquidate_0","{0:c}") %>' runat="server" />
    	</EditItemTemplate>
    </asp:TemplateField>

    However, you have only assigned an ID for the text box in the "EditItemTemplate" but not for the text box in the "ItemTemplate". When you fetch the data from that textbox, note that now the grid view is not in edit mode, you could only access the text box in the normal mode. Therefore, you could not find the text box "txtCost_to_Liquidate_0".

    Solution: 

    There are two approaches that you coud adopt to solve the problem.

    1. Assign the text box in the "ItemTemplate" with an ID and use this ID to access the text box.
    2. Change the mode of the GridView control "gvPMgrp" to "Edit" when you display this grid view.

    What I suggest is the first way since it is a common way we take. Each control should have an ID so that you could access it whenever you need to.

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 2, 2020 7:13 AM
  • User1832772108 posted

    Thank you Sean!

    After so many days of frustration, you saved me !  :D

    I went with option #1 as your suggestion and it worked; I got this to move forward, I am encountering datatype conversions errors now but that's a completely different issue so I am happy to continue debugging :)

    Solution: 

    There are two approaches that you could adopt to solve the problem.

    1. Assign the text box in the "ItemTemplate" with an ID and use this ID to access the text box.

    Thanks again!!

    Thursday, July 2, 2020 8:42 PM
  • User1832772108 posted

    Hi Sean,

    I have implemented your solution and overcame the datatypes issues:

    In the update function:

    cmd.Parameters.AddWithValue("@Cost_to_Liquidate_0", (gvPM.Rows[row.RowIndex].FindControl("Cost_to_Liquidate_0").ToString().Trim()));

    In the markup:

    <asp:TemplateField HeaderText="Cost_to_Liquidate_0" visible="false">
           <ItemTemplate>
               <asp:TextBox ID="Cost_to_Liquidate_0" Text='<%# Eval("Cost_to_Liquidate_0")%>' runat="server" />
          </ItemTemplate>
          <EditItemTemplate>
              <asp:TextBox ID="txtCost_to_Liquidate_0" Text='<%# Eval("Cost_to_Liquidate_0") %>' runat="server" />
          </EditItemTemplate>
    </asp:TemplateField>

    This is for all the columns. I also went back to one grid only after realizing it is not the problem.

    The rest of the code remain as initially posted, But now when passing the values to the stored procedure I only get the value: "System.Web.UI.WebControls.TextBox" passed,

    and when I go back to the EditItemTemplate ID I get the NULL reference error again.

    Do you happen to know what am I doing wrong? I can't seem to grab the value of the textBox.

    Your help is greatly appreciated !

     

    Thank you

    Monday, July 6, 2020 7:33 PM
  • User1832772108 posted

    Hi again,

    Never mind, I have changed the code to show as below:

    TextBox tCost_to_Liquidate_0 = (TextBox)gvPM.Rows[row.RowIndex].FindControl("Cost_to_Liquidate_0");
    string Cost_to_Liquidate_0 = tCost_to_Liquidate_0.Text;

    cmd.Parameters.AddWithValue("@Cost_to_Liquidate_0", Cost_to_Liquidate_0);

    And it passed the right value and updated the table correctly.

    Thanks anyways,

    All the best!

    Tuesday, July 7, 2020 12:07 AM