Reputation: 355
I've been using this same one line code to save a specific field from my database to a variable.
variable = Convert.ToType(dt.Rows[0]["[Column Name]"]);
Problem is i'm getting this annoying column doesn't exist server error. This is happening for different tables and different fields with sql queries as simple as "Select * From Table". I've checked countless of times that the column name is correct , i tried with space in the column name and without - nothing seems to help.
Any idea what's causing this and how to solve it? Here is the stack trace:
[ArgumentException: Column '[Long MSDS Update]' does not belong to table Table.]
System.Data.DataRow.GetDataColumn(String columnName) +5253191
System.Data.DataRow.get_Item(String columnName) +13
SiteMaster.msds_update() in
c:\Users\nravid\Desktop\School\Project\PhillipsSite\Site.master.cs:198
SiteMaster.master_Page_PreLoad(Object sender, EventArgs e) in c:\Users\nravid\Desktop\School\Project\PhillipsSite\Site.master.cs:92
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Page.OnPreLoad(EventArgs e) +121
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +699
Thanks.
Upvotes: 1
Views: 12803
Reputation: 20330
Wrap a test round it
if (dt.Columns.Contains("SomeColumnName"))
{
}
else
{
// Danger Will Robinson!. Danger!
}
Upvotes: 1
Reputation: 26396
You were getting this error because you included the brackets [ ]. Remove the square brackets and it should work
Use
dt.Rows[0]["Column Name"]
Instead of
dt.Rows[0]["[Column Name]"]
In your case
dt.Rows[0]["Long MSDS Update"]
Upvotes: 5
Reputation: 50245
Run the code below and make sure the output is expected.
Debug.WriteLine(dt.TableName);
foreach(DataColumn column in dt.Columns)
{
Debug.WriteLine("\t" + column.ColumnName);
}
Upvotes: 1