Failing to update my Grid using template field - c#

I use EntityDataSource.
When I want to insert into the "ShiftDetails" table with the following data: (3,1,04:00:00)
It is inserted fine if I use:
<telerik:GridBoundColumn DataField="startTime" HeaderText="startTime" SortExpression="startTime"
UniqueName="startTime" DataType="System.TimeSpan" EmptyDataText="00:00:00" >
When I try this with templatefield, the startTime is 00:00:00 instead of 04:00:00
<telerik:GridTemplateColumn HeaderText="startTime" >
<ItemTemplate>
<asp:Label runat="server" ID="lblstartTime" Text='<%# Eval("startTime")%>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<telerik:RadDateInput ID="txt_startTime" runat="server" DbSelectedDate='<%# Eval("startTime","{0:hh\\:mm\\:ss}") %>' DateFormat="HH:mm:ss" DisplayDateFormat="HH:mm:ss"
Culture="English (United States)" LabelWidth="64px"
Width="160px" Height="20px" EmptyMessage="!">
<EmptyMessageStyle ForeColor="Red" />
</telerik:RadDateInput>
</InsertItemTemplate>
<EditItemTemplate>
<telerik:RadDateInput ID="txt_startTime" runat="server" DateFormat="HH:mm:ss" DisplayDateFormat="HH:mm:ss"
Culture="English (United States)" LabelWidth="64px"
Width="160px" Height="20px" EmptyMessage="!">
<EmptyMessageStyle ForeColor="Red" />
</telerik:RadDateInput>
</EditItemTemplate>
</telerik:GridTemplateColumn>
My rad grid is master-details
the source of the details :
<ef:EntityDataSource ID="GetSourceDetail" runat="server" ContextTypeName="ResidenceShift.RShiftEntities" EntitySetName="ShiftDetails" EnableDelete="True" EnableFlattening="False" EnableInsert="True" EnableUpdate="True" ConnectionString="name=RShiftEntities" DefaultContainerName="RShiftEntities" AutoGenerateWhereClause="True" Include="ResidenceShift,ShiftType" AutoPage="False" AutoSort="False" EntityTypeFilter="" Select="" Where="" >
<WhereParameters>
<asp:SessionParameter DefaultValue="0" Name="shiftId" SessionField="shiftId" DbType="Int32" />
</WhereParameters>
</ef:EntityDataSource>
Why does it work fine with the bound field and then fails with the template field?!

Try using RadDateTimePicker instead, as it should also submit the time.

Related

Compute the ReadOnly column in real time in gridview and save the computed value to SQL Server

I am trying to compute the column in real time meaning the calculation should be seen before an edit link is clicked. The weight first half is already found in the gridview.
The gridview is then edited to add the rating first half. the 2, weight first half and rating first half needs to be multiplied to get the result that is score first half and score first half I made it a ReadOnly.
Now I want the score first half to be saved in the SQL Server database. Mind you I already have the update command that the SqlDataSource generates.
Here is my markup:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="P_C_ID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="P_C_ID"
InsertVisible="False" SortExpression="P_C_ID">
<EditItemTemplate>
<asp:Label ID="Label1"
runat="server"
Text='<%# Eval("P_C_ID") %>'>
</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1"
runat="server"
Text='<%# Bind("P_C_ID") %>'>
</asp:Label>
</ItemTemplate>
<asp:TemplateField HeaderText="WEIGHT_FIRST_HALF"
SortExpression="WEIGHT_FIRST_HALF">
<EditItemTemplate>
<asp:TextBox ID="TextBox5"
runat="server"
ReadOnly="True"
Text='<%# Bind("WEIGHT_FIRST_HALF") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6"
runat="server"
Text='<%# Bind("WEIGHT_FIRST_HALF") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label9"
runat="server"
Text='<%# Bind("SCORE_FIRST_HALF") %>'>
</asp:Label>
</ItemTemplate>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:AWPConnectionString5 %>"
UpdateCommand="UPDATE [Performance_Rating_2] SET [WEIGHT FIRST HALF] = #WEIGHT_FIRST_HALF, [RATING FIRST HALF] = #RATING_FIRST_HALF, [SCORE FIRST HALF], [RATING FIRST HALF] = #RATING_FIRST_HALF = #SCORE_FIRST_HALF WHERE [P_C_ID] = #P_C_ID">
<asp:Parameter Name="WEIGHT_FIRST_HALF"
Type="Decimal" />
<UpdateParameters>
<asp:Parameter Name="RATING_FIRST_HALF"
Type="Int32" />
<asp:Parameter Name="SCORE_FIRST_HALF"
Type="Decimal" />
</UpdateParameters>
Okay folks the program is long. I just took part that is not working fine. But should you need clarity do not hesitate to ask for clarity. Right now only the weight and rating is being saved. the computed is not saved at all. I actually do not want to write it in C# codes as I need results in real time

datepicker updating from itemtemplate

