Textbox with textmode time, won't display data from sql database - c#

So i did a research about textmode time but it doesn't seem to help me. My problem is when i tried to bind the data with time textmode it won't display anything I already tried removing the textmode so i can see if it can display data and it does. Here's my code in the front, hope you'll help me. Thank you in advance
<asp:Repeater runat="server" ID="rptrTRFFormItems" OnItemDataBound="rptrTRFFormItems_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="No" Text='<%# Container.ItemIndex + 1 %>' Visible="false">
</asp:Label>
</td>
<td>
<asp:LinkButton runat="server" ID="btnAddItemRow" CssClass="actionbtnstyle fa fa-plus-square" CausesValidation="false" OnClick="btnAddItemRows" Style="font-size: 20px;">
</asp:LinkButton>
</td>
<td>
<asp:LinkButton runat="server" ID="btnDeleteItemRow" CssClass="actionbtnstyle fa fa-minus-square" CausesValidation="false" OnClick="btnDeleteItemRows" Style="font-size: 20px;">
</asp:LinkButton>
</td>
<td>
<div runat="server" id="divInputGroup">
<span runat="server" id="spanInputGroup">
<asp:Label runat="server" ID="validatetbName">
</asp:Label>
</span>
<asp:TextBox runat="server" ID="tbName" Text='<%#Eval("Name") %>' CssClass="form-control rptrtbstyle" ondrop="return false" title="Required" data-toggle="tooltip" TabIndex="13" data-placement="bottom">
</asp:TextBox>
</div>
</td>
<td>
<asp:UpdatePanel runat="server" ID="rptriddate" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tbDate" EventName="TextChanged" />
</Triggers>
<ContentTemplate>
<div runat="server" id="divInputGroup0">
<span runat="server" id="spanInputGroup0">
<asp:Label runat="server" ID="validatetbDate">
</asp:Label>
</span>
<asp:TextBox runat="server" ID="tbDate" CssClass="form-control js-datepicker rptrtbstyle" Style="cursor: pointer;" TabIndex="14" data-toggle="tooltip" data-placement="bottom" title="Required" OnTextChanged="tbDate_TextChanged" autocomplete="off" AutoPostBack="true">
</asp:TextBox>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td>
<div runat="server" id="divInputGroup2">
<span runat="server" id="spanInputGroup2">
<asp:Label runat="server" ID="validatetbPreferredTime">
</asp:Label>
</span>
<asp:TextBox runat="server" ID="tbPreferredTime" Text='<%#Eval("PreferredTime") %>' format="HH:mm" TextMode="Time" CssClass="form-control rptrtbstyle" data-toggle="tooltip" TabIndex="15" data-placement="bottom" title="Required">
</asp:TextBox>
</div>
</td>
<td>
<div runat="server" id="divInputGroup1">
<span runat="server" id="spanInputGroup1">
<asp:Label runat="server" ID="validatetbFromRptr">
</asp:Label>
</span>
<asp:TextBox runat="server" ID="tbFromRptr" Text='<%#Eval("FromRptr") %>' CssClass="form-control rptrtbstyle" data-toggle="tooltip" data-placement="bottom" TabIndex="16" title="Required" AutoPostBack="false">
</asp:TextBox>
</div>
</td>
<td>
<asp:LinkButton runat="server" ID="btnExchange" CssClass="actionbtnstyle fa fa-exchange" CausesValidation="false" Style="font-size: 20px;">
</asp:LinkButton>
</td>
<td>
<div runat="server" id="divInputGroup3">
<span runat="server" id="spanInputGroup3">
<asp:Label runat="server" ID="validatetbToRptr">
</asp:Label>
</span>
<asp:TextBox runat="server" ID="tbToRptr" Text='<%#Eval("ToRptr") %>' CssClass="form-control rptrtbstyle" TabIndex="17" data-toggle="tooltip" data-placement="bottom" title="Required">
</asp:TextBox>
</div>
</td>
</tr>
<%--</ContentTemplate> </asp:UpdatePanel>--%>
</ItemTemplate>
</asp:Repeater>
And here's the code to bind data from behind.
using(SqlConnection con = new SqlConnection(travelrequest))
{
using(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "TRFSearchByRefernce";
cmd.Parameters.AddWithValue("#trfNo", txtSearchByReference.Text);
cmd.Parameters.AddWithValue("#fullname", lblFullname01.Text);
using(SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
adp.Fill(dt);
if(dt.Rows.Count > 0)
{
rptrTRFFormItems.DataSource = dt;
rptrTRFFormItems.DataBind();
ViewState["CurrentTable"] = dt;
}
else
{
SetInitialRow();
}
}
}
}
and here's my stored procedure sql query
SELECT
[TRFGName] as Name
,[TRFGDate] as [Date]
,[TRFGTimeRef] as PreferredTime
,[TRFGFrom] as FromRptr
,[TRFGTo] as ToRptr
,[TRFGNo]
,[TRFGControlNo]
,[TRFRequester]
FROM [TRAVELREQUESTFORM].[TRAVEL].[TRAVELGRID] WHERE TRFGControlNo = #trfNo and TRFRequester = #fullname

