Thursday 27 December 2012

search button in asp with crm data

protected void btnsearch_Click(object sender, EventArgs e)
        {
            string searchkeyword = TextBox1.Text;
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("Order No");
            dt.Columns.Add("Customer Name");
            dt.Columns.Add("Product Name");
            //dt.Columns.Add("Product Category");
            dt.Columns.Add("Product Price");
            dt.Columns.Add("Order Status");
            dt.Columns.Add("Shipped to City");
            dt.Columns.Add("SKU");
            dt.Columns.Add("Offer Price");
            dt.Columns.Add("Payable Price");
            dt.Columns.Add("Order Id");

            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            Uri OrganizationUri = new Uri("http://smdev-crm/SulekhaCRM/XRMServices/2011/Organization.svc");
            Uri HomeRealmUri = null;
            using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, credentials, null))
            {
                Microsoft.Xrm.Sdk.IOrganizationService service = (Microsoft.Xrm.Sdk.IOrganizationService)serviceProxy;


                string fetchquery = string.Empty;

                if (ddlsearch.SelectedItem.Value == "cun")
                {
                    fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorder'>" +
"<attribute name='customerid' />" +
"<attribute name='name' />" +
"<attribute name='new_actualpurchaseprice' />" +
"<attribute name='new_venderorderno' />" +
"<attribute name='new_venderorderstatus' />" +
"<attribute name='new_couponcode' />" +
"<attribute name='shipto_city' />" +
"<attribute name='shipto_telephone' />" +
"<attribute name='billto_telephone' />" +
"<attribute name='createdon' />" +
"<filter type='and'>" +
 "<condition attribute='customeridname' operator='like' value='" + searchkeyword + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";

                }

                if (ddlsearch.SelectedItem.Value == "von")
                {

                    fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorder'>" +
"<attribute name='customerid' />" +
"<attribute name='name' />" +
"<attribute name='new_actualpurchaseprice' />" +
"<attribute name='new_venderorderno' />" +
"<attribute name='new_venderorderstatus' />" +
"<attribute name='new_couponcode' />" +
"<attribute name='shipto_city' />" +
"<attribute name='shipto_telephone' />" +
"<attribute name='billto_telephone' />" +
"<attribute name='createdon' />" +
"<filter type='and'>" +
"<condition attribute='new_venderorderno' operator='like' value='" + searchkeyword + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";


                }
                if (ddlsearch.SelectedItem.Value == "son")
                {
                    fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorder'>" +
"<attribute name='customerid' />" +
"<attribute name='name' />" +
"<attribute name='new_actualpurchaseprice' />" +
"<attribute name='new_venderorderno' />" +
"<attribute name='new_venderorderstatus' />" +
"<attribute name='new_couponcode' />" +
"<attribute name='shipto_city' />" +
"<attribute name='shipto_telephone' />" +
"<attribute name='billto_telephone' />" +
"<attribute name='createdon' />" +
"<filter type='and'>" +
 "<condition attribute='new_couponcode' operator='like' value='" + searchkeyword + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";

                }
                if (ddlsearch.SelectedItem.Value == "smn")
                {
                    fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorder'>" +
"<attribute name='customerid' />" +
"<attribute name='name' />" +
"<attribute name='new_actualpurchaseprice' />" +
"<attribute name='new_venderorderno' />" +
"<attribute name='new_venderorderstatus' />" +
"<attribute name='new_couponcode' />" +
"<attribute name='shipto_city' />" +
"<attribute name='shipto_telephone' />" +
"<attribute name='billto_telephone' />" +
"<attribute name='createdon' />" +
"<filter type='and'>" +
"<condition attribute='shipto_telephone' operator='like' value='" + searchkeyword + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
                }
                if (ddlsearch.SelectedItem.Value == "bmn")
                {
                    fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorder'>" +
"<attribute name='customerid' />" +
"<attribute name='name' />" +
"<attribute name='new_actualpurchaseprice' />" +
"<attribute name='new_venderorderno' />" +
"<attribute name='new_venderorderstatus' />" +
"<attribute name='new_couponcode' />" +
"<attribute name='shipto_city' />" +
"<attribute name='shipto_telephone' />" +
"<attribute name='billto_telephone' />" +
"<attribute name='createdon' />" +
"<filter type='and'>" +
"<condition attribute='billto_telephone' operator='like' value='" + searchkeyword + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
                }

                RetrieveMultipleRequest req = new RetrieveMultipleRequest();
                FetchExpression fetch = new FetchExpression(fetchquery);
                req.Query = fetch;
                RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);
                EntityCollection col = resp.EntityCollection;
                DataRow dr = dt.NewRow();

                string orderstate = null;
                foreach (var c in col.Entities)
                {


                    dr["Order No"] = c["new_couponcode"];

                    EntityReference customer = (EntityReference)c.Attributes["customerid"];
                    dr["Customer Name"] = customer.Name;

                    dr["Product Name"] = c["name"];
                    dr["Product Price"] = c["new_actualpurchaseprice"];

                    orderstate = c["new_venderorderstatus"].ToString();
                    dr["Order Status"] = orderstate; //c["new_venderorderstatus"].ToString();
                    dr["Shipped to City"] = c["shipto_city"];
                    Guid orderguid = (Guid)c.Attributes["salesorderid"];

                    dr["Order Id"] = orderguid;

                    string orderquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='salesorderdetail'>" +
"<attribute name='new_skuid' />" +
"<attribute name='new_ip_offerprice' />" +
"<attribute name='new_ip_payable' />" +

"<attribute name='createdon' />" +
"<filter type='and'>" +
 "<condition attribute='salesorderid' operator='eq' uitype='salesorder' value='" + orderguid + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";

                    FetchExpression fetch1 = new FetchExpression(orderquery);
                    EntityCollection salesorderinfo = serviceProxy.RetrieveMultiple(fetch1);

                    foreach (Entity sorder in salesorderinfo.Entities)
                    {

                        dr["SKU"] = sorder.Attributes["new_skuid"].ToString();
                        if (sorder.Attributes.Keys.Contains("new_ip_offerprice") == true)
                        {
                            //dr["Offer Price"] = (Money)sorder.Attributes["new_ip_offerprice"];

                            Money op = (Money)sorder.Attributes["new_ip_offerprice"];
                            dr["Offer Price"] = op.Value;
                        }
                        else
                        {
                            dr["Offer Price"] = "0"; //new Money { Value = 0 };
                        }

                        if (sorder.Attributes.Keys.Contains("new_ip_payable") == true)
                        {
                            // dr["Payable Price"] = (Money)sorder.Attributes["new_ip_payable"];

                            Money pp = (Money)sorder.Attributes["new_ip_payable"];
                            dr["Payable Price"] = pp.Value;

                        }
                        else
                        {
                            dr["Payable Price"] = "0"; //new Money { Value = 0 };
                        }


                    }



                }

                dt.Rows.Add(dr);

                ds.Tables.Add(dt);

                if (orderstate == "Order Approved")
                {
                    orderapproved.DataSource = ds;
                    orderapproved.DataBind();
                    TabPanel1.Visible = false;
                    TabPanel2.Visible = false;
                    TabPanel3.Visible = false;
                  
                }
                if (orderstate == "Accepted")
                {
                    orderaccepted.DataSource = ds;
                    orderaccepted.DataBind();
                    tbpnluser.Visible = false;
                    TabPanel1.Visible = false;
                    TabPanel3.Visible = false;

                }
                if (orderstate == "Shipped")
                {

                    ordershipped.DataSource = ds;
                    ordershipped.DataBind();

                    tbpnluser.Visible = false;
                    TabPanel2.Visible = false;
                    TabPanel3.Visible = false;

                }
                if (orderstate == "Delivered")
                {
                    orderdelivered.DataSource = ds;
                    orderdelivered.DataBind();
                    tbpnluser.Visible = false;
                    TabPanel2.Visible = false;
                    TabPanel1.Visible = false;

                }

                if (orderstate != "Order Approved" && orderstate != "Accept" && orderstate != "Shipped" && orderstate != "Delivered")
                {
                    orderapproved.DataSource = ds;
                    orderapproved.DataBind();
                    TabPanel1.Visible = false;
                    TabPanel2.Visible = false;
                    TabPanel3.Visible = false;
                }


            }
        }

     

No comments:

Post a Comment