I have code to update a date from a sqldatasource updateCommand.
Code:
<asp:GridView ID="gdLog" runat="server" GridLines="Horizontal" CellPadding="3" CssClass="gridRows" HeaderStyle-CssClass="gridHreader" AlternatingItemStyle-CssClass="gridAlterRows" Width="100%" AutoGenerateColumns="False" AutoGenerateEditButton="True" DataKeyNames="hometemplateID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="hometemplateID" HeaderText="hometemplateID" InsertVisible="False" ReadOnly="True" SortExpression="hometemplateID" Visible="False" />
<asp:BoundField DataField="ttitle" HeaderText="title" SortExpression="ttitle" />
<asp:TemplateField HeaderText="start">
<ItemTemplate>
<asp:Label runat="server" ID="tstart"
Text='<%# makeShortDate(DataBinder.Eval(Container.DataItem, "tstart")) %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tStartdEdit" runat="server" class="form-control calender-icon datepicker date" Text='<%# makeShortDate(DataBinder.Eval(Container.DataItem, "tstart")) %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="end">
<ItemTemplate>
<asp:Label runat="server" ID="tend"
Text='<%# makeShortDate(DataBinder.Eval(Container.DataItem, "tend")) %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tendEdit" runat="server" class="form-control calender-icon datepicker date" Text='<%# makeShortDate(DataBinder.Eval(Container.DataItem, "tend")) %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CheckBoxField DataField="tActive" HeaderText="Active" SortExpression="tActive" />
<asp:BoundField DataField="tdatecreated" HeaderText="date added" InsertVisible="False" ReadOnly="True" SortExpression="tdatecreated" />
</Columns>
<HeaderStyle CssClass="gridHreader">
</HeaderStyle>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Dbconnection %>" SelectCommand="EXEC [usp_SelectHomePageItems] #templateid=1, #rows=1" UpdateCommand="update hometemplate set title=#ttitle, startdate=#tstart, isActive=#tActive where hometemplateID=#hometemplateID">
<UpdateParameters>
<asp:Parameter Name="tstart" />
<asp:Parameter Name="tend" />
</UpdateParameters>
</asp:SqlDataSource>
There are 2 datepickers (start and end). I need to use makeShortDate function to format the text for the datepicker(s) because when I pick from it, it seems to use the format month namespacedayspaceyear
If I don't do that, and simply use Bind("tstart") on edititemtemplate, then after I hit 'edit', it shows in the textbox as 25/7/2018 12:00:00 AM....day first for some reason, and I don't want that to confuse my customers. It's also ugly showing the 12am stuff. It also selects the wrong date when I click the calender icon, whereas if I format first, it selects the right one.
When I try my code as is, the date gets set to blank for some reason. I think I need to Bind on the edititemTemplate but as mentioned I need to format the date with the function makeShortdate.
So how do I get the textbox for the dates to be month/day/year after hit 'edit' instead of the day/month/year 12:00:00am thing? I can do it with the makeshortdate function but then the date is set to null for some reason.
Sorry about the code indent, not sure how to de-indent a block of code on here.
The datepicker is datepicker for bootstrap. In the header I use:
$(function () {
$('.datepicker').datepicker({
format: 'MM dd yyyy',
autoclose: true,
});
});
ok got it with formatting:
<asp:TemplateField HeaderText="start">
<ItemTemplate>
<asp:Label runat="server" ID="tstart" DataField="startdate"
Text='<%# DataBinder.Eval(Container.DataItem, "startdate","{0:MMM dd yyyy}") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="tStartdEdit" runat="server" class="form-control calender-icon datepicker date" Text='<%# Bind("startdate","{0:MMM dd yyyy}") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

'detailsUserTitle' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

