Often we face this problem, there are may example on google, but still i feel like sharing this with you, this is how you can play with any date format in any culture.
DateTime dob = DateTime.Now.AddYears(-18);
if (!string.IsNullOrEmpty(txtDOB.Text))
{
IFormatProvider culture = new CultureInfo("en-US", true); // this is how you can set any culture you want !
dob = DateTime.ParseExact(txtDOB.Text, "dd/MM/yyyy", culture);
}
profile.DOB = dob;
Monday, September 5, 2011
Tuesday, August 9, 2011
convert enum to list in csharp
This function will convert any enum to a List object
public class EnumHelper
{
/// < summary >
/// Convert enum to a list object.
/// < /summary >
/// < typeparam name="T" >< /typeparam >
/// < returns >< /returns >
public static List< T > EnumToList< T >()
{
Type enumType = typeof(T);
// You can't use type constraints on value types, so have to check & throw error.
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum type");
Array enumValArray = Enum.GetValues(enumType);
List< T > enumValList = new List< T >(enumValArray.Length);
foreach (int val in enumValArray)
{
enumValList.Add((T)Enum.Parse(enumType, val.ToString()));
}
return enumValList;
}
// Now call the function with enum you want to conver into a list EnumHelper.EnumToList< EventState >()
public static List < EventState > GetEventStates()
{
List< EventState > eventStates = EnumHelper.EnumToList< EventState >();
//.FindAll(
//delegate(EventState x)
//{
// return x != EventState.Cancelled ;
//});
return eventStates;
}
Tuesday, April 26, 2011
WCF Service debuging technique from client
While implementing any WCF service in any client application, you may find lots of issue, here i share some experience how you can get to know whats going wrong at service end!

You just need to turn on Tracing and Messging on WCF server

As a result you see the following set of tags has been added in your web config file.
< system.diagnostics >
< sources >
< source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
propagateActivity="true" >
< listeners >
< add type="System.Diagnostics.DefaultTraceListener" name="Default" >
< filter type="" / >
< /add >
< add name="ServiceModelTraceListener" >
< filter type="" / >
< /add >
< /listeners >
< /source >
< source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing" >
< listeners >
< add type="System.Diagnostics.DefaultTraceListener" name="Default" >
< filter type="" / >
< /add >
< add name="ServiceModelMessageLoggingListener" >
< filter type="" / >
< /add >
< /listeners >
< /source >
< /sources >
< sharedListeners >
< add initializeData="F:\MyWCFFolder\Web_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp" >
< filter type="" / >
< /add >
< add initializeData="F:\MyWCFFolder\Web_messages.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp" >
< filter type="" / >
< /add >
< /sharedListeners >
< /system.diagnostics >

You just need to turn on Tracing and Messging on WCF server

As a result you see the following set of tags has been added in your web config file.
< system.diagnostics >
< sources >
< source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
propagateActivity="true" >
< listeners >
< add type="System.Diagnostics.DefaultTraceListener" name="Default" >
< filter type="" / >
< /add >
< add name="ServiceModelTraceListener" >
< filter type="" / >
< /add >
< /listeners >
< /source >
< source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing" >
< listeners >
< add type="System.Diagnostics.DefaultTraceListener" name="Default" >
< filter type="" / >
< /add >
< add name="ServiceModelMessageLoggingListener" >
< filter type="" / >
< /add >
< /listeners >
< /source >
< /sources >
< sharedListeners >
< add initializeData="F:\MyWCFFolder\Web_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp" >
< filter type="" / >
< /add >
< add initializeData="F:\MyWCFFolder\Web_messages.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp" >
< filter type="" / >
< /add >
< /sharedListeners >
< /system.diagnostics >
Stop subsequent submit button clicks when a earlier request is being processed in ASP.NET
How we can stop the subsequent click when a earlier request is still been processed.
here is the javascript code you need to place above your update panel.
< script type="text/javascript" language="javascript" >
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_initializeRequest(StopPostbackOnSubsequentSubmitClicks);
function StopPostbackOnSubsequentSubmitClicks(sender, args) {
if (requestManager.get_isInAsyncPostBack() &
args.get_postBackElement().id == '<%=btnSubmit.ClientID %>')
{
args.set_cancel(true);
alert('Please wait, earlier request is still being processed');
}
}
< /script >
Now write your update panel code
< asp:UpdatePanel runat="server" ID="upnlLogin" UpdateMode="Conditional" >
< ContentTemplate >
// do whatever you need
< asp:Button ID="btnSubmit" runat="server" Text="Sign in" CssClass="button" OnClick="btnSubmit_Click" />
< /ContentTemplate>
< /asp:UpdatePanel>
< asp:UpdateProgress AssociatedUpdatePanelID="upnlLogin" runat="server" >
< ProgressTemplate>
< img src="../Images/spinner.gif" / >
< /ProgressTemplate >
< /asp:UpdateProgress >
// Now see the code behind implementation
protected void btnSubmit_Click(object sender, EventArgs e)
{
Thread.Sleep(1000);
}
here is the javascript code you need to place above your update panel.
< script type="text/javascript" language="javascript" >
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_initializeRequest(StopPostbackOnSubsequentSubmitClicks);
function StopPostbackOnSubsequentSubmitClicks(sender, args) {
if (requestManager.get_isInAsyncPostBack() &
args.get_postBackElement().id == '<%=btnSubmit.ClientID %>')
{
args.set_cancel(true);
alert('Please wait, earlier request is still being processed');
}
}
< /script >
Now write your update panel code
< asp:UpdatePanel runat="server" ID="upnlLogin" UpdateMode="Conditional" >
< ContentTemplate >
// do whatever you need
< asp:Button ID="btnSubmit" runat="server" Text="Sign in" CssClass="button" OnClick="btnSubmit_Click" />
< /ContentTemplate>
< /asp:UpdatePanel>
< asp:UpdateProgress AssociatedUpdatePanelID="upnlLogin" runat="server" >
< ProgressTemplate>
< img src="../Images/spinner.gif" / >
< /ProgressTemplate >
< /asp:UpdateProgress >
// Now see the code behind implementation
protected void btnSubmit_Click(object sender, EventArgs e)
{
Thread.Sleep(1000);
}
Monday, April 25, 2011
Sql paging example, paging in sql 2000
Create PROCEDURE spPaggingExample
(
@ProductInfo varchar(100)=null,
@SearchBy varchar(50)=null,
@startRowIndex int,
@maximumRows int,
@totalRows int OUTPUT
)
AS
-- sample spPaggingExample '',null,1,10,0
DECLARE @first_id int, @startRow int
SET @startRowIndex = @startRowIndex * @maximumRows
IF @startRowIndex = 0 SET @startRowIndex = 1
SET ROWCOUNT @startRowIndex
SELECT @first_id = UniqueTableId FROM myProductTable where (Isnull(IsApproved,0)=1 and ProductName like '%'+@ProductInfo+'%' or ProductDescription like '%'+@ProductInfo+'%' ) ORDER BY UniqueTableId
--PRINT @first_id
SET ROWCOUNT @maximumRows
SELECT * FROM myProductTable WHERE Isnull(IsApproved,0)=1 and UniqueTableId >= @first_id and (ProductName like '%'+@ProductInfo+'%' or ProductDescription like '%'+@ProductInfo+'%' ) ORDER BY UniqueTableId
SET ROWCOUNT 0
-- GEt the total rows
SELECT @totalRows = COUNT(UniqueTableId) FROM myProductTable where (Isnull(IsApproved,0)=1 and ltrim(rtrim(ProductName)) like '%'+@ProductInfo+'%' or ProductDescription like '%'+@ProductInfo+'%' )
RETURN @totalRows
(
@ProductInfo varchar(100)=null,
@SearchBy varchar(50)=null,
@startRowIndex int,
@maximumRows int,
@totalRows int OUTPUT
)
AS
-- sample spPaggingExample '',null,1,10,0
DECLARE @first_id int, @startRow int
SET @startRowIndex = @startRowIndex * @maximumRows
IF @startRowIndex = 0 SET @startRowIndex = 1
SET ROWCOUNT @startRowIndex
SELECT @first_id = UniqueTableId FROM myProductTable where (Isnull(IsApproved,0)=1 and ProductName like '%'+@ProductInfo+'%' or ProductDescription like '%'+@ProductInfo+'%' ) ORDER BY UniqueTableId
--PRINT @first_id
SET ROWCOUNT @maximumRows
SELECT * FROM myProductTable WHERE Isnull(IsApproved,0)=1 and UniqueTableId >= @first_id and (ProductName like '%'+@ProductInfo+'%' or ProductDescription like '%'+@ProductInfo+'%' ) ORDER BY UniqueTableId
SET ROWCOUNT 0
-- GEt the total rows
SELECT @totalRows = COUNT(UniqueTableId) FROM myProductTable where (Isnull(IsApproved,0)=1 and ltrim(rtrim(ProductName)) like '%'+@ProductInfo+'%' or ProductDescription like '%'+@ProductInfo+'%' )
RETURN @totalRows
Subscribe to:
Posts (Atom)