LeadConvertToProject

Yanıt LeadConvertToProject

  • 05 Haziran 2012 Salı 09:05
     
     

    Environment:CRM4

    when i want to convert the opportunity to project,the error is occurred. so i check the event viewer,

    the error which is shown as below:

    Event code: 3005
    Event message: An unhandled exception has occurred.
    Event time: 6/5/2012 4:02:52 PM
    Event time (UTC): 6/5/2012 8:02:52 AM
    Event ID: 8ac18df35d84497b82ebc778908e5633
    Event sequence: 3467
    Event occurrence: 4
    Event detail code: 0

    Application information:
        Application domain: /LM/W3SVC/1/ROOT-1-129833112672968750
        Trust level: Full
        Application Virtual Path: /
        Application Path: c:\inetpub\wwwroot\
        Machine name: XXXXXX

    Process information:
        Process ID: 5012
        Process name: w3wp.exe
        Account name: NT AUTHORITY\NETWORK SERVICE

    Exception information:
        Exception type: InvalidOperationException
        Exception message: The ConnectionString property has not been initialized.

    Request information:
        Request URL: http://XXXX/season/LeadConvertToProject.aspx?id={282125FC-C7AB-E111-9515-00237D225A0A}&type=4&typename=lead&num=0.8116486067832297
        Request path: /season/LeadConvertToProject.aspx
        User host address: 114.94.72.177
        User: {a93c20c2-1620-de11-9a7a-00237d225a08}
        Is authenticated: True
        Authentication Type: CrmPostAuthentication
        Thread account name: NT AUTHORITY\NETWORK SERVICE

    Thread information:
        Thread ID: 1
        Thread account name: NT AUTHORITY\NETWORK SERVICE
        Is impersonating: False
        Stack trace:    at System.Data.SqlClient.SqlConnection.PermissionDemand()
       at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
       at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.SqlClient.SqlConnection.Open()
       at LeadConvertToProject.StatusCode(String recordId)
       at LeadConvertToProject.Page_Load(Object sender, EventArgs e)
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

     Hoping someone can help me on this one that i would appreciate it.

