Click here to Skip to main content
16,006,605 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,
I am working on C#,

code for .aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCommand="GridView1_RowCommand" PageSize="10"
class="table table-striped table-bordered datatables"
AllowPaging="True" >
<columns> <asp:TemplateField HeaderText="Employee Name">
<itemtemplate>
<asp:Label ID="lblempname" runat="server" Text='<%#Eval("empname") %>' />


<asp:TemplateField HeaderText="Work Type">
<itemtemplate>
<asp:Label ID="lblworktype" runat="server" Text='<%#Eval("worktype") %>' />


<asp:TemplateField HeaderText="Company Name">
<itemtemplate>
<asp:Label ID="lblcompanyname" runat="server" Text='<%#Eval("companyname") %>' />



<asp:TemplateField HeaderText="Date">
<itemtemplate>
<asp:TextBox ID="txtdate" runat="server" class="form-control"> <asp:CalendarExtender ID="txtdate_CalendarExtender" runat="server" Enabled="True" TargetControlID="txtdate" Format="dd/MM/yyyy">




<asp:TemplateField HeaderText="Start Time">
<itemtemplate>
<asp:TextBox ID="txtstime" class="timepicker form-control" runat="server">


<asp:TemplateField HeaderText="End Time">
<itemtemplate>
<asp:TextBox ID="txtetime" class="timepicker form-control" runat="server" >


<asp:TemplateField HeaderText="Attendance Symbol">
<itemtemplate>
<asp:DropDownList ID="ddlattsysmbol" runat="server" class="form-control" DataSourceID="SqlDataSource1"
DataTextField="shortname" DataValueField="id" AppendDataBoundItems="true">
<asp:ListItem Value="">Select Any One

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HRMConnectionString %>"
SelectCommand="SELECT DISTINCT * FROM [sysmbol_attend] WHERE ([cid] = @cid)">
<SelectParameters>
<asp:SessionParameter Name="cid" SessionField="CompanyID" Type="Int32" />
</SelectParameters>



<asp:TemplateField>
<itemtemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/images/AddNewitem.jpg"
CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="a" />


<pagersettings mode="NextPreviousFirstLast" pagebuttoncount="4" previouspagetext="Previous">
NextPageText="Next" FirstPageText="First" LastPageText="Last" />


.cs
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddNew")
{
GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
String curdate = (row.FindControl("txtdate") as TextBox).Text;
}

In curdate string i am getting the output is '29/07/2014' , I want to also get month from my curdate string output like '07'.
Posted

You could simply use string.SubString - that's trivial - but don't.
Instead, parse the date input using DateTime.TryParse and the user culture to get a DateTime, and get the month from that.
 
Share this answer
 
First read solution of Sergey Alexandrovich Kryukov (Solution 2) and than if you still go woth string than try my solution.. :)

C#
string curdate = "29/07/2014";

string Month = curdate.Split('/')[1].ToString();
 
Share this answer
 
Don't work with sheer strings in .NET. Instead, parse a string into System.DataTime, it will give you the pure data. Then manipulate it and use ToString to get string representation of data. Please see all the methods called Parse, TryParse, ParseExact and TryParseExact, and also ToString here:
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^],
see also http://msdn.microsoft.com/en-us/library/system.datetime.month(v=vs.110).aspx[^] :-).

—SA
 
Share this answer
 
Comments
CHill60 2-Aug-14 10:38am    
Strange that someone voted a 3 for the best advice given! 5'd
Sergey Alexandrovich Kryukov 2-Aug-14 20:16pm    
Thank you very much.
Well such votes is a usual thing these days...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900