Prateek J

Prateek J

  • NA
  • 66
  • 822

Add new row to gridview not working inside repeater control

May 10 2019 12:16 PM
Hello, I'm using Gridview inside a repeater controlto select items for billing.
 
but when I try to add new row it says:
 
"Object reference not set to an instance of an object."
Here is my c# code for the same:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!this.IsPostBack)  
  4.     {  
  5.         this.getTableDetails();  
  6.     }  
  7. }  
  8. protected void getTableDetails()  
  9. {  
  10.     DataTable dt = new DataTable();  
  11.     SqlDataAdapter sda = new SqlDataAdapter("Select TableName From TableMaster where TableStatus='0'", constring);  
  12.     sda.Fill(dt);  
  13.     rptrTables.DataSource = dt;  
  14.     rptrTables.DataBind();  
  15. }  
  16. protected void rptrTables_ItemDataBound(object sender, RepeaterItemEventArgs e)  
  17. {  
  18.     GridView grdItemDetails = e.Item.FindControl("grdOrder"as GridView;  
  19.     DataTable dt = new DataTable();  
  20.     DataRow dr = null;  
  21.     dt.Columns.Add(new DataColumn("RowNumber"typeof(string)));  
  22.     dt.Columns.Add(new DataColumn("ItemName"typeof(string)));  
  23.     dt.Columns.Add(new DataColumn("UnitPrice"typeof(string)));  
  24.     dt.Columns.Add(new DataColumn("Quantity"typeof(string)));  
  25.     dt.Columns.Add(new DataColumn("LineTotal"typeof(string)));  
  26.     dr = dt.NewRow();  
  27.     dr["RowNumber"] = 1;  
  28.     dr["ItemName"] = string.Empty;  
  29.     dr["UnitPrice"] = string.Empty;  
  30.     dr["Quantity"] = string.Empty;  
  31.     dr["LineTotal"] = string.Empty;  
  32.     dt.Rows.Add(dr);  
  33.     ViewState["CurrentTable"] = dt;  
  34.     grdItemDetails.DataSource = dt;  
  35.     grdItemDetails.DataBind();  
  36. }  
  37. private void AddNewRowToGrid()  
  38. {  
  39.     int x = 0;  
  40.     foreach (RepeaterItem rptrItems in rptrTables.Items)  
  41.     {  
  42.         GridView gvItems = (GridView)rptrItems.FindControl("grdOrder");  
  43.         int rowIndex = 0;  
  44.         if (ViewState["CurrentTable"] != null)  
  45.         {  
  46.             DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];  
  47.             DataTable dt = dtCurrentTable.Copy();  
  48.             DataRow drCurrentRow = dt.NewRow();  
  49.             if (dt.Rows.Count > 0)  
  50.             {  
  51.                 for (int i = 1; i <= dt.Rows.Count; i++)  
  52.                 {  
  53.                     DropDownList box1 = (DropDownList)gvItems.Rows[rowIndex].Cells[0].FindControl("drpItemname");  
  54.                     TextBox box2 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl("lblUnitPrice");  
  55.                     TextBox box3 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl("lblQuantity");  
  56.                     TextBox box4 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl("lblLineTotal");  
  57.                     drCurrentRow = dt.NewRow();  
  58.                     drCurrentRow["RowNumber"] = i + 1;  
  59.                     dt.Rows[i - 1]["ItemName"] = box1.Text;  
  60.                     dt.Rows[i - 1]["UnitPrice"] = box2.Text;  
  61.                     dt.Rows[i - 1]["Quantity"] = box3.Text;  
  62.                     dt.Rows[i - 1]["LineTotal"] = box4.Text;  
  63.                     rowIndex++;  
  64.                 }  
  65.                 dt.Rows.Add(drCurrentRow);  
  66.                 if (x == rptrTables.Items.Count - 1)  
  67.                 {  
  68.                     ViewState["CurrentTable"] = dt;  
  69.                 }  
  70.                 gvItems.DataSource = dt;  
  71.                 gvItems.DataBind();  
  72.             }  
  73.             //SetPreviousData(gvItem);    
  74.         }  
  75.         else  
  76.         {  
  77.             Response.Write("ViewState is null");  
  78.         }  
  79.         x++;  
  80.     }  
  81. }  
  82. protected void AddItem(object sender, EventArgs e)  
  83. {  
  84.     AddNewRowToGrid();  
  85. }  
  86. private void SetPreviousData()  
  87. {  
  88.     GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");  
  89.     int rowIndex = 0;  
  90.     if (ViewState["CurrentTable"] != null)  
  91.     {  
  92.         DataTable dt = (DataTable)ViewState["CurrentTable"];  
  93.         if (dt.Rows.Count > 0)  
  94.         {  
  95.             for (int i = 0; i < dt.Rows.Count; i++)  
  96.             {  
  97.                 DropDownList box1 = (DropDownList)gvItems.Rows[rowIndex].Cells[0].FindControl("drpItemname");  
  98.                 TextBox box2 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl("lblUnitPrice");  
  99.                 TextBox box3 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl("lblQuantity");  
  100.                 TextBox box4 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl("lblLineTotal");  
  101.                 box1.Text = dt.Rows[i]["ItemName"].ToString();  
  102.                 box2.Text = dt.Rows[i]["UnitPrice"].ToString();  
  103.                 box3.Text = dt.Rows[i]["Quantity"].ToString();  
  104.                 box4.Text = dt.Rows[i]["LineTotal"].ToString();  
  105.                 rowIndex++;  
  106.                 //if (!string.IsNullOrEmpty(box5.Text) && !string.IsNullOrEmpty(box5.Text))    
  107.                 //{    
  108.                 //    box5.Text = dt.Rows[i]["Total"].ToString();    
  109.                 //}    
  110.                 //else    
  111.                 //{    
  112.                 //    box5.Text = "0";    
  113.                 //}    
  114.             }  
  115.         }  
  116.     }  
  117. }  
  118. protected void GetItemDetails(object sender, EventArgs e)  
  119. {  
  120.     foreach (RepeaterItem rptrItems in rptrTables.Items)  
  121.     {  
  122.         GridView gvItems = (GridView)rptrItems.FindControl("grdOrder");  
  123.         //GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");    
  124.         foreach (GridViewRow row in gvItems.Rows)  
  125.         {  
  126.             DropDownList ddl = sender as DropDownList;  
  127.             Control ctrl = row.FindControl("drpItemname"as DropDownList;  
  128.             if (ctrl != null)  
  129.             {  
  130.                 DropDownList ddl1 = (DropDownList)ctrl;  
  131.                 if (ddl.ClientID == ddl1.ClientID)  
  132.                 {  
  133.                     TextBox UnitPrice = row.FindControl("lblUnitPrice"as TextBox;  
  134.                     TextBox QTTY = row.FindControl("lblQuantity"as TextBox;  
  135.                     TextBox UPrice = row.FindControl("lblUnitPrice"as TextBox;  
  136.                     TextBox LINETOTAL = row.FindControl("lblLineTotal"as TextBox;  
  137.                     SqlConnection conn = new SqlConnection(constring);  
  138.                     conn.Open();  
  139.                     if (ctrl != null)  
  140.                     {  
  141.                         if ((ddl1.ID == ddl.ID) && (ddl1.SelectedIndex == ddl.SelectedIndex))  
  142.                         {  
  143.                             string str = "select * from ItemMaster where ItemName='" + ddl1.SelectedItem.ToString() + "'";  
  144.                             SqlCommand com = new SqlCommand(str, conn);  
  145.                             SqlDataReader reader = com.ExecuteReader();  
  146.                             while (reader.Read())  
  147.                             {  
  148.                                 UnitPrice.Text = reader["UnitPrice"].ToString();  
  149.                                 //decimal totamt = Convert.ToDecimal(QTTY.Text) * Convert.ToDecimal(UnitPrice.Text);    
  150.                             }  
  151.                             reader.Close();  
  152.                             conn.Close();  
  153.                         }  
  154.                         else  
  155.                         {  
  156.                             ScriptManager.RegisterStartupScript(thisthis.GetType(), "swal""swal('Item already selected, you can increase the quantity instead!', 'Happy Cakes, Vijayapura', 'warning');"true);  
  157.                         }  
  158.                     }  
  159.   
  160.                 }  
  161.             }  
  162.         }  
  163.     }  
  164. }  
  165. protected void Gridview1_RowDeleting1(object sender, GridViewDeleteEventArgs e)  
  166. {  
  167.     DataTable dt = (DataTable)ViewState["CurrentTable"];  
  168.     GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");  
  169.     //txtAmtPayable.Text = (Convert.ToDecimal(txtAmtPayable.Text) - Convert.ToDecimal(((TextBox)gvItems.Rows[e.RowIndex].FindControl("txtTotalPrice")).Text)).ToString("#0.00");    
  170.     dt.Rows.RemoveAt(e.RowIndex);  
  171.     gvItems.DataSource = dt;  
  172.     gvItems.DataBind();  
  173.     SetPreviousData();  
  174.     if (gvItems.Rows.Count <= 0)  
  175.     {  
  176.         ScriptManager.RegisterStartupScript(thisthis.GetType(), "swal""swal('This is the only item in the list!', 'Royal Riders, Vijayapura', 'success');"true);  
  177.         SetInitialRow();  
  178.         /*txtAmtPaid.Text = "0";  
  179.         txtAmtPayable.Text = "0";  
  180.         txtBalAmt.Text = "0";  
  181.         txtcpan.Text = "NA";  
  182.         txtgst.Text = "NA";*/  
  183.     }  
  184. }  
And below is my ASPX Markup:
  1. <div class="row">  
  2. <asp:Repeater ID="rptrTables" runat="server" OnItemDataBound="rptrTables_ItemDataBound">  
  3. <ItemTemplate>  
  4. <div class="col-md-6">  
  5. <div class="card card-info">  
  6. <div class="card-header with-border">  
  7. <h3 class="card-title"><asp:Label ID="lblTableNumber" runat="server" Text='<%# Eval("TableName") %>'></asp:Label></h3>  
  8. <div class="card-tools">  
  9. <button type="button" class="btn btn-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>  
  10. </div>  
  11. </div>  
  12. <div class="card-body">  
  13. <div class="row">  
  14. <div class="col-md-12">  
  15. <div class="form-group">  
  16. <asp:GridView ID="grdOrder" runat="server" AutoGenerateColumns="false" AllowPaging="true" CssClass="table table-bordered table-hover table-responsive" GridLines="None" PageSize="10" OnRowDataBound="gvRowDataBound" OnRowDeleting="Gridview1_RowDeleting1" ShowFooter="true">  
  17. <Columns>  
  18. <asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash" DeleteText="" HeaderText="Remove" />  
  19. <%--<asp:BoundField DataField="RowNumber" HeaderText="Sl. No." />--%>  
  20. <asp:TemplateField HeaderText="Item Name">  
  21. <ItemTemplate>  
  22. <asp:DropDownList ID="drpItemname" runat="server" CssClass="form-control select2" OnSelectedIndexChanged="GetItemDetails" AutoPostBack="true"></asp:DropDownList>  
  23. </ItemTemplate>  
  24. </asp:TemplateField>  
  25. <asp:TemplateField HeaderText="Price">  
  26. <ItemTemplate>  
  27. <asp:TextBox ID="lblUnitPrice" runat="server" CssClass="form-control" Text='<%# Eval("UnitPrice")%>'></asp:TextBox>  
  28. </ItemTemplate>  
  29. </asp:TemplateField>  
  30. <asp:TemplateField HeaderText="Qty">  
  31. <ItemTemplate>  
  32. <asp:TextBox ID="lblQuantity" runat="server" TextMode="Number" CssClass="form-control" Text="1"></asp:TextBox>  
  33. </ItemTemplate>  
  34. </asp:TemplateField>  
  35. <asp:TemplateField HeaderText="Line Total">  
  36. <ItemTemplate>  
  37. <asp:TextBox ID="lblLineTotal" runat="server" CssClass="form-control" Text="0"></asp:TextBox>  
  38. </ItemTemplate>  
  39. <FooterStyle HorizontalAlign="Right" />  
  40. <FooterTemplate>  
  41. <asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" CausesValidation="False" OnCommand="ButtonAdd_Command" />  
  42. </FooterTemplate>  
  43. </asp:TemplateField>  
  44. </Columns>  
  45. </asp:GridView>  
  46. </div>  
  47. </div>  
  48. </div>  
  49. </div>  
  50. <div class="card-footer">  
  51. <div class="pull-right">  
  52. <asp:Button ID="btnSubmit" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print KOT" />  
  53. <asp:Button ID="Button1" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print Final Bill" />  
  54. <asp:Button ID="Button2" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Complete Order" />  
  55. </div>  
  56. </div>  
  57. </div>  
  58. </div>  
  59. </ItemTemplate>  
  60. </asp:Repeater>  
  61. </div>  
Please help!

Answers (2)