Showing posts with label Errors. Show all posts
Showing posts with label Errors. Show all posts

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;

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

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.

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);
}
}
}