Advertise here


Tag cloud


Posts Tagged ‘c-sharp’

Stored procedures and obscure errors messages

Posted on Friday, March 16th, 2012 in MySQL

Recently found a extremely simple way to fix a problem in one of our projects at work in MySQL stored procedures. Hopefully saving everyone else the massive amount of time spent adapting the program to work.

What was the problem and how did this error occur. In our C-sharp dot NET project we needed to upgrade the MySQL .Net connector dll from 6.3.x to 6.4.x.

The project built fine but when running the INSERT function it through this nice unrelated error message – “Unable to cast object of type ‘System.DBNull’ to type ‘System.String’“. So from working fine in the previous version to failing in the next with this strange error message. We soon discovered that is was some sort of permissions error. Simply assigning the user all permissions fixed it but the problem with that was this user can not have all permissions! Not the solutions we are looking for.

Anyway we did not find any real solutions or cases from searching the web, so we moved from stored procedures to using the standard insert command. Took a while to change but eventually got working.

We then came across another issue which lead to us altering the connection string in our web.config file. A the end we put check parameter = false by default this is set to true. The changed fixed this small issue but we decided to test it with our original program using stored procedures. This essentially allowed our stored procedures to work. Not exactly sure how it fixed our first issue but it does. According to MySQL it: “Indicates if stored routine parameters should be checked against the server“. This might be allowing the user permissions authenticate against MySQL server rather than in the MySQL .NET Connector or something along them lines.

Conclusion

Use check parameter = false in your MySQL connection string when using stored procedures!

Simples.


Regex link removal script

Posted on Thursday, February 16th, 2012 in Web Development

Need to remove links from your content? This regex patten should do the trick.

First you will need to choose a programming language has regex functionality and returns the properties start position, the length and group values of a string.

Then use a substring method to extract the string that matched the pattern.

Use the replace function to remove the link and use group two from the results of the regex function to put back the link text.

(<a\s*href="http://www\.sitename\.com/[a-zA-Z0-9-\?&;%=/_""\s\.]*">)([a-zA-Z0-9-\?&;%=/_"\s\.]*)(</a>)

Example below is using c#.

if (myMatchs.IsMatch(newContent))
{
      Status = "[Match]";

      try
      {
            int totalMatches = myMatchs.Matches(newContent).Count;
            Match keyMatch;
            string m;

            for (int i = 0; i < totalMatches; i++)
            {
                   keyMatch = myMatchs.Match(newContent);
                   m = newContent.Substring(keyMatch.Index, keyMatch.Length);
                   changes += " , " + m;
                   newContent = newContent.Replace(m, keyMatch.Groups[2].Value);
             }
        }
        catch (Exception ex)
        {
              Status = string.Format("[Fail] - {0}", ex.Message);
        }
}
else
{
        Status = "[No Match]";
}

A good idea would be to output the changes to a text file so you can track the modifications.

If you are replacing links in MySql database, using innodb engine use transactions so you can check the changes before committing them.


Using MVC DropDownListFor Html helper

Posted on Wednesday, January 25th, 2012 in Web Development

You might have been mislead when using DropDownListFor method when you wanted to set the selected item or it is just me. I thought you can use the SelectList function fourth parameter to set the selected value, as it says in the documentation but this does not work because data is binded to the data object.

If the object or property in the model is empty and it used as an int then it will default to zero, so the item with the value zero will be selected.

So naturally all you need to do is set the property in the model to the value you want  and it will select the correct default value in the list.

Example below:

@{
//set the username to be selected
Model.Post.UserId = Model.GetUserId;
}
//Outputs a list of userid
@Html.DropDownListFor(model => model.Post.UserId, new SelectList(Model.Users, "Id", "Name"))

Sounds so simple and straight forward now, but is amazing how something small can distract you from the obvious answer sometimes.


Convert int array to string array

Posted on Saturday, September 10th, 2011 in Top Tips

Quick and useful way to convert an int array to a string.

int[] ints = new[] { 2, 5, 90, 49}
string[] strings = Ids.Select(m => m.ToString()).ToArray();

What is happening? We are moving through each item in the array and converting it into a string. They will all be in a string format and it is then assigned to the string array. The same is done below except it is from a string to an int array. This can be applied to most other array types.

String array to int array


string[] strings = new[] { "1", "2", "3" };
int[] ints = strings.Select(x =&gt; int.Parse(x)).ToArray();


Web Design Essex | Richard Kotze – Web Technology, Design and Development powered by WordPress | Entries (RSS)