Related

How to calculate all rows in a GridView in a single button click

I have a GridView in which I'm letting users edit previously generated estimates. Everything works fine till I hit recalculate button but after that, all the row values go haywire.
Below is a screenshot of the layout for your reference.
In that, All the details are being fetched from database and few of them are editable and after changing the values, below there is a refresh button which I'm using to recalculate. but my loop is causing them to calculate incorrectly. Please help me rectify my mistake.
below is my aspx code:
<div class="card-body">
<div class="row">
<div class="col-md-12">
<asp:GridView ID="grdPurchaseBill" runat="server" CssClass="table table-bordered table-responsive table-sm" AutoGenerateColumns="False" ShowFooter="false">
<Columns>
<asp:TemplateField HeaderText="Product" ItemStyle-Wrap="false" ControlStyle-Width="100%" ItemStyle-Width="100%">
<ItemTemplate>
<asp:TextBox ID="txtProduct" runat="server" CssClass="form-control form-control-sm" Width="100%" Style="width: 100%;" AutoCompleteType="Disabled" autocomplete="off" Text='<%# Eval("product") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight" ItemStyle-Wrap="false" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:TextBox ID="txtWeight" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("weight") %>' Width="100%" Style="width: 100%;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="# of Bags" ItemStyle-Wrap="false" ControlStyle-Width="100%" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:TextBox ID="txtBags" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("bags") %>' Width="100%" Style="width: 100%;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shortage %" ItemStyle-Wrap="false" ControlStyle-Width="100%" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:TextBox ID="txtShortagePercent" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("shortage_percent") %>' Width="100%" Style="width: 100%;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shortage" ItemStyle-Wrap="false" ControlStyle-Width="100%" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:TextBox ID="txtShortage" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("shortage") %>' Width="100%" Style="width: 100%;" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Net Weight" ItemStyle-Wrap="false" ControlStyle-Width="100%" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:TextBox ID="txtNetWeight" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("net_weight") %>' Width="100%" Style="width: 100%;" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price" ItemStyle-Wrap="false" HeaderStyle-Wrap="false" ControlStyle-Width="100%">
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("rate") %>' Width="100%" Style="width: 100%;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub-Total" ItemStyle-Wrap="false" HeaderStyle-Wrap="false" ControlStyle-Width="100%">
<ItemTemplate>
<asp:TextBox ID="txtSubTotal" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("sub_total") %>' Width="100%" Style="width: 100%;" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hamali %" ItemStyle-Wrap="false" HeaderStyle-Wrap="false" ControlStyle-Width="100%">
<ItemTemplate>
<asp:TextBox ID="txtHamali" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("hamali_percent") %>' Width="100%" Style="width: 100%;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hamali Amt" ItemStyle-Wrap="false" HeaderStyle-Wrap="false" ControlStyle-Width="100%">
<ItemTemplate>
<asp:TextBox ID="txtHamaliAmount" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("hamali") %>' Width="100%" Style="width: 100%;" Enabled="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Driver Comm. " ItemStyle-Wrap="false" HeaderStyle-Wrap="false" ControlStyle-Width="100%">
<ItemTemplate>
<asp:TextBox ID="txtDriverCommission" runat="server" CssClass="form-control form-control-sm" Text='<%# Eval("driver_commission") %>' Width="100%" Style="width: 100%;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Net Amt" ItemStyle-Wrap="false" ControlStyle-Width="100%" HeaderStyle-Wrap="false">
<ItemTemplate>
<asp:TextBox ID="txtNetAmount" runat="server" CssClass="form-control form-control-sm" Enabled="false" Text='<%# Eval("net_amount") %>' Width="100%" Style="width: 100%;"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Bill Date</label>
<div class="input-group margin input-group-sm">
<asp:TextBox ID="txtBDate" runat="server" CssClass="form-control form-control-sm" TabIndex="1" AutoCompleteType="Disabled" autocomplete="off"></asp:TextBox>
<ajax:CalendarExtender ID="ce1" runat="server" TargetControlID="txtBDate" Format="dd-MM-yyyy" />
<span class="input-group-btn">
<span class="fa fa-times"></span>
</span>
</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<div class="input-group input-group-sm">
<asp:TextBox ID="txtcname" runat="server" CssClass="form-control form-control-sm" TabIndex="2" CausesValidation="True" AutoCompleteType="Disabled" autocomplete="off"></asp:TextBox>
<asp:HiddenField ID="hfCustomerId" runat="server" />
</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Phone</label>
<asp:TextBox ID="txtcmobile" runat="server" CssClass="form-control form-control-sm" TabIndex="3" AutoCompleteType="Disabled" autocomplete="off"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Address</label>
<asp:TextBox ID="txtcaddress" runat="server" TextMode="MultiLine" TabIndex="6" CssClass="form-control form-control-sm"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Lorry Freight</label>
<asp:TextBox ID="txtLorryFreight" runat="server" TabIndex="6" CssClass="form-control form-control-sm"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Remarks</label>
<asp:TextBox ID="txtRemarks" runat="server" TabIndex="6" CssClass="form-control form-control-sm" TextMode="MultiLine"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Total Amount</label>
<asp:Label ID="lblTotal" runat="server" CssClass="form-control form-control-sm lblTotal" Text="0" ForeColor="Blue" BackColor="#DEE2E6"></asp:Label>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Payable Amount</label>
<asp:Label ID="lblPayableAmt" runat="server" CssClass="form-control form-control-sm lblPayableAmt" Text="0" ForeColor="Blue" BackColor="#DEE2E6"></asp:Label>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Paid Amount</label>
<asp:TextBox ID="txtPaidAmt" runat="server" CssClass="form-control form-control-sm txtPaidAmt" OnTextChanged="CalculatePaidAmount" AutoPostBack="true" TabIndex="8" ForeColor="Blue" Text="0"></asp:TextBox>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Balance Amount</label>
<div class="input-group input-group-sm">
<label id="lblBalAmt" runat="server" class="form-control form-control-sm lblBalAmt" style="color: red; background-color: #DEE2E6;">0</label>
<span class="input-group-append">
<asp:LinkButton ID="lnkPayable" runat="server" OnClick="ReCalculate" CssClass="btn btn-outline-success btn-flat" Text="" TabIndex="9" CausesValidation="false"><i class="fa fa-sync"></i></asp:LinkButton>
</span>
</div>
</div>
</div>
</div>
</div>
And my aspx.cs code:
protected void GetBillDetails()
{
string query = "select * from estimates where bno='" + Request.QueryString["bno"].ToString() + "';";
grdPurchaseBill.DataSource = dal.GetData(query);
grdPurchaseBill.DataBind();
using (SqlConnection con = new SqlConnection(cm.constring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
txtBDate.Text = dr["bill_date"].ToString();
txtcname.Text = dr["name"].ToString();
txtcmobile.Text = dr["mobileno"].ToString();
txtcaddress.Text = dr["address"].ToString();
txtLorryFreight.Text = dr["lorry_freight"].ToString();
txtRemarks.Text = dr["remarks"].ToString();
lblTotal.Text = dr["grand_total"].ToString();
lblPayableAmt.Text = dr["payable_amount"].ToString();
txtPaidAmt.Text = dr["paid_amount"].ToString();
lblBalAmt.InnerText = dr["balance_amount"].ToString();
bno.InnerText = Request.QueryString["bno"].ToString();
}
}
}
con.Close();
}
}
protected void ReCalculate(object sender, EventArgs e)
{
decimal PayableAmount = 0, PaidAmount = !string.IsNullOrEmpty(txtPaidAmt.Text) ? Convert.ToDecimal(txtPaidAmt.Text) : 0, NetWeight = 0, SubTotal = 0, NetAmount = 0;
foreach (GridViewRow Row in grdPurchaseBill.Rows)
{
TextBox txtProduct = Row.FindControl("txtProduct") as TextBox;
TextBox txtWeight = Row.FindControl("txtWeight") as TextBox;
TextBox txtBags = Row.FindControl("txtBags") as TextBox;
TextBox txtShortagePercent = Row.FindControl("txtShortagePercent") as TextBox;
TextBox txtShortage = Row.FindControl("txtShortage") as TextBox;
TextBox txtNetWeight = Row.FindControl("txtNetWeight") as TextBox;
TextBox txtRate = Row.FindControl("txtRate") as TextBox;
TextBox txtSubTotal = Row.FindControl("txtSubTotal") as TextBox;
TextBox txtHamali = Row.FindControl("txtHamali") as TextBox;
TextBox txtHamaliAmount = Row.FindControl("txtHamaliAmount") as TextBox;
TextBox txtDriverCommission = Row.FindControl("txtDriverCommission") as TextBox;
TextBox txtNetAmount = Row.FindControl("txtNetAmount") as TextBox;
Control txtShortagePercent_ctrl = Row.FindControl("txtShortagePercent") as TextBox;
if (txtShortagePercent_ctrl != null)
{
txtShortage.Text = (Convert.ToDecimal(txtWeight.Text) - (Convert.ToDecimal(txtWeight.Text) - (Convert.ToDecimal(txtWeight.Text)) * (Convert.ToDecimal(txtShortage.Text) / 100))).ToString("#0.00");
//txtHamaliAmount.Text = (calculateHamali(Convert.ToDecimal(txtWeight.Text), Convert.ToDecimal(txtHamali.Text))).ToString("#0.00");
decimal TotalHamali = Convert.ToDecimal(txtHamali.Text) > 0 ? ((Convert.ToDecimal(txtWeight.Text) / 100) * Convert.ToDecimal(txtHamali.Text)) : 0;
NetAmount += ((Convert.ToDecimal(txtSubTotal.Text) - TotalHamali));// + Convert.ToDecimal(txtDriverCommission.Text)
txtNetAmount.Text = NetAmount.ToString("#0.00");
txtHamaliAmount.Text = TotalHamali.ToString("#0.00");
NetWeight += (Convert.ToDecimal(txtWeight.Text) - (Convert.ToDecimal(txtWeight.Text) * Convert.ToDecimal(txtShortage.Text) / 100));
txtNetWeight.Text = NetWeight.ToString("#0.00");
txtShortage.Text = (Convert.ToDecimal(txtWeight.Text) - (Convert.ToDecimal(txtWeight.Text) - (Convert.ToDecimal(txtWeight.Text) * Convert.ToDecimal(txtShortage.Text) / 100))).ToString();
SubTotal += (Convert.ToDecimal(txtNetWeight.Text) * Convert.ToDecimal(txtRate.Text));
txtSubTotal.Text = SubTotal.ToString("#0.00");
TextBox totamt = Row.FindControl("txtNetAmount") as TextBox;
PayableAmount += Convert.ToDecimal(!string.IsNullOrEmpty(totamt.Text) ? totamt.Text : "0");
}
}
lblTotal.Text = (PayableAmount).ToString("#0.00");
lblPayableAmt.Text = (PayableAmount - Convert.ToDecimal(!string.IsNullOrEmpty(txtLorryFreight.Text) ? txtLorryFreight.Text : "0")).ToString("#0.00");
lblBalAmt.InnerText = (PaidAmount > 0 ? Convert.ToDecimal(lblPayableAmt.Text) - PaidAmount : Convert.ToDecimal(lblPayableAmt.Text) - 0).ToString("#0.00");
}

