Sunday, December 7, 2008

Is Base64String

This is a function to check is the string base64 string or not.




public static bool IsBase64String(string s)
{
if ((s.Length % 4) != 0)
{
return false;
}
try
{
MemoryStream stream = new MemoryStream(Convert.FromBase64String(s));
return true;
}
catch
{
return false;
}
}








keys: c#, base64, crypto,

Thursday, August 21, 2008

How to get C# Assembly GUID

How to get C# Assembly GUID using Reflection




public string AssemblyGuidString(System.Reflection.Assembly assembly)
{
object[] objects =
assembly.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
if (objects.Length > 0)
{
return ((System.Runtime.InteropServices.GuidAttribute)objects[0]).Value;
}
else
{
return String.Empty;
}
}






keys: c#, assembly, reflection, guid, attributes

Wednesday, July 16, 2008

Cross-thread operation not valid: Control 'xxxxx' accessed from a thread other than the thread it was created on.

This exception is thrown when you try to access control from thread other than the thread it was created on.
To access control and control properties use delegate. Its easy. Here is example for accessing Label control (label1) Text property.


delegate void updateLabelTextDelegate(string newText);
private void updateLabelText(string newText)
{
if (label1.InvokeRequired)
{
// this is worker thread
updateLabelTextDelegate del = new updateLabelTextDelegate(updateLabelText);
label1.Invoke(del, new object[] { newText });
}
else
{
// this is UI thread
label1.Text = newText;
}
}


Now whenever you want to change text of label you call method updateLabelText(newText). You can do the same for all other controls.

keys: gui multithreading, update from another thread, threading, cross-thread,

Sunday, June 22, 2008

Set proxy for WebRequest in web.config

Put this in web.config, you can set usesystemdefault but for me that dont work.


<system.net>
<defaultproxy>
<proxy
usesystemdefault="false"
proxyaddress="http://192.168.1.1:3128"
bypassonlocal="true"
</proxy>
</defaultproxy>
</system.net>

Keys: asp.net, webrequest, proxy.

RESTORE cannot process database because it is in use by this session

"System.Data.SqlClient.SqlError: RESTORE cannot process database because it is
in use by this session. It is recommended that the master database be used
when performing this operation."

If you are using Microsoft.SqlServer.SMO set SqlServer.ConnectionContext.DatabaseName to "master".
If you are using TSQL, select master database (USE master).

Keys: error, smo, tsql, c#.