TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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:
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!
this
.IsPostBack)
{
this
.getTableDetails();
}
}
protected
void
getTableDetails()
{
DataTable dt =
new
DataTable();
SqlDataAdapter sda =
new
SqlDataAdapter(
"Select TableName From TableMaster where TableStatus='0'"
, constring);
sda.Fill(dt);
rptrTables.DataSource = dt;
rptrTables.DataBind();
}
protected
void
rptrTables_ItemDataBound(
object
sender, RepeaterItemEventArgs e)
{
GridView grdItemDetails = e.Item.FindControl(
"grdOrder"
)
as
GridView;
DataTable dt =
new
DataTable();
DataRow dr =
null
;
dt.Columns.Add(
new
DataColumn(
"RowNumber"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"ItemName"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"UnitPrice"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"Quantity"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"LineTotal"
,
typeof
(
string
)));
dr = dt.NewRow();
dr[
"RowNumber"
] = 1;
dr[
"ItemName"
] =
string
.Empty;
dr[
"UnitPrice"
] =
string
.Empty;
dr[
"Quantity"
] =
string
.Empty;
dr[
"LineTotal"
] =
string
.Empty;
dt.Rows.Add(dr);
ViewState[
"CurrentTable"
] = dt;
grdItemDetails.DataSource = dt;
grdItemDetails.DataBind();
}
private
void
AddNewRowToGrid()
{
int
x = 0;
foreach
(RepeaterItem rptrItems
in
rptrTables.Items)
{
GridView gvItems = (GridView)rptrItems.FindControl(
"grdOrder"
);
int
rowIndex = 0;
if
(ViewState[
"CurrentTable"
] !=
null
)
{
DataTable dtCurrentTable = (DataTable)ViewState[
"CurrentTable"
];
DataTable dt = dtCurrentTable.Copy();
DataRow drCurrentRow = dt.NewRow();
if
(dt.Rows.Count > 0)
{
for
(
int
i = 1; i <= dt.Rows.Count; i++)
{
DropDownList box1 = (DropDownList)gvItems.Rows[rowIndex].Cells[0].FindControl(
"drpItemname"
);
TextBox box2 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl(
"lblUnitPrice"
);
TextBox box3 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl(
"lblQuantity"
);
TextBox box4 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl(
"lblLineTotal"
);
drCurrentRow = dt.NewRow();
drCurrentRow[
"RowNumber"
] = i + 1;
dt.Rows[i - 1][
"ItemName"
] = box1.Text;
dt.Rows[i - 1][
"UnitPrice"
] = box2.Text;
dt.Rows[i - 1][
"Quantity"
] = box3.Text;
dt.Rows[i - 1][
"LineTotal"
] = box4.Text;
rowIndex++;
}
dt.Rows.Add(drCurrentRow);
if
(x == rptrTables.Items.Count - 1)
{
ViewState[
"CurrentTable"
] = dt;
}
gvItems.DataSource = dt;
gvItems.DataBind();
}
//SetPreviousData(gvItem);
}
else
{
Response.Write(
"ViewState is null"
);
}
x++;
}
}
protected
void
AddItem(
object
sender, EventArgs e)
{
AddNewRowToGrid();
}
private
void
SetPreviousData()
{
GridView gvItems = (GridView)rptrTables.Items[0].FindControl(
"grdOrder"
);
int
rowIndex = 0;
if
(ViewState[
"CurrentTable"
] !=
null
)
{
DataTable dt = (DataTable)ViewState[
"CurrentTable"
];
if
(dt.Rows.Count > 0)
{
for
(
int
i = 0; i < dt.Rows.Count; i++)
{
DropDownList box1 = (DropDownList)gvItems.Rows[rowIndex].Cells[0].FindControl(
"drpItemname"
);
TextBox box2 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl(
"lblUnitPrice"
);
TextBox box3 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl(
"lblQuantity"
);
TextBox box4 = (TextBox)gvItems.Rows[rowIndex].Cells[0].FindControl(
"lblLineTotal"
);
box1.Text = dt.Rows[i][
"ItemName"
].ToString();
box2.Text = dt.Rows[i][
"UnitPrice"
].ToString();
box3.Text = dt.Rows[i][
"Quantity"
].ToString();
box4.Text = dt.Rows[i][
"LineTotal"
].ToString();
rowIndex++;
//if (!string.IsNullOrEmpty(box5.Text) && !string.IsNullOrEmpty(box5.Text))
//{
// box5.Text = dt.Rows[i]["Total"].ToString();
//}
//else
//{
// box5.Text = "0";
//}
}
}
}
}
protected
void
GetItemDetails(
object
sender, EventArgs e)
{
foreach
(RepeaterItem rptrItems
in
rptrTables.Items)
{
GridView gvItems = (GridView)rptrItems.FindControl(
"grdOrder"
);
//GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");
foreach
(GridViewRow row
in
gvItems.Rows)
{
DropDownList ddl = sender
as
DropDownList;
Control ctrl = row.FindControl(
"drpItemname"
)
as
DropDownList;
if
(ctrl !=
null
)
{
DropDownList ddl1 = (DropDownList)ctrl;
if
(ddl.ClientID == ddl1.ClientID)
{
TextBox UnitPrice = row.FindControl(
"lblUnitPrice"
)
as
TextBox;
TextBox QTTY = row.FindControl(
"lblQuantity"
)
as
TextBox;
TextBox UPrice = row.FindControl(
"lblUnitPrice"
)
as
TextBox;
TextBox LINETOTAL = row.FindControl(
"lblLineTotal"
)
as
TextBox;
SqlConnection conn =
new
SqlConnection(constring);
conn.Open();
if
(ctrl !=
null
)
{
if
((ddl1.ID == ddl.ID) && (ddl1.SelectedIndex == ddl.SelectedIndex))
{
string
str =
"select * from ItemMaster where ItemName='"
+ ddl1.SelectedItem.ToString() +
"'"
;
SqlCommand com =
new
SqlCommand(str, conn);
SqlDataReader reader = com.ExecuteReader();
while
(reader.Read())
{
UnitPrice.Text = reader[
"UnitPrice"
].ToString();
//decimal totamt = Convert.ToDecimal(QTTY.Text) * Convert.ToDecimal(UnitPrice.Text);
}
reader.Close();
conn.Close();
}
else
{
ScriptManager.RegisterStartupScript(
this
,
this
.GetType(),
"swal"
,
"swal('Item already selected, you can increase the quantity instead!', 'Happy Cakes, Vijayapura', 'warning');"
,
true
);
}
}
}
}
}
}
}
protected
void
Gridview1_RowDeleting1(
object
sender, GridViewDeleteEventArgs e)
{
DataTable dt = (DataTable)ViewState[
"CurrentTable"
];
GridView gvItems = (GridView)rptrTables.Items[0].FindControl(
"grdOrder"
);
//txtAmtPayable.Text = (Convert.ToDecimal(txtAmtPayable.Text) - Convert.ToDecimal(((TextBox)gvItems.Rows[e.RowIndex].FindControl("txtTotalPrice")).Text)).ToString("#0.00");
dt.Rows.RemoveAt(e.RowIndex);
gvItems.DataSource = dt;
gvItems.DataBind();
SetPreviousData();
if
(gvItems.Rows.Count <= 0)
{
ScriptManager.RegisterStartupScript(
this
,
this
.GetType(),
"swal"
,
"swal('This is the only item in the list!', 'Royal Riders, Vijayapura', 'success');"
,
true
);
SetInitialRow();
/*txtAmtPaid.Text = "0";
txtAmtPayable.Text = "0";
txtBalAmt.Text = "0";
txtcpan.Text = "NA";
txtgst.Text = "NA";*/
}
}
And below is my ASPX Markup:
<
div
class
=
"row"
>
<
asp:Repeater
ID
=
"rptrTables"
runat
=
"server"
OnItemDataBound
=
"rptrTables_ItemDataBound"
>
<
ItemTemplate
>
<
div
class
=
"col-md-6"
>
<
div
class
=
"card card-info"
>
<
div
class
=
"card-header with-border"
>
<
h3
class
=
"card-title"
>
<
asp:Label
ID
=
"lblTableNumber"
runat
=
"server"
Text
=
'<%# Eval("TableName") %>'
>
</
asp:Label
>
</
h3
>
<
div
class
=
"card-tools"
>
<
button
type
=
"button"
class
=
"btn btn-tool"
data-widget
=
"collapse"
>
<
i
class
=
"fa fa-minus"
>
</
i
>
</
button
>
</
div
>
</
div
>
<
div
class
=
"card-body"
>
<
div
class
=
"row"
>
<
div
class
=
"col-md-12"
>
<
div
class
=
"form-group"
>
<
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"
>
<
Columns
>
<
asp:CommandField
ShowDeleteButton
=
"true"
ControlStyle-CssClass
=
"btn btn-danger fa fa-trash"
DeleteText
=
""
HeaderText
=
"Remove"
/>
<
%--
<
asp:BoundField
DataField
=
"RowNumber"
HeaderText
=
"Sl. No."
/>
--%
>
<
asp:TemplateField
HeaderText
=
"Item Name"
>
<
ItemTemplate
>
<
asp:DropDownList
ID
=
"drpItemname"
runat
=
"server"
CssClass
=
"form-control select2"
OnSelectedIndexChanged
=
"GetItemDetails"
AutoPostBack
=
"true"
>
</
asp:DropDownList
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Price"
>
<
ItemTemplate
>
<
asp:TextBox
ID
=
"lblUnitPrice"
runat
=
"server"
CssClass
=
"form-control"
Text
=
'<%# Eval("UnitPrice")%>'
>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Qty"
>
<
ItemTemplate
>
<
asp:TextBox
ID
=
"lblQuantity"
runat
=
"server"
TextMode
=
"Number"
CssClass
=
"form-control"
Text
=
"1"
>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Line Total"
>
<
ItemTemplate
>
<
asp:TextBox
ID
=
"lblLineTotal"
runat
=
"server"
CssClass
=
"form-control"
Text
=
"0"
>
</
asp:TextBox
>
</
ItemTemplate
>
<
FooterStyle
HorizontalAlign
=
"Right"
/>
<
FooterTemplate
>
<
asp:Button
ID
=
"ButtonAdd"
runat
=
"server"
CssClass
=
"btn btn-primary"
Text
=
"Add"
OnClick
=
"AddItem"
CausesValidation
=
"False"
OnCommand
=
"ButtonAdd_Command"
/>
</
FooterTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
</
div
>
</
div
>
</
div
>
</
div
>
<
div
class
=
"card-footer"
>
<
div
class
=
"pull-right"
>
<
asp:Button
ID
=
"btnSubmit"
runat
=
"server"
CausesValidation
=
"false"
TabIndex
=
"2"
CssClass
=
"btn btn-primary"
Text
=
"Print KOT"
/>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
CausesValidation
=
"false"
TabIndex
=
"2"
CssClass
=
"btn btn-primary"
Text
=
"Print Final Bill"
/>
<
asp:Button
ID
=
"Button2"
runat
=
"server"
CausesValidation
=
"false"
TabIndex
=
"2"
CssClass
=
"btn btn-primary"
Text
=
"Complete Order"
/>
</
div
>
</
div
>
</
div
>
</
div
>
</
ItemTemplate
>
</
asp:Repeater
>
</
div
>
Please help!
Attachment:
HotelBillingSoftware.rar
Reply
Answers (
2
)
Why do I see ?'s or black boxes in my PDF? Asp.net c# RDLC
transfer mvc controller data Json format in Web Api method