Thursday, February 9, 2012

XML Value generation and convertions


  private int GetAvailableBids(string referralScheme, decimal moneyLevel)
    {
        int freeBids=0;
        DataSet dsScheme = new DataSet();
        XmlDocument XmlDocumentObject = new XmlDocument();
        string MembershipFile = string.Empty;
        //SubscriptionFileName = Server.MapPath(ConfigurationManager.AppSettings["ImageFolder"].ToString()) + "Subscription.xml";
        MembershipFile = HttpContext.Current.Server.MapPath("~\\App_Data\\Files\\MembershipSheme.xml");
        XmlDocumentObject.Load(MembershipFile);

        XmlNodeList Nodelist = XmlDocumentObject.SelectNodes("/Membership/SignupBids/" + referralScheme);
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

        string strXML = "<MembershipSheme>";
        strXML = strXML + Nodelist.Item(0).InnerXml;
        strXML = strXML + "</MembershipSheme>";
        doc.LoadXml(strXML);
        dsScheme.ReadXml(new System.IO.StringReader(doc.OuterXml));

        if (dsScheme != null)
        {
            if (dsScheme.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dsScheme.Tables[0].Rows)
                {
                   decimal moneyLevelxml = Convert.ToDecimal(dr["price"]);
                   if (moneyLevel == moneyLevelxml)
                   {
                       freeBids = Convert.ToInt32(dr["value"]);
                   }

                }
            }
        }

        if (referralScheme.ToUpper() == "RESIDUAL")
        {
            freeBids = freeBids / 12;
        }
        return (freeBids);
    }

Core Ajax Code To Get Value From Other Page Load Event



<script type = "text/javascript" language="javascript">
var RedirectPath = '<%= "http://" + Request.ServerVariables["HTTP_HOST"].ToString() + Request.ApplicationPath+"/Ajax/checkuser.aspx" %>';
        function GetName(source, args) {
     
        var current_path = window.location;
        if(current_path.toString().substr(0,5) == "https")
        {
           RedirectPath = '<%= "https://" + Request.ServerVariables["HTTP_HOST"].ToString() + Request.ApplicationPath+"/Ajax/checkuser.aspx" %>';      
        }

          hdnrefuserexit =  document.getElementById("<%=hdnrefuserexit.ClientID %>");
          txtReferredBy = document.getElementById("<%=txtReferralID.ClientID %>");
          lableMessage = document.getElementById("<%=lblError1.ClientID %>");
           referralID = txtReferredBy.value;
         
            if(referralID!="")
            {
           
                $.ajax({
                    type: "POST",
                    url: RedirectPath,
                    data: "q=" + referralID+"&t=referral" ,
                    success: function(response) {
                               
                        if(response !="failed")
                        {
                          if(response=="false")
                          {
                           
                             lableMessage.innerHTML  = "Referral User does not exist";
                             lableMessage.style.color = "Red";
                             hdnrefuserexit.value = "false";
                             txtReferredBy.focus();
                             args.IsValid = false;
                          }
                          else
                          {
                             lableMessage.innerHTML  = response.toString();
                             lableMessage.style.color = "Green";      
                             hdnrefuserexit.value = "true";                
                             args.IsValid = true;
                          }
                        }
                        else if(response =="failed")
                        {          
                       
                          lableMessage.innerHTML  = "Referral User does not exist";
                          lableMessage.style.color = "Red";
                          txtReferredBy.focus();
                          hdnrefuserexit.value = "false";
                          args.IsValid= false;
                        }
                        else
                        {                        
                          lableMessage.innerHTML  = "Referral User does not exist";
                          lableMessage.style.color = "Red";
                          txtReferredBy.focus();
                          hdnrefuserexit.value = "false";
                          args.IsValid = false;
                        }                
                    }
                });
              }
              else
              {
                hdnrefuserexit.value="true;"
                lableMessage.innerHTML  = "";
                args.IsValid = true;
              }
         
        }

<script type/>




<table>

<tr id="trReferral" runat="server" style="visibility: hidden;">
                                                                                <td align="right" valign="top" class="input_label_right">
                                                                                    Referee UserID
                                                                                </td>
                                                                                <td align="left" valign="top">
                                                                                    <asp:TextBox ID="txtReferralID" CssClass="input_value" ValidationGroup="step1" runat="server" />
                                                                                    &nbsp;
                                                                                    <asp:CustomValidator ID="CustomValidator2" ValidationGroup="step1" ControlToValidate="txtReferralID"
                                                                                        SetFocusOnError="true" ClientValidationFunction="GetName" runat="server" Display="Dynamic"
                                                                                        Visible="true" ErrorMessage="Referral User does not exist" />
                                                                                    <asp:Label ID="lblError1" runat="server" Text="" ForeColor="Green" Visible="true"></asp:Label>
                                                                                </td>
                                                                            </tr>
</table>




Anoth Page  checkuser.aspx in Ajax folder 
code in this file below.



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataObjects.General;

public partial class Ajax_checkuser : System.Web.UI.Page
{

    #region Page Members
    string SearchUserName = string.Empty;
    #endregion

    #region  Events

    /// <summary>
    /// Page Load Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        string SearchKey = Convert.ToString(Request.Form["q"]);
        if (!string.IsNullOrEmpty(SearchKey))
        {

            string searchType = Convert.ToString(Request.Form["t"]);
            if (!string.IsNullOrEmpty(searchType))
            {
                if (searchType == "referral")
                {
                  GetUserName(SearchKey);
                }
                else if (searchType == "registration")
                {
                    CheckUsersExists(SearchKey);
                }    
            }
        }
    }



    #endregion

    #region Methods


    /// <summary>
    /// Get Provider Name by Referral ID Asyncronusly
    /// </summary>
    private void GetUserName(string SearchKey)
    {
        try
        {
            DataTable dt = PaperTab.BusinessObjects.User.UsersBo.GetUserByUserName(SearchKey);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string firstName = Convert.ToString(dt.Rows[0]["FirstName"]);
                    string lastName = Convert.ToString(dt.Rows[0]["LastName"]);
                    SearchUserName = firstName + " " + lastName;

                    Response.Write(SearchUserName);
                }
                else
                {
                    Response.Write("false");
                }
            }
            else
            {
                Response.Write("failed");
            }
        }
        catch (Exception ex)
        {
            SearchUserName = string.Empty;
            Response.Write("failed");
        }
        finally
        {
         
        }

    }


    /// <summary>
    ///
    /// </summary>
    /// <param name="SearchKey"></param>
    private void CheckUsersExists(string SearchKey)
    {
        try
        {
            DataTable dt = PaperTab.BusinessObjects.User.UsersBo.GetUserByUserName(SearchKey);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    Response.Write("true");
                }
                else
                {
                    Response.Write("false");
                }
            }
            else
            {
                Response.Write("failed");
            }
        }
        catch (Exception ex)
        {
            SearchUserName = string.Empty;
            Response.Write("failed");
        }
        finally
        {
         
        }
    }
    #endregion


}



SQL Optimization

  SQL Optimization  1. Add where on your query  2. If you remove some data after the data return then remove the remove condition in the sel...