Update TimeRecorded on edititemtemplate label vb.net - vb.net

I'm trying to have a label in the edititemtemplate of my gridview show the current date and time when the user clicks the edit linkbutton. When I put date.now in the data binding box, I can see the result I want, but when I click update, I get an error that the field can't be null. It works fine when I try it for the footer row, but not the edit row. Here's the footer row code that works:
'Get Time Recorded
Dim lblFtrTimeRecorded As Label = GridView1.FooterRow.FindControl("lblFtrTimeRecorded")
lblFtrTimeRecorded.Text = Date.Now
Here's the template field .aspx code:
<asp:TemplateField HeaderText="TimeRecorded" SortExpression="TimeRecorded">
<EditItemTemplate>
<asp:Label ID="lblEditTimeRecorded" runat="server"
Text='<%# Bind("TimeRecorded") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblFtrTimeRecorded" runat="server"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblTimeRecorded" runat="server"
Text='<%# Bind("TimeRecorded") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Any help is greatly appreciated!

Write this code :
Dim lblFtrTimeRecorded As Label = GridView1.Rows[GridView1.EditIndex].FindControl("lblFtrTimeRecorded")
lblFtrTimeRecorded.Text = Date.Now

Related

Changing a columns contents in a GridView from VB code

I am displaying a grid of data which includes a users name and profile pic etc.
If they haven’t uploaded a profile picture I have set the code below to set imgPic.ImageUrl = "~/files/images/blankProfilePic.png"
However, this works only for the 1st page of 6 pages of data displayed.. As soon as you switch to the 2nd page I get the missing picture icon as the code below is only working for the initial page displayed.
I need to change this code to work on every page as it is only called when the page loads.
VB CODE
Protected Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete
Dim acc As New accounts(Membership.GetUser.ProviderUserKey)
If acc.region = "North East" Then
For Each r As GridViewRow in gdvNorthEast.Rows
If r.RowType = DataControlRowType.DataRow Then
Dim imgPic As Image
imgPic = r.Cells(4).FindControl("imgProfilePic")
If imgPic.ImageUrl = "~/catalog/images/"
imgPic.ImageUrl = "~/files/images/blankProfilePic.png"
End If
End If
Next r
End If
ASP.NET
<asp:SqlDataSource
ID="DSLeaderboardNorthEast"
runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT tblAccounts.contactName, tblAccounts.profilePic, tblAccounts.minAge, tblAccounts.maxAge, tblAccounts.AccountID, tblAccounts.region
FROM tblAccounts
WHERE tblAccounts.region='North East'
ORDER BY tblAccounts.contactName ">
</asp:SqlDataSource>
<asp:GridView ID="gdvNorthEast" width="100%" runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="accountID" DataSourceID="DSLeaderboardNorthEast" PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Image ID="imgProfilePic" AlternateText="Players profile picture" runat="server" width="100px" height="100px" ImageUrl='<%# "~/catalog/images/" & Eval("ProfilePic").ToString %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="contactName" HeaderText="Player Name" />
<asp:BoundField DataField="minAge" HeaderText="Won" />
<asp:BoundField DataField="maxAge" HeaderText="Lost" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:HyperLink ID="hypMessage" runat="server" CssClass="nyroModalMsg" NavigateUrl='<%# "~/sendLeaderboardMessage.aspx?" & "&aID=" & Eval("accountID").ToString %>'>Msg</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Your foreach loop for assigning default image should be reusable method.
Use PageIndexChanged event handler to call the same reusable method.
Update:
Since the above option is not helping, follow this post
//RowDataBound Event
protected void gdvNorthEast_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking the RowType of the Row
if(e.Row.RowType==DataControlRowType.DataRow)
{
//your logic goes here without foreach loop
}
}
Use the Gridview_RowDataBound event to make the function of substitution instead use a for instruction

Exclude column in certain rows - SQL / Asp.Net

