Datatype error, I think

I

izco

Hi all,
I'm trying to edit data in a dataset, and keep getting errors. I'm
selecting data from SQL, and a few different fields are either 1 or 0
based on the way questions were answered originally. I'm trying to
change the 1's to "Yes" the 0's to "No", but I'm having no luck.
Here's my code:

SqlDataAdapter myCommand = new SqlDataAdapter("SELECT firstName,
lastName, answer1, answer2, answer3 FROM myTable", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "List");

for (int i = 0; i < ds.Tables["List"].Rows.Count; i++)
{
if(ds.Tables["List"].Rows["answer1"].ToString() == "1"){
ds.Tables["List"].Rows["answer1"].ToString() = "Yes";
}

if(ds.Tables["List"].Rows["answer2"].ToString() == "1")
{
ds.Tables["List"].Rows["answer2"].ToString() = "Yes";
}

if(ds.Tables["List"].Rows["answer3"].ToString() == "1")
{
ds.Tables["List"].Rows["answer3"].ToString() = "Yes";
}
}

ResultsGrid.DataSource= ds.Tables["List"].DefaultView;
ResultsGrid.DataBind();

So, I get the data and dump it into a dataset. Then I go row by row
through the dataset looking for 1's, and attempt to change them. I can
change the 1's to 9's, so I'm on the right track (I think!) But, when
I try to change the 1's to "Yes" I get an "Input string was not in a
correct format" error, or a "the left-hand side of an assignment must
be a variable, property or indexer" error. What can I do to fix this?
There must be a way, right?

I'm kind of new at this and trying to add on to a previous developer's
code, so I can't change the current SQL table or muck with the data
that is in there. I just need it to display properly! Thanks for your
help!
Jim
 
B

Bob Grommes

You don't indicate what data type the fields are in the table (bit?
int?). Offhand I'd say you have a twofold problem:

1) You can't assign a string to the field in the dataset because it's
not of type string, it's going to be bool or int presumably.

2) Your code isn't assigning "Yes" to the row, you're assigning it to a
method called on the row (ToString) which is of course illegal.

I would not try to change the dataset, instead either transform the
values in the SQL to what you need or specify appropriate display
formatting for the underlying value. Since you can't change the SQL
then you will have to go the latter route. If you can indicate the
underlying data types and whether this is a Winforms or ASP.NET data
grid then I or someone here can steer you to the right solution for
getting the display transform that you desire.

--Bob
 
R

Rick Lones

Hi all,
I'm trying to edit data in a dataset, and keep getting errors. I'm
selecting data from SQL, and a few different fields are either 1 or 0
based on the way questions were answered originally. I'm trying to
change the 1's to "Yes" the 0's to "No", but I'm having no luck.
Here's my code:


So, I get the data and dump it into a dataset. Then I go row by row
through the dataset looking for 1's, and attempt to change them. I can
change the 1's to 9's, so I'm on the right track (I think!) But, when
I try to change the 1's to "Yes" I get an "Input string was not in a
correct format" error, or a "the left-hand side of an assignment must
be a variable, property or indexer" error. What can I do to fix this?
There must be a way, right?

Consider your statement:
if(ds.Tables["List"].Rows["answer1"].ToString() == "1"
ds.Tables["List"].Rows["answer1"].ToString() = "Yes";

The problem here is that ToString() is a method not a property. So in the "if"
conditional it works fine - it's a function call which returns a string result -
but you can't assign something to it.

A syntax for what you are trying to do would be:
if(ds.Tables["List"].Rows["answer1"].ToString() == "1"){
ds.Tables["List"].Rows["answer1"] = "Yes";

.. . . but this should only work if "answer1" is a string type. If it's a
numeric type in the dataset you can't assign a string to it. (The reason I
mention this is because of the .ToString() call in the conditional - it looks as
though it is being treated as a string by your program.)

HTH,
-rick-
 
P

pinkstor

Thanks for the replies! The fields that I'm having a problem with
(answer1, answer2 and answer3) are all INT datatype. So the issue is
that I'm trying to make it a string... Bob, you suggested that I
should "specify the appropriate display formatting for the underlying
value." How might I do that? I played around with some DataBinder
methods in the ResultsGrid, but I could never get it to work properly -
I'm not sure if that's the correct way to go at this. Thanks a million
to you both for your help with this!
 
P

pinkstor

Do you have any suggestions on how I might specify the appropriate
display formatting? Like I said, I messed around with some DataBinder
tricks, but nothing worked. The datagrid eventually gets dumped to a
spreadsheet - I'm not sure if that matters or not. Thanks for your
help!
 
B

Bob Grommes

One approach is to hook the ItemDataBound event:

www.codeproject.com/aspnet/gridcolumnformatting.asp

You can also make the column a template column, see these help links in
VS 2003:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclasscolumnstopic.htm
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbconDataGridColumns.htm

For simpler formatting tasks, such as specifying how a date or number is
to display, you can set the DataFormatString property.

--Bob
 

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