469,898 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Datatype error, I think

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[i]["answer1"].ToString() == "1"){
ds.Tables["List"].Rows[i]["answer1"].ToString() = "Yes";
}

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

if(ds.Tables["List"].Rows[i]["answer3"].ToString() == "1")
{
ds.Tables["List"].Rows[i]["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

Nov 17 '05 #1
5 1096
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

iz**@hotmail.com wrote:
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[i]["answer1"].ToString() == "1"){
ds.Tables["List"].Rows[i]["answer1"].ToString() = "Yes";
}

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

if(ds.Tables["List"].Rows[i]["answer3"].ToString() == "1")
{
ds.Tables["List"].Rows[i]["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

Nov 17 '05 #2
iz**@hotmail.com wrote:
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[i]["answer1"].ToString() == "1"
ds.Tables["List"].Rows[i]["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[i]["answer1"].ToString() == "1"){
ds.Tables["List"].Rows[i]["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-
Nov 17 '05 #3
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!

Nov 17 '05 #4
I'd still like to know if this is WinForms or ASP.NET.

Thx,

--Bob

pi******@gmail.com wrote:
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!

Nov 17 '05 #5
Oops, sorry. It's asp.net.

Thanks,
Jim

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by J.C. Flores | last post: by
8 posts views Thread by Eternally | last post: by
1 post views Thread by al | last post: by
1 post views Thread by Prabu Subroto | last post: by
1 post views Thread by Jay Balapa | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.