Sunday, March 28, 2010

ExecuteReader: Connection property has not been initialized

This is common error if you don't set connection property of the command object.

Suppose that you have code something like this:


SqlConnection con = new SqlConnection(myConnectionString);
SqlCommand cmd = new SqlCommand("SQL STATEMENT");

con.Open();
cmd.ExecuteReader();



well, in this peace of code your command object doesn't know anything about your connection. To fix this create your command object like this:


SqlCommand cmd = new SqlCommand("SQL STATEMENT", con);


or if you use empty constructor or don't set connection property in costructor assign connection as property:

cmd.Connection = con;

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#.

Friday, April 11, 2008

C# DataGridView and Multithreading Problem

I had some problems updating datagridview from another thread. Problem is in painting scrollbars (vertical one). One thing that is interesting is that everything is working fine if you start your program from VisualStudio debugger.

My first solution to this problem was: I've disabled scrollbars and application works fine.

After some time of research I've found solution to my problem!!!

to simplify there is two posibilities:

1. you have datagridview without binded datasource
Solution: use delegate to add rows, or update existing.

2. you have datagridview with binded datasource (for example datatable)
Solution: use delegate to update datatable/datasource,

if you dont now how, you can start from this article.


keys: c# datagridview multithread freeze, scrollbar problem, c# application freeze, program hangup, multithread gui update, datagridview update from another thread, threading, csharp.

Tuesday, March 18, 2008

How to convert string to byte[]

Here is a litle function to convert string to byte array.


public static byte[] StringToByteArray(string inputString)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(inputString);
}

Sunday, March 16, 2008

Deserialization failed: The type initializer for "Microsoft.ReportDesigner.Drawing.Language" threw an exception.

I had this error because I registered custom CultureInfo with same name that already exists in collection.

Above is peace of code that you can use to check if you have the same problem. If this code don't throw an exception, than you have some other problem.



private void test()
{
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
Hashtable hashtable = new Hashtable();
ArrayList m_languages = new ArrayList();
hashtable.Add("", "Default");
m_languages.Add("");
for (int i = 0; i < cultures.Length; i++)
{
CultureInfo info = cultures[i];
if (info.Name != "")
{
hashtable.Add(info.Name, info.DisplayName);
}
}
}

Friday, February 8, 2008

Welcome to the C# Programming Reference

I've created this blog as reference to C# programming tools, articles, and free/open source code.