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
(E-Mail Removed) 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
>