Monday, November 1, 2010

Advance Search

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

public static class AdvancedSearchCriteria
{
public static string MouldSearchCriteria(string searchCriteria)
{
string newSearchCriteriaString = ConvertAllWhiteSpaceToSingleSpace(searchCriteria);
int searchCriteriaSpaceIndex = searchCriteria.IndexOf(" ");
int searchCriteriaQuotesIndex = searchCriteria.IndexOf("\"");
int countForQuotes = CountForQuotes(searchCriteria);

if (countForQuotes > 1)
{
if (countForQuotes % 2 == 0)
{
int cursorPosition = 0;
int quotesLoops = countForQuotes / 2;
List quoteTextList = new List();

for (int i = 0; i < quotesLoops; i++)
{
int startQuote = searchCriteria.IndexOf('"', cursorPosition);
int endQuote = searchCriteria.IndexOf('"', startQuote + 1);
cursorPosition = endQuote + 1;
string t = GetTextWithinQuotes(startQuote + 1, (endQuote - startQuote) - 1, searchCriteria);
quoteTextList.Add(t);
}

string t1 = CreateSearchTextForQuoteValues(quoteTextList);
string t2 = CreateSearchTextForNonQuoteValues(quoteTextList, searchCriteria);

if (t2.Equals(""))
newSearchCriteriaString = t1;
else
newSearchCriteriaString = t1 + " and " + t2;
}
else
{
int quoteLastIndex = searchCriteria.LastIndexOf("\"");
searchCriteria = searchCriteria.Remove(quoteLastIndex, 1);
newSearchCriteriaString = MouldSearchCriteria(searchCriteria);
}
}
else
{
if (searchCriteriaSpaceIndex == -1 && searchCriteriaQuotesIndex == -1)
newSearchCriteriaString = AddQuotes(searchCriteria);

if (searchCriteriaSpaceIndex == -1 && searchCriteriaQuotesIndex >= 0)
newSearchCriteriaString = AddQuotes(searchCriteria.Replace("\"", ""));

if (searchCriteriaSpaceIndex >= 0)
newSearchCriteriaString = GetSpaceSplit(searchCriteria);

}
return newSearchCriteriaString;

}

public static string CreateSearchTextForQuoteValues(List quoteTextList)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < quoteTextList.Count; i++)
{
sb.Append(AddQuotes(quoteTextList[i]));

if (i != quoteTextList.Count - 1)
sb.Append(" and ");
}

return (sb.ToString());

}

public static string CreateSearchTextForNonQuoteValues(List quoteTextList, string searchString)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < quoteTextList.Count; i++)
{
searchString = searchString.Replace(AddQuotes(quoteTextList[i]), "");
}

searchString = ConvertAllWhiteSpaceToSingleSpace(searchString);

if (searchString.StartsWith(" "))
searchString = searchString.Substring(1);

if (searchString.EndsWith(" "))
searchString = searchString.Substring(0, searchString.Length - 1);

searchString = GetSpaceSplit(searchString);

if (searchString.Equals("\"\""))
searchString = "";

return (searchString);

}

public static string ConvertAllWhiteSpaceToSingleSpace(string searchString)
{
return System.Text.RegularExpressions.Regex.Replace(searchString, @"\s+", " ");
}

public static string GetTextWithinQuotes(int startQuote, int endQuote, string searchString)
{
string fortest = searchString.Substring(startQuote, endQuote);
return fortest;
}

public static string AddQuotes(string searchString)
{
searchString = "\"" + searchString + "\"";
return searchString;
}

public static string GetSpaceSplit(string searchString)
{
string[] splitWithSpace = searchString.Split(' ');
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitWithSpace.Length; i++)
{
sb.Append(AddQuotes(splitWithSpace[i]));

if (i != splitWithSpace.Length - 1)
sb.Append(" and ");
}

return (sb.ToString());
}

public static int CountForQuotes(string searchString)
{
char quotes = '"';
int count = 0;
foreach (char c in searchString)
{
if (c.Equals(quotes))
count++;
}

return count;

}
}

Monday, September 6, 2010

Ajax Tutorial for Beginners: Part 1

My full article for learning Ajax for beginners
http://www.codeproject.com/KB/ajax/AjaxTutorial.aspx

Motivation Part 1 : Lack of Confidence

People cannot stay motivated for all the time because our drive is constantly assaulted by negative thoughts and anxiety about the future. We all faces doubts and depression. We all have lack of motivation somehow and there is no simple soultion for it. The point is how to keep yourself motivated, the key is understanding your thoughts and how they drive your emotions, how to nurture motivating thoughts, neutralize negative ones and focus on the task which is there on your hand.

There are several reason why we loose motivation, three as primary are
1. Lack of confidence : If you dont believe in yourself what the point of trying
2. Lack of focus : If you dont know what you want or what you want to achieve then whats the point of starting or doing anything
3. Lack of direction : If you dont know what to do, how can you be motivated to do it.

Lack of Confidence
The first motivation killer is a lack of confidence. When this happens to me, it’s usually because I’m focusing entirely on what I want and neglecting what I already have. When you only think about what you want, your mind creates explanations for why you aren’t getting it. This creates negative thoughts. Past failures, bad breaks, and personal weaknesses dominate your mind. You become jealous of your competitors and start making excuses for why you can’t succeed. In this state, you tend to make a bad impression, assume the worst about others, and lose self confidence.
The way to get out of this thought pattern is to focus on gratitude. Set aside time to focus on everything positive in your life. Make a mental list of your strengths, past successes, and current advantages. We tend to take our strengths for granted and dwell on our failures. By making an effort to feel grateful, you’ll realize how competent and successful you already are. This will rejuvenate your confidence and get you motivated to build on your current success.
It might sound strange that repeating things you already know can improve your mindset, but it’s amazingly effective. The mind distorts reality to confirm what it wants to believe. The more negatively you think, the more examples your mind will discover to confirm that belief. When you truly believe that you deserve success, your mind will generate ways to achieve it. The best way to bring success to yourself is to genuinely desire to create value for the rest of the world.

Wednesday, July 22, 2009

Ajax with webservice and javascript

[Write this in javascript]

function xmlhttpPost(strURL)
{
// alert(strURL)
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4)
{
alert('hi');
updatepage(self.xmlHttpReq.responseText);
}
}
alert(getquerystring());
self.xmlHttpReq.send(getquerystring());
}

function getquerystring()
{
var form = document.forms['form1'];
var word = form.word.value;
qstr = 'w=' + escape(word); // NOTE: no '?' before querystring
return qstr;
}

function updatepage(str){
alert(str)
document.getElementById("result").innerHTML = str;
}

[Put some controls in HTML page under orm tag]

word:< name="word" type="text">
< value="Go" type="button" onclick="'JavaScript:xmlhttpPost(">
< id="result">< /div>


[ Create a webservice name it as WebService.asmx]

public class WebService : System.Web.Services.WebService {

public WebService () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld(string w) {
return w;
}

}