Adding Header to the repeater in asp.net

How to add header to the Repeater control? I want to add the header for each columns mentioned
Below is my repeater control
<asp:Repeater ID="rptSingleSapDataInfo" runat="server">
<ItemTemplate>
<div class="widget-box widget-blue">
<div class="widget-main widget-repeater-horizontal">
<div class="pull-left">
<asp:Label ID="lblSapId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SAP_ID") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right">
<asp:Label ID="lblState" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "STATE") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right">
<asp:Label ID="lblNLat" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "NEW_LATITUDE") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right">
<asp:Label ID="lblNLong" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "NEW_LONGITUDE") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right">
<asp:Label ID="lblOLat" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OLD_LATITUDE") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right">
<asp:Label ID="lblOLong" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OLD_LONGITUDE") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right1" style="padding: 5px 5px 5px 20px;">
<asp:Label ID="lblRemarks" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "REMARKS") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right">
<asp:Label ID="lblCreatedBy" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CREATED_BY") %>' CssClass="labelbold"></asp:Label>
</div>
<div class="pull-right">
<asp:Label ID="lblCreatedDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CREATED_DATE", "{0:dd-MM-yyyy}") %>' CssClass="labelbold"></asp:Label>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
You are already using the ItemTemplate so use the HeaderTemplate to add a header:
<asp:Repeater ID="rptSingleSapDataInfo" runat="server">
<HeaderTemplate>
My header
</HeaderTemplate>
<ItemTemplate>
<div class="widget-box widget-blue">
...
</div>
</ItemTemplate>
</asp:Repeater>
As i understand your question use Header Template
<asp:Repeater runat="server" ID="rowRepeater" OnItemDataBound="rowRepeater_ItemBound">
<HeaderTemplate>
<table>
<tr>
<asp:Repeater runat="server" ID="headerRepeater">
<ItemTemplate>
<th>
<%# Container.DataItem %>
</th>
</ItemTemplate>
</asp:Repeater>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="lblUserName" Text='<%# Eval("Key") %>' /></td>
<asp:Repeater runat="server" ID="columnRepeater">
<ItemTemplate>
<td>
<asp:HiddenField runat="server" ID="hfRoleIndex" Value='<%# Container.ItemIndex %>' />
<asp:CheckBox runat="server" ID="cbColumnValue" Checked='<%# Container.DataItem %>' OnCheckedChanged="cbColumnValue_CheckedChanged" AutoPostBack="true" />
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Here is the reference link
Repeater and add columns to header and itemtemplate on page load
it will help you

