Adding a NULL to an array then adding array to a data table

G

Guest

I'm having problems adding some data to my data table where one of my rows
contains a column that has a null value. Here is a snippet of my code
while ((strDataFileLine = DataFile.ReadLine()) != null) //Read till EOF
{
strTokens = strDataFileLine.Split(';'); //Parse
string by ;'s and put in strTokens
for(int i = 0; i < strTokens.Length; i++)
{
if (strTokens == "NULL")
strTokens = DBNull.Value.ToString();
}

if (intTotalColumns == strTokens.Length)
tblToLoad.Rows.Add(strTokens);

I'm reading the data from a text file, parsing it out into an array then
going through the array to check and see if the values contain "NULL" (it was
"" but i changed it to NULL for readability for my sake) When trying to add
the row I get the error
Unhandled Exception: System.ArgumentException: Input string was not in a
correct format.Couldn't store <DBNULL> in highquotaamount Column. Expected
type is Decimal. ---> System.FormatException: Input string was not in a
correct format.
I can't just check for NULL then put DBNull.value in my array because the
array wont accept that type being it's not a string. Is there a good way to
do this that I'm simply over looking?
 
G

Guest

No suggestions?
What about instead of adding each individual item from my text file to a
string in my array, can I instead iterate through my datafile and add item
one to column 1 directly and if item one = "NULL" then set my row's column1
to DBNull, THEN add the row to my table? I'm just throwing that off the top
of my head. Is it even possible, or is there a better solution?
 
G

Guest

Will that work if the field is a string though? Not all my fields are of
type decimal
--
Matt
www.Fiddelke.org


Jeff Dillon said:
Put a 0 in instead?

Fiddelm3742 said:
I'm having problems adding some data to my data table where one of my rows
contains a column that has a null value. Here is a snippet of my code
while ((strDataFileLine = DataFile.ReadLine()) != null) //Read till EOF
{
strTokens = strDataFileLine.Split(';'); //Parse
string by ;'s and put in strTokens
for(int i = 0; i < strTokens.Length; i++)
{
if (strTokens == "NULL")
strTokens = DBNull.Value.ToString();
}

if (intTotalColumns == strTokens.Length)
tblToLoad.Rows.Add(strTokens);

I'm reading the data from a text file, parsing it out into an array then
going through the array to check and see if the values contain "NULL" (it
was
"" but i changed it to NULL for readability for my sake) When trying to
add
the row I get the error
Unhandled Exception: System.ArgumentException: Input string was not in a
correct format.Couldn't store <DBNULL> in highquotaamount Column.
Expected
type is Decimal. ---> System.FormatException: Input string was not in a
correct format.
I can't just check for NULL then put DBNull.value in my array because the
array wont accept that type being it's not a string. Is there a good way
to
do this that I'm simply over looking?
 
G

Guest

Well, i ended up not doing the tblToLoad.Rows.Add(strTokens) being I couldn't
store a DB null in it. What i ended up doing was parsing the line from my
datafile out to the strTokens array still but then doing the following

rowToLoad = tblToLoad.NewRow();
for (int i = 0; i < strTokens.Length; i++)
{
if (strTokens == "")
rowToLoad[tblToLoad.Columns] = DBNull.Value;
else
rowToLoad[tblToLoad.Columns] = strTokens;
}
tblToLoad.Rows.Add(rowToLoad);

If anyone cares or needs it for future reference.
--
Matt
www.Fiddelke.org


Fiddelm3742 said:
Will that work if the field is a string though? Not all my fields are of
type decimal
--
Matt
www.Fiddelke.org


Jeff Dillon said:
Put a 0 in instead?

Fiddelm3742 said:
I'm having problems adding some data to my data table where one of my rows
contains a column that has a null value. Here is a snippet of my code
while ((strDataFileLine = DataFile.ReadLine()) != null) //Read till EOF
{
strTokens = strDataFileLine.Split(';'); //Parse
string by ;'s and put in strTokens
for(int i = 0; i < strTokens.Length; i++)
{
if (strTokens == "NULL")
strTokens = DBNull.Value.ToString();
}

if (intTotalColumns == strTokens.Length)
tblToLoad.Rows.Add(strTokens);

I'm reading the data from a text file, parsing it out into an array then
going through the array to check and see if the values contain "NULL" (it
was
"" but i changed it to NULL for readability for my sake) When trying to
add
the row I get the error
Unhandled Exception: System.ArgumentException: Input string was not in a
correct format.Couldn't store <DBNULL> in highquotaamount Column.
Expected
type is Decimal. ---> System.FormatException: Input string was not in a
correct format.
I can't just check for NULL then put DBNull.value in my array because the
array wont accept that type being it's not a string. Is there a good way
to
do this that I'm simply over looking?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top