Feeds:
Posts
Comments

Archive for the ‘C#.NET’ Category

I was calling my WCF services from JQuery. Basically I was calling $getJSON to retrieve JSON data to display it in a dynamic table.

I started facing problem with the data sequence. It looked like i was not getting data at the same sequence it is in the class. My class looked like that
[DataContract]
public class MyClass
{
public DateTime Interval { get; set; }
public Double Actual { get; set; }
public String Units { get; set; }

public MyClass(DateTime interval, double actual, string units)
{
this.Interval = interval;
this.Actual = actual;
this.Units = units;
}
}

It was returning data in the order of Actual, Interval, Units.
Looks like it sends data sorting by DataMember Name
To Solve the issue we need to add Order option for every DataMember.
[DataContract]
public class MyClass
{
[DataMember(Order = 0)]
public DateTime Interval { get; set; }
[DataMember(Order = 1)]
public Double Actual { get; set; }
[DataMember(Order = 2)]
public String Units { get; set; }

public MyClass(DateTime interval, double actual, string units)
{
this.Interval = interval;
this.Actual = actual;
this.Units = units;
}
}

By the way it did not solve my problem fully either. I needed to call DataContractSerializer to serialize/sequence/order the data before sending.
public List GetData(string token)
{
List data = null;
foreach (DataRow dr in dt.Rows)
{
MyClass points = new MyClass(param1,param2,param3);
DataContractSerializer contractSerializer = new DataContractSerializer(typeof(MyClass));
data.Add(points);
index++;
}
}

return data;
}

This post describes this too.

Read Full Post »

In my aspx page I have several CheckBoxList and the requirement is client side validation for all the CheckBoxList in the page before page submission.
As CheckListBox do not support Required Validator we need to user Custom Validator for this validation.

Here is the sample of my ASPX code

<div>
<asp:CheckBoxList runat=”server” ID=”chkList1″>
<asp:ListItem Text=”List Item 1_1″ Value=”11″></asp:ListItem>
<asp:ListItem Text=”List Item 1_2″ Value=”12″></asp:ListItem>
</asp:CheckBoxList>

<asp:CustomValidator ID=”validator_chkList1″ ClientValidationFunction=”ValidateCheckList” runat=”server”>*Select at least one item</asp:CustomValidator>

<asp:CheckBoxList runat=”server” ID=”chkList2″>
<asp:ListItem Text=”List Item 2_1″ Value=”21″></asp:ListItem>
<asp:ListItem Text=”Lsit Item 2_2″ Value=”22″></asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID=”validator_chkList2″ ClientValidationFunction=”ValidateCheckList” runat=”server”>*Select at least one item</asp:CustomValidator>

<asp:Button runat=”server” ID=”btnSubmit” onclick=”btnSubmit_Click” />
</div>

&nbsp;

I used the following javascript to do the validation

function ValidateCheckList(sender, args) {
var chkControlId = sender.id.replace("validator_", "");
alert(chkControlId);
var options = document.getElementById(chkControlId).getElementsByTagName('input');
var ischecked = false;
args.IsValid = false;
for (i = 0; i < options.length; i++) {
var opt = options[i];
if (opt.type == "checkbox") {
if (opt.checked) {
ischecked = true;
args.IsValid = true;
}
}
}
}

Nice and easy trick but it took around two hours for me to figure out, that’s why sharing.
Happy coding. ūüôā

Read Full Post »

Automate internet explorer

Recently I was trying to automate one step of my internet explorer actually my automation step was very simple I just needed to navigate an URL, read DOM of the URL, get text boxes and enter data in it. After entering data in text boxes I was suppose to click on login button.

Actually one of my 3rd party tool is depending on the login and was trying to automate the login and run the 3rd party tool so I need not to go to server everyday and do that.

It is easy to automate Internet Explorer by using a COM control named Microsoft Internet Controls (SHDocVw). To browse the DOM Microsoft.mshtml is used. I found a very helpful article with code in codeproject.com. here is the article : Automating Internet Explorer

By the way, I was facing problem to insert data in the text box  and click button. It worked fine in my local computer where VS is installed but for the computers where VS is not installed it shown exception to convert _Comobject to mshtml.HTMLInputElementClass . Download modified file.

Read Full Post »

Send asynchronous mail using asp.net

Send mail is an important and common feature in asp.net. using system.net.mail namespace  we can send mail. In that article my main concert is to show how shall we send mail asynchronously, as for a bulk of mail sending asynchronous mail send is very important and asp.net have that feature in it.

I had written a wiki in asp.net on with code on  send asynchronous mail.

here you will get codes do send  asynchronous mail http://wiki.asp.net/page.aspx/536/send-asynchronous-mail-using-aspnet/

Read Full Post »

I was reading httpwebresponse data and get the basic data from the html data. So i need to remove all html tag, script tags, style tags from page to get original text data. So use a simple regular expression to remove tags. this regular expression was simple like that.

Regex.Replace(mainData,@”<scripts[^>]*>.*?</script>|<s*(?!/?(?:br?|i|p|u)b[^>]*>)[^>]*>”,””, RegexOptions.IgnoreCase | RegexOptions.Singleline) ;

I was working fine but i found that for some pages these are making problem specially problems are coming to remove javascript and styles from html data and data was not coming in a good format.

Finally I have got a very nice tool to do that . name of the tool is Html Agility .

this is a very nice features in html agility , it has libraires to convert html to xml, html to rss and html to text. I used html to text yo convert my httpresponse data to text.

in html agility you will find a class HTMLDocument.cs If you want to convert your webresponses then just add these codes in Load method, bacuase in load method this is reading html file from a local drive so to read webresponse use that code.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), OptionDefaultStreamEncoding);

Read Full Post »

Caching is a very important concept in programming . caching is a very nice technique to speed up processing. To increase performance of application caching is a great enhance.

One of my buddy razwan has written a very nice article on asp.net caching using cache dependency on file. You can follow this article to implement that in your applicaiton to speed it up.

here is the link ASP.NET Caching with Cache Dependency on a File

Read Full Post »

Now a day’s event management is becoming a very popular idea especially in case of web application development. Most of the application is synchronizing with outlook contacts and calendar. Here is a small tool to create event both hour event and day event. This concept is very simple creating a ics file and sending as mail attachment.
event1.png
I have written an small article on how to send event through mail. Please check here to get complete code.
http://www.codeproject.com/KB/applications/SendAppointment.aspx

Read Full Post »

Older Posts »