I'm at lost with my error regarding c# code (asp.net) web application

LinkButton btnCerFormsMon_Print = (LinkButton)sender;
Repeater Item = (Repeater)btnCerFormsMon_Print.NamingContainer;
Label referenceNo = (Label)Item.FindControl("rptrControlNo");
Label name = (Label)Item.FindControl("rptrRequester");
Label employeeNo = (Label)Item.FindControl("rptrEmployeeNo");
Label entryDate = (Label)Item.FindControl("rptrEntryDate");
Label document = (Label)Item.FindControl("rptrDocu");
Label Approver = (Label)Item.FindControl("rptrApprover");
Label requestStatus = (Label)Item.FindControl("rptrReqStatus");
Label rejectRemarks = (Label)Item.FindControl("rptrRejectRemarks");
hide_rptrControlNo.Text = referenceNo.Text;
hide_rptrStatus.Text = requestStatus.Text;
The code above produces the following error message.
Additional information: Unable to cast object of type 'System.Web.UI.WebControls.RepeaterItem' to type 'System.Web.UI.WebControls.Repeater'.
Markup:
<asp:Repeater runat="server" ID="rptrCERFormsMonItems">
<ItemTemplate>
<tr>
<td class="col-xs-1 col-sm-1">
<asp:UpdatePanel runat="server" ID="UpdatePanelRepeater" UpdateMode="Conditional">
<Triggers></Triggers>
<ContentTemplate>
<asp:LinkButton runat="server" class="btn btn-sm btn-default" ID="btnCERFormsMon_Print" OnClick="btnCERFormsMon_Print_Click" CausesValidation="false" Text="<i class='fa fa-print' aria-hidden='true'></i> PRINT"></asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</td>
<td class="col-xs-1 col-sm-1">
<asp:Label runat="server" ID="rptrControlNo" Text='<%#Eval("DI_REFERENCENO")%>' Style="text-transform: uppercase" />
</td>
<td class="col-xs-2 col-sm-2">
<asp:Label runat="server" ID="rptrRequester" Text='<%#Eval("DI_REQUESTEDBY")%>' Style="text-transform: uppercase" />
</td>
<td class="col-xs-2 col-sm-2">
<asp:Label runat="server" ID="rptrEmployeeNo" Text='<%#Eval("DI_EMPLOYEENO")%>' Style="text-transform: uppercase" />
</td>
<td class="col-xs-1 col-sm-1">
<asp:Label runat="server" ID="rptrEntryDate" Text='<%#Eval("DI_ENTRYDATE")%>' Style="text-transform: uppercase; float:right;" />
</td>
<td class="col-xs-1 col-sm-1">
<asp:Label runat="server" ID="rptrDocu" Text='<%#Eval("DI_DOCUMENTTOREQUEST")%>' Style="text-transform: uppercase" />
</td>
<td class="col-xs-2 col-sm-2">
<asp:Label runat="server" ID="rptrApprover" Text='<%#Eval("DI_APPROVER")%>' Style="text-transform: uppercase" />
</td>
<td class="col-xs-1 col-sm-1">
<asp:Label runat="server" ID="rptrReqStatus" Text='<%#Eval("DI_REQUESTSTATUS")%>' Style="text-transform: uppercase" />
</td>
<asp:Label runat="server" ID="rptrRejectRemarks" Text='<%#Eval("DI_REJECTREMARKS")%>' Visible="false" />
<asp:Label runat ="server" ID="rptrTime" Text='<%#Eval("DI_ENTRYTIME")%>' Visible="false" />
</tr>
</ItemTemplate>
</asp:Repeater>
Solved it. Unfortunately repeater is a method used for displaying and it cant find any item in the datatable when I bind it on my repeater so I have to use the repeaterItem method to cast repeater's obj.
RepeaterItem Item = (RepeaterItem)btnCerFormsMon_Print.NamingContainer;