DropDownList keeps returning this error :(
Here is my aspx code
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="username" DataSourceID="SqlDataSource1" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" SortExpression="title">
<EditItemTemplate>
<asp:DropDownList ID="detailsUserTitle" runat="server" SelectedValue='<%# Bind("title") %>' DataSourceID="SqlDataSource1">
<asp:ListItem Value="-1">Select Title</asp:ListItem>
<asp:ListItem Value="Mr">Mr</asp:ListItem>
<asp:ListItem Value="Mrs">Mrs</asp:ListItem>
<asp:ListItem Value="Miss">Miss</asp:ListItem>
<asp:ListItem Value="Ms">Ms</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorTitle"
ControlToValidate="detailsUserTitle" runat="server" ErrorMessage="Title is a required field"
Text="*" ForeColor="Red" InitialValue="-1"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
I have been looking online for advice, but there hasn't been a conclusive one that helped me. Please help. I didn't add any code behind related to this issue.
Thank you
Your issue is with the DDL attribute SelectedValue='<%# Bind("title") %>'
Your SQL DataSource "SqlDataSource1" may not be returning any of the values from Mr, Mrs, Miss or MS.
If Title field has a different value you get this error
UPDATE
You may validate your Title string as below before binding. But, I wouldn't recommend this. I prefer if you could validate the values before retrieving from your data source. The ideal solution would be to bind the DropDownList with same set of values before finding the values in it. then you won't miss anything.
Here's the quick fix. :-)
SelectedValue='<%# (!string.IsNullOrEmpty(Bind("title")) || "Mr, Mrs, Miss, MS".IndexOf(Bind("title")) > -1) ? Bind("title") : "-1" %>'
UPDATE 2
Here you go :-D
SelectedValue='<%# (!string.IsNullOrEmpty((string)Eval("title")) || "Mr, Mrs, Miss, MS".IndexOf((string)Eval("title")) > -1) ? (string)Eval("title") : "-1" %>'
Please accept the answer if it resolves your issue. All the best!

Paging not working in GridView when ajax control is removed

I have a grid view in my aspx page. I have enabled paging also.
Paging is working fine when an ajax control is in page, but when I removed the ajax control paging is not working anymore. ie, on clicking page number 2 grid becomes empty.
What could be the possible reason? Please suggest a solution.
code
<asp:GridView ID="SitesGrid" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SitesDataMgr" AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="3" CellSpacing="3" OnRowDataBound="viewSite_RowDataBound" class="tbl_blck gridtable clearfix" PageSize="4" EmptyDataText="No rows found">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="clientID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White" SortExpression="ClientName">
<ItemTemplate>
<asp:Label ID="clientLabel" runat="server" Text='<%# Bind("ClientName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White" SortExpression="SiteStatus">
<ItemTemplate>
<asp:Label ID="siteLabel" runat="server" Text='<%# Bind("SiteStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-ForeColor="White" >
<ItemTemplate>
<asp:LinkButton ID="viewSite" runat="server" OnClick="viewSite_click" CausesValidation="False" CommandName="Edit"
Text="View"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="SitesDataMgr" runat="server" ConvertNullToDBNull="True" OldValuesParameterFormatString="{0}" SelectMethod="GetAllSitesByUser" FilterExpression="ClientName LIKE '%{0}%'" TypeName="Creation.DataMgr">
<SelectParameters>
<asp:SessionParameter Name="createdUserID" Type="String" SessionField="strUserName" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="ClientName" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:ObjectDataSource>
It looks like you have not added the SelectCount method to your ObjectDataSource.
This is required for the ObjectDatasource to correctly page your data.
Your select method needs the int parameters maximumRows and startRowIndex This is the default parameter names required for paging. if you do have them in. Please use them to filter your data.

asp.net entity framework <%# Bind("linkedTable.Field") %>

.NET 4 ASP.NET
I have a DetailsView that is displaying an entity framework record for a table that has a linked lookup table. I have an asp:BoundField with the datafield set as "linkedTable.Field" and it displays a value.
<asp:BoundField DataField="linkedTable.Field" HeaderText="linkedTable.Field"
SortExpression="linkedTable.Field" />
I am trying to use that value in an asp:TemplateField but when I try to get it using:
<asp:TemplateField HeaderText="Field" SortExpression="linkedTable.Field" >
<EditItemTemplate>
<asp:Label runat="server" ID="lblField" Text='<%# Bind("linkedTable.Field") %>' />
</EditItemTemplate>
</asp:TemplateField>
Nothing shows up in the label. I can change the Bind() to a field that is not part of the linked table and it works (i.e. the "ID" field). My problem is I don't understand why the linkedtable.Field value shows up in one context and not in the other.
FYI, my data connection is a EntityDataSource
<asp:EntityDataSource ID="edsNYSEDaily" runat="server"
ConnectionString="name=ServerDBEntities"
DefaultContainerName="ServerDBEntities" EntitySetName="tblNYSE"
EntityTypeFilter="tblNYSE" EnableUpdate="True" EnableFlattening="true"
AutoGenerateWhereClause="True" Select="" Where="">
<WhereParameters>
<asp:QueryStringParameter DefaultValue="0" Name="ID"
QueryStringField="ID" Type="Int32" />
</WhereParameters>
Let me know if you need any other information. I am stuck
Ok, discovered the problem:
Needed to add Include="linkedTable" to the EntityDataSource tag. Still not sure why it was even working in the <asp:DataBound /> tag.
Source for the answer: forums.asp.net
Copy of the text:
you should start here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.include.aspx
notice that, you won't be able to Bind (I mean two-way data-binding) the related entities (see the remarks there).
and, you'd have to use a TemplateField for those properties.
see this example (I used the link table 'TableAB' for EntitySetName and included the other two):
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="EntityDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="IDA" HeaderText="IDA" SortExpression="IDA" />
<asp:BoundField DataField="IDB" HeaderText="IDB" SortExpression="IDB" />
<asp:TemplateField HeaderText="TableA Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("TableA.NameA") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TableB Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("TableB.NameB") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=ABLinkEntities"
DefaultContainerName="ABLinkEntities" EnableDelete="True" EnableFlattening="False"
EnableInsert="True" EnableUpdate="True" EntitySetName="TableABs" Include="TableA,TableB">
</asp:EntityDataSource>
you'll have to re-consider the updates and deletes, you could do them manually.

Categories