2
Answers

Column X does not belong to table Y strange error

eddy uk

eddy uk

16y
9.8k
1

Hi all.

 

I have googled for this error and saw some people having it,

but no deccent solution. This problem happens even with static

queries, as no matter what, table MUST be returned. So, i

wanted to track the problem by writing text log, but when i

started to write to text log the problem gone... I removed the

code of keeping the error log, and the problem occured again...

and so on and on for 2 days now. Here is the function with no log,

that fails:

 

Note: this function is a part from class and variables like lastError or date

defined in it.

 

public bool ExecuteManualQuery(string query)

{

                bool flag = true;

                try

                {

                                data = new DataSet();

                                OpenConnection();

                                SqlDataAdapter da = new SqlDataAdapter(query, conn);

                                da.Fill(data);

                                if (data.Tables.Count == 0)

                                {

                                                flag = false;

                                                lastError = Properties.Resources.ERR_NO_TBL;

                                }

                                else if (data.Tables[0].Rows.Count == 0)

                                {

                                                flag = false;

                                                lastError = Properties.Resources.ERR_NO_ROW;

                                }

                }

                catch (Exception ex)

                {

                                lastError = ex.Message;

                                flag = false;

                }

                GC.Collect();

                return flag;

}

 

And here is the same function, with logging that (for some wierd and

strange reason) does work:

 

public bool ExecuteManualQuery(string query)

{

                //******** TESTING BLOCK START*******\\

                System.IO.TextWriter tw = new System.IO.StreamWriter("C:\\test.log", true);

                tw.WriteLine("-----" + DateTime.Now.ToShortTimeString() + "-----");

                tw.WriteLine(DateTime.Now.ToShortTimeString() + " - Query: " + query);

                //******** TESTING BLOCK END *******\\

 

bool flag = true;

                try

                {

                                data = new DataSet();

                                OpenConnection();

                                SqlDataAdapter da = new SqlDataAdapter(query, conn);

                                da.Fill(data);

                                if (data.Tables.Count == 0)

                                {

                                                flag = false;

                                                lastError = Properties.Resources.ERR_NO_TBL;

                                }

                                else if (data.Tables[0].Rows.Count == 0)

                                {

                                                flag = false;

                                                lastError = Properties.Resources.ERR_NO_ROW;

                                }

                }

                catch (Exception ex)

                {

                                lastError = ex.Message;

                                flag = false;

                }

                //******** TESTING BLOCK START*******\\

                tw.Close();

                //******** TESTING BLOCK END *******\\

                GC.Collect();

                return flag;

}

 

Maybe it's function runtime issue? If anyone can help, please do.

 

Thanks in advanced!

 

Answers (2)