webby68
webby68

Reputation: 421

"Column" does not belong to table

Below is my code for this method. I want to process the Active column in the database I am using but I get this error that "Active" does not belong to table. It is clearly in the table and I have it defined in the RestartData.cs with the properties. In the database I have it set as a bit rather than a boolean. I'm wondering if that may be the problem. 1 is for True and 0 is for False. I've researched this through Google and can't really find anything on point. Any help would be greatly appreciated.

static List<RestartData> AppRestartList()
        {
            List<RestartData> restartDatas = new List<RestartData>();

            RestartData restartData = null;

            string sqlQuery = "Select RestartTime, ProgramLocation, LastRestartTime, RestartInterval, ProgramServer, RestartIfRunning, ProcessName from dbo.anyDatabase where active=1;";

            //execute sql query
            //execute database reader
            SqlCommand command = new SqlCommand(sqlQuery, (SqlConnection)DB.MakeConnection("any_database"));
            DataTable dt;  

            command.Connection.Open();

            dt = DB.ExecuteTable(command, "any_database");

            foreach (DataRow row in dt.Rows)
            {
                //move stuff from reader into log data, and exp
                //not RestartData restartData = new RestartData();  The RestartData() method is already declared
                restartData = new RestartData();
                restartData.ProgramLocation = Misc.NullSafeString(row["programLocation"]);
                restartData.Active = Misc.NullSafeBool(row["active"]);
                restartData.RestarInterval = Misc.NullSafeInt(row["restartInterval"]);
                restartData.RestartIfRunning = Misc.NullSafeBool(row["restartIfRunning"]);
                restartData.ProcessName = Misc.NullSafeString(row["processName"]);
                restartData.LastRestartTime = Misc.NullSafeDateTime(row["lastRestartTime"]);

                //add restartData to list
                restartDatas.Add(restartData);

            }
            return restartDatas;
        }

Upvotes: 0

Views: 4455

Answers (2)

Sachin
Sachin

Reputation: 40990

It is because you are not selecting active column from your query.

string sqlQuery = "Select RestartTime, ProgramLocation, LastRestartTime, RestartInterval, ProgramServer, RestartIfRunning, ProcessName from dbo.anyDatabase where active=1;";

above query turned into a datatable which will contains the column you are selecting using select statement.

Now you are using this datatable and trying to access the active column which is not even present in that datatable.

So this statement will cause an error

restartData.Active = Misc.NullSafeBool(row["active"]);

So you need to select the active column using your select query.

string sqlQuery = "Select Active, RestartTime, ProgramLocation, LastRestartTime, RestartInterval, ProgramServer, RestartIfRunning, ProcessName from dbo.anyDatabase where active=1;";

But apart from this I would say that why are you accessing the value of active col like this

restartData.Active = Misc.NullSafeBool(row["active"]);

Even you know that this value will always be 1

So better to use it like this

restartData.Active = true;

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166586

I think your actual select

Select    RestartTime, 
          ProgramLocation, 
          LastRestartTime, 
          RestartInterval, 
          ProgramServer, 
          RestartIfRunning, 
          ProcessName 
from      dbo.anyDatabase 
where     active=1

is fine.

Do note that active is not in the select list though, but you are referencing it in

restartData.Active = Misc.NullSafeBool(row["active"]);

Either change the SELECT to include the active column or remove the line where you reference it.

Upvotes: 2

Related Questions