Tüm Yanıtlar

  • 05 Haziran 2012 Salı 09:31
     
     

    What is the code you use to convert a lead to a "project"? Are you doing this directly via SQL code?

    Seems like you forgot to set your connection properties.

    The ConnectionString property has not been initialized. 

  • 06 Haziran 2012 Çarşamba 16:09
     
     

    you mean i did not set the web.config?

  • 06 Haziran 2012 Çarşamba 23:22
     
     

    lavigne,

    Yes. Looks like you haven't set the Connection String to connect to the CRM database.


    Dimaz Pramudya | CSG (Melbourne) | http://www.xrmbits.com | dimaz@xrmbits.com If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

  • 07 Haziran 2012 Perşembe 03:50
     
     

    I have checked it,not this problem.

  • 07 Haziran 2012 Perşembe 03:52
     
     
    Can you paste the code that you have during Page_Load so that we can investigate it further?

    Dimaz Pramudya | CSG (Melbourne) | http://www.xrmbits.com | dimaz@xrmbits.com If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

  • 08 Haziran 2012 Cuma 04:37
     
     

    Here is my code and web.config

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.IO;


    public partial class LeadConvertToProject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {


                int objectTypeCode = int.Parse(this.Request.QueryString["type"]);
                string entityName = this.Request.QueryString["typename"];
                string recordId = this.Request.QueryString["id"];

                if (!StatusCode(recordId).Equals("2"))
                {
                    if (!isLeadEnglishName(recordId))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script>alert('请记录 英文名字 信息!(Please record leads english name!)');window.close();</script>");
                        return;
                    }
                    if (!isLeadAddressDoNotKnow(recordId))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script>alert('请记录 地址 信息!(Please record address information!)');window.close();</script>");
                        return;
                    }
                    if (!isnew_leadssourceDoNotKnow(recordId))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script>alert('请记录 线索来源 信息!(Please record leads source information!)');window.close();</script>");
                        return;
                    }
                    if (!isExistLinkMan(recordId))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script>alert('请输入至少一个联系人信息!(Please record at least one Contact Person!)');window.close();</script>");
                        return;
                    }
                    using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
                    {
                        SqlCommand command = connection.CreateCommand();
                        command.CommandType = CommandType.StoredProcedure;
                        command.CommandText = "Frensworkz_LeadConvertToProject";
                        command.Parameters.Add(new SqlParameter("@recordId", recordId));
                        connection.Open();
                        int result = command.ExecuteNonQuery();
                        int statuCode = UpdateStatCode(recordId);
                        if (result > 0 && statuCode > 0)
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script>alert('转换成功!(Convert successfully!)');window.close();var args=window.dialogArguments;args.location.reload();args.close()</script>");
                            result = 0;
                        }
                    }
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "<script>alert('该线索已经被转换成项目!(The lead has been convert to project!)');window.close();</script>");
                }
            }
            catch (Exception ex)
            {
                AppendToFile(ex.Message);
                throw ex;
            }
        }

        static void AppendToFile(string ex)
        {
            StreamWriter SW;
            SW = File.AppendText("C://Inetpub//wwwroot//ISV//log.txt");
            SW.WriteLine(ex);
            SW.Close();
            Console.WriteLine("");
        } 


        /// <summary>
        /// 返回当前项目的状态
        /// </summary>
        /// <param name="recordId"></param>
        /// <returns></returns>
        public string StatusCode(string recordId)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            conn.Open();
            SqlCommand cmd = new SqlCommand("select statecode from Frensworkz_lead where leadid='" + recordId + "'", conn);
            string StatusCode = cmd.ExecuteScalar().ToString();
            conn.Close();

            return StatusCode;
        }

        /// <summary>
        /// 更新lead中statecode的状态
        /// </summary>
        /// <param name="recordId"></param>
        /// <returns></returns>
        public int UpdateStatCode(string recordId)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            conn.Open();
            SqlCommand cmd = new SqlCommand("update leadbase set statecode='2' where leadid='" + recordId + "'", conn);
            int StatusCode = cmd.ExecuteNonQuery();
            conn.Close();

            return StatusCode;
        }
        private bool isExistLinkMan(string id)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
            {
                connection.Open();
                string selectString = "select count(*) from Frensworkz_New_contact where new_lead_new_contact='" + id + "' and  DeletionStateCode=0 and StateCode=0";
                SqlCommand cmd = new SqlCommand(selectString, connection);
                Object obj = cmd.ExecuteScalar();
                if (obj != null)
                {
                    int result = int.Parse(obj.ToString());
                    if (result > 0)
                    {
                        return true;
                    }
                }
                connection.Close();
            }
            return false;
        }
        private bool isLeadEnglishName(string id)
        {
            bool isUpload = false;
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
            {
                connection.Open();
                string selectString = "select new_en from Frensworkz_Lead where leadid='" + id + "' and  DeletionStateCode=0 and StateCode=0";
                SqlCommand cmd = new SqlCommand(selectString, connection);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    string sf_isuploadfile = reader["new_en"].ToString();
                    if (sf_isuploadfile != null && sf_isuploadfile != "")
                    {
                        if (sf_isuploadfile == "4")
                        {
                            isUpload = false;
                        }
                        else
                        {
                            isUpload = true;
                        }
                    }
                    else
                    {
                        isUpload = false;
                    }
                }
                connection.Close();
            }
            if (isUpload == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private bool isLeadAddressDoNotKnow(string id)
        {

            bool isUpload = false;
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
            {
                connection.Open();
                string selectString = "select address1_line1 from Frensworkz_Lead where leadid='" + id + "' and  DeletionStateCode=0 and StateCode=0";
                SqlCommand cmd = new SqlCommand(selectString, connection);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    string sf_isuploadfile = reader["address1_line1"].ToString();
                    if (sf_isuploadfile != null && sf_isuploadfile != "")
                    {
                        isUpload = true;
                    }
                }
                connection.Close();
            }
            if (isUpload == true)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        private bool isnew_leadssourceDoNotKnow(string id)
        {
            bool isUpload = false;
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
            {
                connection.Open();
                string selectString = "select new_leads_source from Frensworkz_Lead where leadid='" + id + "' and  DeletionStateCode=0 and StateCode=0";
                SqlCommand cmd = new SqlCommand(selectString, connection);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    string sf_isuploadfile = reader["new_leads_source"].ToString();
                    if (sf_isuploadfile != null && sf_isuploadfile != "")
                    {
                        isUpload = true;
                    }
                }
                connection.Close();
            }
            if (isUpload == true)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

    }

    web.config

    <?xml version="1.0"?>

    <configuration>

     <appSettings>
      <add key="CrmHost" value="http://192.168.0.11"/>
      <add key="OrganizationName" value="XXXXX"/>
      <add key="WebServicePort" value="80"/>
      <!--<add key="ConnectionString" value="server=localhost;Initial Catalog=XXXX_MSCRM;Integrated Security=True"/>-->
      <add key="ConnectionString" value="Data Source=.;Initial Catalog=XXXX_MSCRM;User ID=sa;Password=Pa$$w0rd" />
      <connectionStrings/>

     </appSettings>

     
        <system.web>
            <!--
                Set compilation debug="true" to insert debugging
                symbols into the compiled page. Because this
                affects performance, set this value to true only
                during development.
            -->
            <compilation debug="false">

            </compilation>
            <!--
                The <authentication> section enables configuration
                of the security authentication mode used by
                ASP.NET to identify an incoming user.
            -->
            <authentication mode="Windows" />
            <!--
                The <customErrors> section enables configuration
                of what to do if/when an unhandled error occurs
                during the execution of a request. Specifically,
                it enables developers to configure html error pages
                to be displayed in place of a error stack trace.

            <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
                <error statusCode="403" redirect="NoAccess.htm" />
                <error statusCode="404" redirect="FileNotFound.htm" />
            </customErrors>
            -->

        </system.web>

    </configuration>

  • 08 Haziran 2012 Cuma 11:36
     
     

    Can you connect to that database using the credentials User ID=sa;Password=Pa$$w0rd" using SQL Server Management studio?
    You could try to specify the complete name of your DB server as DataSource

    Try debugging that page and looking at how your sqlconnection is created

  • 08 Haziran 2012 Cuma 16:50
     
     

    Yeah.i can do that with the userID and Password.

    still not work that i use the complete name of DB server as DataSource.i find a strange problem that i've created a "test.aspx" and a "test.aspx.cs" in ISV which also can not be accessed when i type the url "http://XXXXX/ISV/test.aspx" in IE.The error page shows as follows:

    "Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine."

    maybe it's not the problem of connection with the database

  • 10 Haziran 2012 Pazar 16:01
     
     
    i copied the code  into my VPC and the it worked.so i can definitely make sure that authority is the key of the problem
  • 11 Haziran 2012 Pazartesi 05:17
     
     Yanıt

    From the code it appears you are making direct SQL updates into the Database

      SqlCommand cmd = new SqlCommand("update leadbase set statecode='2' where leadid='" + recordId + "'", conn);
            int StatusCode = cmd.ExecuteNonQuery();

    Also the connection string, you are connecting to the database using uid/pwd... You are directly accessing the Tables and not the FilteredViews.

    If you want to make any updates to the CRM records, you should strictly use the CRM Services that have been made available for just this purpose. The CRM services implement business logic and is the ONLY supported way to performed updates to CRM records.

    CRM databases use Integrated Security and the Filtered Views implement the Security roles permissions and returns only the records the user has access to.

    Even if you are able to fix the connection issue, it would be strictly advisable to convert this code to use CRM services made available. You can download the CRM SDK that has samples to get this done.

    HTH

    Sam


    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"