The session value stored into two columns instead of one column

I am facing a problem with below code. When I click on Repsendbtn it stores the same session value of UsrNme into both columns Receiver and Sender, where it is supposed to save session value into Receiver and into Sender the value of tolbl
<asp:FormView ID="repmsgform" runat="server" DataKeyNames="mailno" Width="100%" >
<ItemTemplate>
<div class="col-lg-12">
<form class="well span8">
<div class="row">
<div class="span3">
<label>From</label>
<asp:Label ID="tolbl" runat="server" CssClass="form-control" Text='<%# Bind("Receiver") %>'></asp:Label>
<label>To</label>
<asp:Label ID="frmlbl" runat="server" CssClass="form-control" Text='<%# Bind("sender") %>'></asp:Label>
<label>Ads Title</label>
<asp:Label ID="MsgTitLbl" runat="server" CssClass="form-control" Text='<%# Bind("Mestitle") %>'></asp:Label>
<asp:Label ID="adsnummsglbl" runat="server" Text='<%# Bind("AdsID") %>' Visible="false" ></asp:Label>
<asp:Label ID="mailnolbl" runat="server" Text='<%# Bind("mailno") %>' Visible="false" ></asp:Label>
</div>
<div class="span5">
<label>Message</label>
<asp:TextBox ID="TextBox15" runat="server" TextMode="MultiLine" Height="150px" CssClass="form-control"></asp:TextBox>
</textarea>
</div>
<asp:Label ID="msgsentlbl" runat="server" Font-Size="Medium" ForeColor="#669900"></asp:Label>
<asp:Button runat="server" ID="Repsendbtn" Text="Send" CssClass="btn btn-primary pull-right" OnCommand=" Repsendbtn_Command" CommandArgument='<%# Bind ("mailno") %>'/>
<asp:Button ID="Closebtn" runat="server" Text="Close" CssClass="btn btn-default pull-right" OnCommand=" Closebtn_Command" />
</div>
</form>
</div>
</ItemTemplate>
</asp:FormView>
Code:
protected void Repsendbtn_Command(object sender, CommandEventArgs e)
{
if (Session["UsrNme"] != null)
{
using (SqlConnection cn = new SqlConnection(sc))
{
string SendMsgSQL = #"INSERT INTO mails (AdsID, Mestitle, Message, Receiver, sender, Date) VALUES (#AdsID, #Mestitle, #Message, #Receiver, #sender, #Date)";
using (SqlCommand SendMsgcmd = new SqlCommand(SendMsgSQL, cn))
{
cn.Open();
var user = Session["UsrNme"];
SendMsgcmd.Parameters.AddWithValue("#Receiver", user);
SendMsgcmd.Parameters.AddWithValue("#Message", ((TextBox)repmsgform.FindControl("TextBox15")).Text);
SendMsgcmd.Parameters.AddWithValue("#Mestitle", ((Label)repmsgform.FindControl("MsgTitLbl")).Text);
SendMsgcmd.Parameters.AddWithValue("#AdsID", ((Label)repmsgform.FindControl("adsnummsglbl")).Text);
SendMsgcmd.Parameters.AddWithValue("#Date", DateTime.Now);
SendMsgcmd.Parameters.AddWithValue("#sender", ((Label)repmsgform.FindControl("tolbl")).Text);
SendMsgcmd.ExecuteNonQuery();
viewmsgView.Visible = true;
}
}
}
else
{
// Consider throwing an error (if these fields are required)
}
}