I'm wanting to change some code which was written for me. I would like to omit displaying the hyperlink where tblResults.videoLink=''
The hyperlink is displayed in the table as "Video Link" so people can click on it. I only want the words/hyperlink "Video Link" to appear in the row where people have entered one.
The code is Microsoft SQL but is written in the ASPX file. The back end is VB but there's VB is pretty much empty.. The ASPX file handles everything...
<h1>Recent Results</h1>
<asp:Panel ID="pnlMyWishes" runat="server">
<asp:SqlDataSource ID="DSWishes" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT TOP (100) PERCENT tblResults.wishID, tblResults.Player1AccountID, tblResults.Player2AccountID, tblResults.Player1Result, tblResults.Player2Result, tblResults.venue, tblResults.potSize, tblResults.player1Name, tblResults.player2Name, tblResults.videoLink, tblResults.date FROM tblResults ORDER BY tblResults.date DESC">
</asp:SqlDataSource>
<asp:GridView ID="gdvWishes" width="100%" runat="server" AllowPaging="True" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="wishID" DataSourceID="DSWishes" PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:BoundField DataField="player1Name" HeaderText="Player 1" />
<asp:TemplateField HeaderText="Result">
<ItemTemplate>
<asp:Label ID="lblP1" Text='<%# Eval("player1Result").ToString %>' runat="server" Visible="true">
</asp:Label><asp:Label ID="lblVs" Text=" - " runat="server" Visible="true"></asp:Label>
<asp:Label ID="lblP2" Text='<%# Eval("player2Result").ToString %>' runat="server" Visible="true"></asp:Label>
</ItemTemplate>
<ItemStyle Width="17%" />
</asp:TemplateField>
<asp:BoundField DataField="player2Name" HeaderText="Player 2" />
<asp:BoundField DataField="potSize" HeaderText="Pot" />
<asp:BoundField DataField="venue" HeaderText="Venue" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:HyperLink ID="hypVideoLink" runat="server" NavigateUrl='<%# Eval("videoLink").ToString %>'>Video Link</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
Create a sub like below, call it after the code that fills your GridView with data.
Private Sub HideBlankURLs()
For Each r As GridViewRow in gdvWishes.Rows
If r.RowType = DataControlRowType.DataRow Then //Execute the code only for datarow, excluding footer and header
Dim hypURL As HyperLink
hypURL = r.Cells(5).FindControl("hypVideoLink") //Goes to the column of VideoURL, index 5 is counting from 0 to 5
If hypURL.NavigateURL = "" Then //Checks if the URL is blank, if it is then hide the hyperlink control
hypURL.Visible = False
End If
End If
Next r
End Sub
This will loop through all the rows and look for the control in that column, check if navigate URL is blank, then hide that control.
NOTE: Replace the (//) with single quote (') because SO reads it as text enclosing character rather than a comment.

DetailsView ItemCommand Index outside the bounds of the array

I have the following DetailsView defined with a DropDownList of data and an Add ButtonField:
<asp:DetailsView ID="dvNewEntry" runat="server" Height="50px" Width="800px"
AutoGenerateRows="False" CssClass="gv_footer" BorderColor="Black"
BorderStyle="Solid" BorderWidth="2px" CellPadding="5" CellSpacing="8">
<FieldHeaderStyle CssClass="th_right" />
<Fields>
<asp:TemplateField HeaderText="Account Name">
<ItemTemplate>
<asp:Label ID="lblNewAcct" runat="server" Text='<%# Eval("ACCT_LIST") %
>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="ddAcctList" runat="server" DataSourceID="LUWAcctList"
DataTextField="ACCT_LIST" DataValueField="ACCT_LIST"
SelectedValue='<%# Bind("ACCT_LIST") %>' Font-Size="Small" Width="600">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="Save" CommandName="Add"/>
</Fields>
</asp:DetailsView>
In the codebehind - I have the following ItemCommand sub.
Protected Sub dvNewEntry_ItemCommand(ByVal sender As Object, ByVal e As DetailsViewCommandEventArgs) Handles dvNewEntry.ItemCommand
If e.CommandName = "Add" Then
Dim newA As String
newA = Trim(CType(dvNewEntry.FindControl("ddAcctList"), DropDownList).Text)
etc. etc.
Everything works fine when the user selects a value from the dropdown list. But if they don't make a selection and click the Add button - I get the following error when I land on the "If e.CommandName = Add" line:
Index was outside the bounds of the array
How do I handle this exception? Thanks
If (e.[someproperty] IsNot Null) Then --> your code solved the problem. Thanks Tanner

Format date in GridView

Please see the database DDL below:
create table datetest (datetime1 datetime)
insert into datetest values(getdate())
Please see the presentation code below:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# bind("datetime1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The output on the page is: 11/04/2013 19:51:40, but the database value is: 2013-04-11 19:51:40.017 (notice the milliseconds are chopped off the first value).
I have looked into this and have found a number of posts on here, which suggest using EVAL and String.Format, but I have been unsuccessful. How can I display milliseconds on the webpage?
I have tried the following code:
<asp:Label ID="Label1" runat="server" Text='<%
Eval("datetime1").ToString("dd/MM/yyyy hh:mm:ss.fff") %>'></asp:Label>
However, I am prompted with an exception: "Conversion from string "dd/MM/yyyy" to type 'Integer' is not valid."
<asp:Label ID="Label1" runat="server" Text='<%# Eval("datetime1").ToString("dd/MM/yyyy hh:mm:ss.fff") %>'></asp:Label>
Something like that should do it. If you need help with custom time formats, have a look here: http://msdn.microsoft.com/en-gb/library/8kb3ddd4.aspx
You do not need to do a template to change the format though, you can add {0:dd/MM/yyyy hh:mm:ss.fff} in the custom format property
You should use Eval with additional format parameter instead of Eval().ToString() call:
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("datetime1", "{0:dd/MM/yyyy hh:mm:ss.fff}") %>'></asp:Label>

How to remove one item from string in a textbox

I have a gridview with 2 columns.
First field contains checkbox named chkSelect and second column is a label which is binded with EmailId.
<asp:GridView ID="gvwNewsLetter" runat="server" AutoGenerateColumns="false" DataKeyNames="UserID">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelectMail" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmailID">
<ItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("EmailID")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
When I check each checkbox I have to display mailid in corresponding row in a textarea which is outside gridview like" abc#gmail.com,sdf#gmail.com". If I uncheck inbetween I have to remove that particular id from textbox. Can anybody help give the code to remove mailid on unchecking the checkbox.
Can you not just rebuild the entire string each time a checkbox is checked/unchecked? Might be faster than trying to parse through and modify the existing string each time.