Home »
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Disable Viewstate of a page (No postback) in C# .NET

No comments

Simply add this function in the code behind of that page
protected override void Render(HtmlTextWriter output)
        {
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
            base.Render(textWriter);
            textWriter.Close();
            string strOutput = stringWriter.GetStringBuilder().ToString();
            strOutput = Regex.Replace(strOutput, "<input[^>]*id=\"__VIEWSTATE\"[^>]*>", "", RegexOptions.Singleline);
            output.Write(strOutput);
        }

Generate Random String in C#

No comments
Sometimes when I want to generate a random string and I forget this function, so I have to search on google or in my old project. It's quiet annoyed. So I post it here to make everything more convenient xD.
    private readonly Random random = new Random();
    private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private string RandomString(int size)
    {
        StringBuilder strBuilder = new StringBuilder();

        for (int i = 0; i < size; i++)
        {
            strBuilder.Append(chars[random.Next(chars.Length)]);
        }
        return strBuilder.ToString();
    }
Because the random object is seeded from the system clock, so if you instantiate it inside the RandomString(), you will get the same result (string) when you call your method several times in quick succession. So you can put your Random instance outside the method as above code or you can do as follow for your Random object:
private static Random random = new Random((int)DateTime.Now.Ticks);
Hope this entry help you! If you found any mistake or error in this entry, please let me know. I'll try to fix that a.s.a.p. Any solution is highly appreciated! -Share2Learn-

Convert Dataset to JSON String and vice versa using Json.NET

18 comments
Sometimes it's more convenient to use a string rather a dataset. So if you handle a dataset, you must convert it to string. The best way to do that is to convert it to json string.
And then, after you finish everything, you'll need to convert that string back to dataset so that you can save the result into the database or display it.
In this entry I'll show you the easiest way to do that using Json.NET
In this project I use .NET 2.0. If you want to use .NET 3.5 all you have to do is remove reference Newtonsoft.Json.Net20 and add the Newtonsoft.Json.Net35 in bin folder.

Dataset to Json String

StringWriter sw = new StringWriter();
ds.WriteXml(sw, XmlWriteMode.IgnoreSchema);
XmlDocument xd = new XmlDocument();
xd.LoadXml(sw.ToString());

string jsonText = JsonConvert.SerializeXmlNode(xd);
txtResult.Text = jsonText;
First, if you want to convert DataSet to Json String you have to convert DataSet to XMLDocument (Here I use StringWritter).
When you've already added Newtonsoft.Json.Net to you References, you can use JsonConvert to convert between JSON and XML. If you want to convert o JSON String, use SerializeXmlNode. This method takes an XmlNode and serializes it to JSON text.

dd/MM/yyyy Date Format in ASP.Net AJAX Calendar

1 comment
When you use Calendar control in ajax, the default format is MM/dd/yyyy. You can change the format using MaskedEditExtender but it doesn't work for dd/MM/yyyy format. There's a simple way that do the trick is to add the CultureName "en-GB" for the MaskedEditExtender control.
//






//
Thanks to DevCurry.com for this solution!

DateTime Format in C# [Convert String to DateTime and backwards]

2 comments
Converting string to datetime and backwards is sometime a problem. I used to face this problem. Everytime when I work with DateTime format, I google it, and it takes a lot of time to find out the solution. After several times, I think that I found the suitable solution that works perfectly (for me xD).

Convert String to DateTime


When you want to convert a string to a DateTime object, all you have to do is to use DateTimeFormatInfo.
string dtStr = "2010-12-01 15:23:42.123";
DateTime dt;
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
try{
dtfi.ShortDatePattern = "yyyy-MM-dd HH:mm:ss.fff";
//Or you can use dtfi.LongDatePattern
dtfi.DateSeparator = "-"; //date separator character
dtfi.TimeSeparator = ":"; //time sepatator character
dt = Convert.ToDateTime(strDate, dtfi);
}catch (Exception ex){
//Do something here
}

The convenient when you use DateTimeFormatInfo is that you can define the DateTime format inside the string. In the code above, the result will be 2010 Dec 01st Time. But if I change the pattern into yyyy-dd-MM HH:mm:ss.fff then the result will be 2010 Jan 12th Time
The only thing is to make sure your string match the format xD.