Ajax TabPanels trigger validation for other tabs in ASP.Net 4.0

I have Ajax TabPanel with Tab "Employee List" "Add Employee" "Edit Employee"
On Employee List Tab i use gridview to display list of employee along with link Button to view details of employee on same tab and "Del" button to delete employee. It worked fine but when i added Controls to Second tab "Add Employee" along with validation controls. After this gridview in Tab one is not working as it trigger validation on second Tab.
How can i stop this validation which is triggered by controls on Tab 1
Below is the code
<asp:TabContainer ID="TabContainer1" runat="server" Height="320px" Width="100%"
ActiveTabIndex="0" >
<asp:TabPanel ID="atpEmployeeList" runat="server" >
<HeaderTemplate>Employee List</HeaderTemplate>
<ContentTemplate>
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="style1" align="left" valign="top">
<b>Employee List</b>
<asp:GridView ID="gvEmployeeList" runat="server" AutoGenerateColumns="False"
DataKeyNames="eCode" BackColor="White" BorderColor="#336666"
BorderStyle="None" BorderWidth="2px" CellPadding="4"
GridLines="Horizontal" onrowcommand="gvEmployeeList_RowCommand"
onrowdatabound="gvEmployeeList_RowDataBound" >
<Columns>
<asp:BoundField DataField="eSno" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="eSno" />
<asp:BoundField DataField="eFirstName" HeaderText="First Name"
SortExpression="eFirstName" />
<asp:BoundField DataField="eLastName" HeaderText="Last Name"
SortExpression="eLastName" />
<asp:BoundField DataField="eDepartment" HeaderText="Department"
SortExpression="eDepartment" />
<asp:BoundField DataField="eSalary" HeaderText="Salary"
SortExpression="eSalary" />
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEmpDetails" CommandArgument='<%# Eval("eSno") %>' CommandName="ViewDetails" runat="server">
View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEmpDelete" CommandArgument='<%# Eval("eSno") %>' CommandName="Delete" runat="server">
Del</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Right" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
</td>
<td width="250px" align="left" valign="top">
<b>Employee Details</b>
<br />
<table width="280px" align="left" cellpadding="3" cellspacing="0" >
<tr>
<td class="style8" bgcolor="#336699">
Code</td>
<td>
<asp:Label ID="lblEmpCode" runat="server" ></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
Fisrt Name</td>
<td>
<asp:Label ID="lblFirstName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style5" bgcolor="#336699">
Last Name</td>
<td class="style6">
<asp:Label ID="lblLastName" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
Designation</td>
<td>
<asp:Label ID="lblDesignation" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
Department</td>
<td>
<asp:Label ID="lblDepartment" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
Salary</td>
<td>
<asp:Label ID="lblSalary" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
Join Date</td>
<td>
<asp:Label ID="lblJoinDate" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
Phone</td>
<td>
<asp:Label ID="lblPhone" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
Address</td>
<td>
<asp:Label ID="lblAddress" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style8" bgcolor="#336699">
City</td>
<td>
<asp:Label ID="lblCity" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="atpAddEmployee" runat="server" >
<HeaderTemplate>Add Employee</HeaderTemplate>
<ContentTemplate>
<div><b>Enter Details of New Employee</b></div>
<table border="0px"><tr>
<td width="400px">
<div class="spacer"></div>
<div class="row">
<asp:Label ID="lblAEmpCode" CssClass="txtLabel" runat="server" Text="Code :"></asp:Label>
<asp:TextBox ID="txtACode" runat="server" CssClass="txt4Digit"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCode" runat="server" ErrorMessage="*"
ControlToValidate="txtACode"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblAFN" CssClass="txtLabel" runat="server" Text="First Name :"></asp:Label>
<asp:TextBox ID="txtAFN" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="txtAFN"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblALN" CssClass="txtLabel" runat="server" Text="Last Name :"></asp:Label>
<asp:TextBox ID="txtALN" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
ControlToValidate="txtALN"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblADesignation" CssClass="txtLabel" runat="server" Text="Designation :"></asp:Label>
<asp:TextBox ID="txtADesignation" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
ControlToValidate="txtADesignation"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblADepartment" CssClass="txtLabel" runat="server" Text="Department :"></asp:Label>
<asp:TextBox ID="txtADepartment" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="*"
ControlToValidate="txtADepartment"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblASalary" CssClass="txtLabel" runat="server" Text="Salary :"></asp:Label>
<asp:TextBox ID="txtASalary" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="*"
ControlToValidate="txtASalary"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblAJoinDate" CssClass="txtLabel" runat="server" Text="Join Date :"></asp:Label>
<asp:TextBox ID="txtAJoinDate" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="*"
ControlToValidate="txtAJoinDate"></asp:RequiredFieldValidator>
</div>
</td>
<td width="400px">
<div class="spacer"></div>
<div class="row">
<asp:Label ID="lblAAddress" CssClass="txtLabel" runat="server" Text="Address :"></asp:Label>
<asp:TextBox ID="txtAAddress" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="*"
ControlToValidate="txtAAddress"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblACity" CssClass="txtLabel" runat="server" Text="City :"></asp:Label>
<asp:TextBox ID="txtACity" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="*"
ControlToValidate="txtACity"></asp:RequiredFieldValidator>
</div>
<div class="row">
<asp:Label ID="lblACountry" CssClass="txtLabel" runat="server" Text="Country :"></asp:Label>
<asp:UpdatePanel ID="updatepanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddCountry" runat="server" CssClass="ddGeneral" DataTextField="Country"
DataValueField="CountryId" OnSelectedIndexChanged="ddCountry_SelectedIndexChanged"
AutoPostBack="true" AppendDataBoundItems="true" >
<asp:ListItem Value="0">Select Country</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="row">
<asp:Label ID="lblAState" CssClass="txtLabel" runat="server" Text="State :"></asp:Label>
<asp:UpdatePanel ID="updatePnlState" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddState" runat="server" CssClass="ddGeneral" DataTextField="Region" DataValueField="RegionId" AppendDataBoundItems="true">
<asp:ListItem Value="0">Select State</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="updProgLoading" runat="server" >
<ProgressTemplate>
<img src="images/ajax-loader-small.gif" align="left" alt="Loading..." vspace="0" />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
<div class="row">
<asp:Label ID="lblALL" CssClass="txtLabel" runat="server" Text="Landline :"></asp:Label>
<asp:TextBox ID="txtALL" runat="server" CssClass="txtbox"></asp:TextBox>
</div>
<div class="row">
<asp:Label ID="lblAMobile" CssClass="txtLabel" runat="server" Text="Mobile :"></asp:Label>
<asp:TextBox ID="txtAMobile" runat="server" CssClass="txtbox"></asp:TextBox>
</div>
<div class="row">
<asp:Label ID="lblAEmail" CssClass="txtLabel" runat="server" Text="Email :"></asp:Label>
<asp:TextBox ID="txtAEmail" runat="server" CssClass="txtbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" ErrorMessage="*"
ControlToValidate="txtAEmail"></asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr><td colspan="2" align="center" height="40px">
<asp:Button ID="btnAddEmployee" runat="server" Text="Add Employee"
CssClass="btn" onclick="btnAddEmployee_Click" /> <input id="Reset1" type="reset" class="btn" value="Reset" />
</td></tr>
</table>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="atpEditEmployee" runat="server" >
<HeaderTemplate>Edit Employee</HeaderTemplate>
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<input id="Button2" type="button" value="Next" onclick="MoveTab(1)" />
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
You should look at utilising the ValidationGroup attribute for validators. For example for all the validators in the atpAddEmployee tab you would set the attribute like so ValidationGroup="atpAddEmployee" or any other name that you think appropriate.
I've taken one of the validators from the atpAddEmployee tab and added the attribute as described above.
<asp:RequiredFieldValidator ID="rfvCode" runat="server" ErrorMessage="*" ControlToValidate="txtACode" ValidationGroup="atpAddEmployee"></asp:RequiredFieldValidator>
For that particular tab you would also need to add the ValidationGroup attribute to the button that should trigger the validation, see below.
<asp:Button ID="btnAddEmployee" runat="server" Text="Add Employee" CssClass="btn" onclick="btnAddEmployee_Click" ValidationGroup="atpAddEmployee" />
Some further reading, MSDN and W3Schools.

Categories

Resources