How to solve "Restore failed for Server '...\SQLEXPRESS'. (Microsoft.SqlServer.Express.Smo)"

When trying to restore a database backup you my across the following error "Restore failed for Server '...\SQLEXPRESS'. (Microsoft.SqlServer.Express.Smo)". There are 4 ways of solving this problem. Check the one that is working for you.

1) Change the sql service to run as a local(not as a NetworkService). You can do that in services in the administrative tools.
2) Check the "Overwrite the existing database" in Option section.
3) Add new admin user with privileges to write in mssql folder.
4) If nothing solves the problem try selecting new paths for the generated mdf and ldf files of the restored DB.

How to get the data "under" the selected row in datagriedview binded to datatable

All that you must do is to create an instance of CurrencyManager class. This class primary work is to manage list of binding objects. So to get the data under the selected row we must supply the DataSource and the DataMember of the DataGridView. DataMember represents the current list or the current table that are displayed. By supplying:


// ...
BindingContext[dgvDataView.DataSource, dgvDataView.DataMember];
// and casting to CurrencyManager
// ...


we create an instance of class CurrencyManager which derives from the abstract class BindingManagerBase.

By supplying



//...
currManager.Current
//...



we get the current item in a object type. The only thing that we have to do is to cast this object to the appropriate type. In our case DataViewRow. So here is a method returning currently selected row in DataGridView.



public static DataRow GetCurrentRow(DataGridView dgvDataView)
{
CurrencyManager currManager =
(CurrencyManager)dgvDataView.BindingContext[dgvDataView.DataSource, dgvDataView.DataMember];
DataRowView drView = (DataRowView)currManager.Current;

return drView.Row;
}

Working with IMAP, POP3, SMTP and .NET

Yesterday i had to make a little app which functionality was to download file attachments. The idea was simple. Find new mails, search for specific files, download them, mark letters as read, reconnect after some period of time. With current possibilities of .net you have to write your own client, with your own MIME parser... Of course to reinvent the wheel it's not very smart. What I found as third party library was . Very nice library with many features. Library is not free. The unregistered version is fully functional but puts sometimes on random mails' subject "Please purchase the license". If you are developing a serious project whit this kind of functionality this library is for you.

(Insist to say that my opinion is based only on my user experience and it's not an add)

Some sample code from the web-site:
All you need is to reference Mail.dll.



using Lesnikowski.Client.IMAP;
using Lesnikowski.Mail;
using Lesnikowski.Mail.Headers;
using Lesnikowski.Mail.Headers.Constants;
using System;

class Program
{
static void Main(string[] args)
{
Imap imap = new Imap();
imap.Connect("mail.host.com");

imap.User = "lesnikowski";
imap.Password = "password";
imap.Login();

imap.SelectInbox();
List uids = imap.SearchFlag(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
ISimpleMailMessage email = new SimpleMailMessageBuilder()
.CreateFromEml(eml);
Console.WriteLine(email.Subject);
Console.WriteLine(email.TextDataString);
}
imap.Close(true);
}
};




Easy and nice, right? :)

Sorting custom objects implementing IComparable

Today a friend of mine asked me how to sort a list collection of custom object. The answer is very simple. All you need to do is to implement IComparable interface and it's method CompareTo. Lets see a little code:

In our scenario we have a class that represents some doc. It has only 2 property members. We gonna use docID for sorting criteria ;).



public int docID { get; set; }
public string SomeImportantDocText { get; set; }



The class has one method CompareTo (implemented from IComparable)



public int CompareTo(object obj)
{
ImportantDoc imd = (ImportantDoc)obj;

return docID.CompareTo(imd.docID);

}



Here comes the whole class declaration:



public class ImportantDoc : IComparable
{

public int docID { get; set; }
public string SomeImportantDocText { get; set; }

public int CompareTo(object obj)
{
ImportantDoc imd = (ImportantDoc)obj;

return docID.CompareTo(imd.docID);

}
}


Now if we have a unsorted collection of List



List docs = new List();

///adding some unsorted docs





Calling


docs.Sort()


will sort our collection.

If you want some different business logic, or sorting on two fields and so on... you need to create your own Comparer by declaring class and implementing IComparer interface.

.NET regex and Cyrillic (unicode)

Some days ago I had to validate a Cyrillic text using regex class. Of course there is no way to do it using “[а-зА-З]{3}” (for example) . Good thing is that .net supports Unicode in it’s regex engine.
Let’s try to match that string “абв” in Cyrillic. To be able to do that, we have to know the Unicode mapping for each letter in the validated string. For the current example Unicode mapping is:

а - 0430
б - 0431
в – 0432

To create a regex pattern for Unicode validation is required to add \u to the pattern. So to match “а” we need that pattern \u0430. To match the whole string the pattern will be (obviously) \u0430\u0431\u0432
Let see:



Match match = Regex.Match("абв", "\u0430\u0431\u0432");

if(match.Success)
{
Console.WriteLine("We did it :D");
}



Output:
We did it :D

Of course using unicode in regex It’s not only limited to single characters. You can use ranges and so on…
In my first post i just wanna say hello :). Hope you find something useful in my blog and hope that this blog will help me find new friendships with people with same interests.
top