Specified cast is not valid

  • Thread starter Thread starter Ruslan Shlain
  • Start date Start date
R

Ruslan Shlain

A little problem I am having and its driving me nuts.
I have a datatable and I am adding a column to it on the fly. Then based on
some other fields i add or not add a value to that column for each row.
When I do update statement on DataAdapter I get "Specified cast is not
valid". The DB column is varchar 50. Here is my code.

dsDS.Tables[0].Columns.Add("dt_date");

int i;


for(i = 0;i < objTIC.Count; i++)

{

string k = objTIC;

string[] s = k.Split(',');


foreach (DataRow dr in dsDS.Tables[0].Rows)

{

if (dr["number"].ToString() == DBNull.Value)

{

dr["dtcontract_date"] = "1";

}

}

}
 
Ruslan, which line is throwing the exception? I'm guessing its the if
(dr["number"].ToString() == DBNull.Value)? What value is dr["number"] if
you step through it?
 
Sorry for the confusion. I get that error when i use the DataAdapter.Update
with the dataset that contains that table

--
Ruslan Shlain
nAlliance Corporation
www.nAlliance.com
William Ryan eMVP said:
Ruslan, which line is throwing the exception? I'm guessing its the if
(dr["number"].ToString() == DBNull.Value)? What value is dr["number"] if
you step through it?
Ruslan Shlain said:
A little problem I am having and its driving me nuts.
I have a datatable and I am adding a column to it on the fly. Then based on
some other fields i add or not add a value to that column for each row.
When I do update statement on DataAdapter I get "Specified cast is not
valid". The DB column is varchar 50. Here is my code.

dsDS.Tables[0].Columns.Add("dt_date");

int i;


for(i = 0;i < objTIC.Count; i++)

{

string k = objTIC;

string[] s = k.Split(',');


foreach (DataRow dr in dsDS.Tables[0].Rows)

{

if (dr["number"].ToString() == DBNull.Value)

{

dr["dtcontract_date"] = "1";

}

}

}

 
Ruslan Shlain said:
A little problem I am having and its driving me nuts.
I have a datatable and I am adding a column to it on the fly. Then based on
some other fields i add or not add a value to that column for each row.
When I do update statement on DataAdapter I get "Specified cast is not
valid". The DB column is varchar 50. Here is my code.

*Which* DB column is varchar 50? You mention 3 different columns in the
code posted.
 
Since you aren't casting anything per se and you are sure it's in the
update...let me ask this, what is the DB Type in the database that
corresponds to dt_contractdate? It looks like you are setting the value to
1, but if it's a date value on the backend, 1 isn't a legit date so that may
be the issue.
Ruslan Shlain said:
Sorry for the confusion. I get that error when i use the DataAdapter.Update
with the dataset that contains that table

--
Ruslan Shlain
nAlliance Corporation
www.nAlliance.com
William Ryan eMVP said:
Ruslan, which line is throwing the exception? I'm guessing its the if
(dr["number"].ToString() == DBNull.Value)? What value is dr["number"] if
you step through it?
Ruslan Shlain said:
A little problem I am having and its driving me nuts.
I have a datatable and I am adding a column to it on the fly. Then
based
on
some other fields i add or not add a value to that column for each row.
When I do update statement on DataAdapter I get "Specified cast is not
valid". The DB column is varchar 50. Here is my code.

dsDS.Tables[0].Columns.Add("dt_date");

int i;


for(i = 0;i < objTIC.Count; i++)

{

string k = objTIC;

string[] s = k.Split(',');


foreach (DataRow dr in dsDS.Tables[0].Rows)

{

if (dr["number"].ToString() == DBNull.Value)

{

dr["dtcontract_date"] = "1";

}

}

}


 
Ruslan Shlain said:
Its the column that i am adding to the datatable

But you didn't show it getting populated with data. What does your
update statement look like? What is the type in the database itself?
 
Try using the statemen
dsDS.Tables[0].Columns.Add("dt_date",GetType(Type of the column that you are adding)
if the type is date then put it as System.Date or else if it is Integer then put it as System.Int3

Shij
MCP
 
Back
Top