Technical Insights: Azure, .NET, Dynamics 365 & EV Charging Architecture

Category: ASP.NET

Category for ASP.NET

App_GlobalResources maps to a directory outside this application, which is not supported

I got this error when i switched the default web server in visual studio to use IIS instead of visual studio integrated web server. To fix this error is by changing the IIS Default Web Site Properties in (Home Directory -> Local Path). Remove any “\” character at the end of the directory. IIS will automatically add one which causing double “//” at then end and it causes problem if you manually add one before. You can check Default Web Site Properties -> ASP.NET -> File Location for actual setting information

407 Proxy authentication required

I found this problem when i try to do any of httpweb request behind proxy and i got error of “407 proxy authentication required”. Normally your company has its own proxy server and sometimes you want to call webservice or reading XML from any website or sending xml to payment gateway.

The workaround this is to use proxy properties of your httpwebrequest variable. this is some snippet

         Dim objRequest As HttpWebRequest
         objSendXML = New XmlDocument
         objRequest = WebRequest.Create(strPaymentURL)
         objRequest.Method = "POST"
         objRequest.ContentLength = strSend.Length
         objRequest.ContentType = "text/xml"

        'for development only!!! have to be blocked on production
         objRequest.Proxy = New System.Net.WebProxy("http://proxy-syd:8080", True)
         objRequest.Proxy.Credentials = CredentialCache.DefaultCredentials

Now you can call your webservice or reading xml behind the proxy server

not valid Base64 character when doing redirect with query string

I was having a problem in my checkout page, the problem that i had was i’ve got this memberID from the checkout page and it’s being passed to another page via query string. This problem doesn’t happen everytime and it happens rarely, this is what i hate the most!!. After spending a couple of hours, i found the problem is in the query string it self.

when you pass something like this in query string

“c+ckDQiiilyW4r7moRA8oQ==”

and it will be automatically converted becoming

“c ckDQiiilyW4r7moRA8oQ==”

and when you try to decrypt it, it will throw the exception of “not valid Base64 character”

“+” when you pass it to url it will be automatically converted to ” “(blank space)

The work around of this problem is

on the target page, you can do some string replace of ” “(blank space) with “+” , and voila it works!!!!

Dim strQuery As String = Request.QueryString("OrderID")
strQuery = strQuery.Replace(" ", "+")

Failed to enable constraints, one or more rows contain values violating non null, unique or foreign-key constraints

I found this error on my project. Well i spent around one hour to figure out this problem. People might think that this is some silly error message.

The error message i got is “Failed to enable constraints, one or more rows contain values violating non null, unique or foreign-key constraints”.

this is caused by my stored procedure which is

SELECT e.eventid,e.event,e.eventdate,i.email,u.username
,u.firstname,u.surname,i.senttime,i.readtime,i.respond
FROM invitefriends i
inner join users u ON i.franchiseeid=u.userid
inner join events e ON i.eventid = e.eventid
WHERE i.franchiseeid is not null
    and ( (@EventID IS NULL) or (e.eventid=@EventID) )
ORDER BY e.eventdate DESC
GO

Since the query is returning multiple rows with the same eventid and the primary key in my datatable is eventid then it caused the error.

There are two workaround to this problem:

  • by using identity from your own table or you can generate it from your query and you need to regenerate your datatable and make sure check the primary key in datatable since it’s not automatically changed for you.
  • You can also relax the constraint by removing the Primary key on the DataTable
  • Register Javascript to hide Div

    Sometimes people add javascript into the aspx page instead of using code behind but there is a case when we need to add javascript in the runtime using code behind. This is the example where javascript is used to provide switching of visibility based on the table visibility status. I used this technique to hide another user control when user click expand button. Client ID is required to know the real ID of the user control after rendering. In here we also add onclick event to the image tag.

    if (!this.Page.IsStartupScriptRegistered("tableScript"))
    {
    string script = @"function checkVisibility() "
    + @"{"
    + @"    var element = document.getElementById('" + this.OuterTableDetailSearch.ClientID + @"');"
    + @"    var elementBtn = document.getElementById('" + this.btnExp.ClientID + @"');"
    + @"    if (element.style['visibility'] == 'hidden')"
    + @"{"
    + @"element.style['visibility'] = 'visible';"
    + @"elementBtn.src = '/Images/button_expand.gif';"
    + @"}"
    + @"else"
    + @"{"
    + @"element.style['visibility'] = 'hidden';"
    + @"elementBtn.src = '/Images/image_casestudy_search.gif';"
    + @"}"
    + @"}";
    
    this.Page.RegisterStartupScript("tableScript", script);
    }
    
    btnExp.Attributes.Add("OnClick",
                   "javascript:checkVisibility()");

    Row filter in Dataset

    Here it will be useful for someone who would like to filter a particular record in datagrid. You need to create a dataview and filter it. Here, i would like to filter a particular record where the field name is “metakey” and i don’t want to show it when its value is “thumbnail”. So a record where metakey is thumbnail will not be shown at all. It’s a simple thing but it will be useful.

    DataSet metaData = new DataSet();
    dv = new DataView(metaData.Tables[0]);
    
    gridMeta.DataSource = dv;
    gridMeta.DataBind();
    
    dv.RowFilter = ("metakey  'thumbnail'");
    //bind it to datagrid
    

    Using wild card to look up in a string C#

    This is the class that can be used to look up a keyword from a string. This class is inherited from Regex class.
    Base Class

    public class ProductLookupRegex : Regex
    {
    public ProductLookupRegex()
    {
    //
    // TODO: Add constructor logic here
    //
    }
    ///
    /// Initializes a wildcard with the given search pattern.
    ///
    ///
    The wildcard pattern to match.
    public ProductLookupRegex(string pattern) : base(WildcardToRegex(pattern))
    {
    }
    
    ///
    /// Initializes a wildcard with the given search pattern and options.
    ///
    ///
    The wildcard pattern to match.
    ///
    A combination of one or more
    /// .
    public ProductLookupRegex(string pattern, RegexOptions options): base(WildcardToRegex(pattern), options)
    {
    }
    
    ///
    /// Converts a wildcard to a regex.
    ///
    ///
    The wildcard pattern to convert.
    /// A regex equivalent of the given wildcard.
    public static string WildcardToRegex(string pattern)
    {
    return "^" + Regex.Escape(pattern).
    Replace("\\*", ".*").
    Replace("\\?", ".") + "$";
    }
    }

    How to use it

    ProductLookupRegex productlookup =
                     new ProductLookupRegex(productName,
                                             System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    
    foreach (DictionaryEntry de in prodList)
    {
    
    if (productlookup.IsMatch(de.Key.ToString().Trim()))
    {
    productLink = LinkGenerator(de.Value.ToString());
    productName = de.Key.ToString();
    break; // (sn)
    }
    }

    Page 7 of 7

    Powered by WordPress & Theme by Anders Norén