PC Review


Reply
Thread Tools Rate Thread

Datatype error, I think

 
 
izco@hotmail.com
Guest
Posts: n/a
 
      27th Sep 2005
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

 
Reply With Quote
 
 
 
 
Bob Grommes
Guest
Posts: n/a
 
      27th Sep 2005
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
>

 
Reply With Quote
 
Rick Lones
Guest
Posts: n/a
 
      27th Sep 2005
(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:
>
>
> 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-
 
Reply With Quote
 
pinkstor@gmail.com
Guest
Posts: n/a
 
      27th Sep 2005
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!

 
Reply With Quote
 
Bob Grommes
Guest
Posts: n/a
 
      28th Sep 2005
I'd still like to know if this is WinForms or ASP.NET.

Thx,

--Bob

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

 
Reply With Quote
 
pinkstor@gmail.com
Guest
Posts: n/a
 
      28th Sep 2005
Oops, sorry. It's asp.net.

Thanks,
Jim

 
Reply With Quote
 
pinkstor@gmail.com
Guest
Posts: n/a
 
      28th Sep 2005
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!

 
Reply With Quote
 
Bob Grommes
Guest
Posts: n/a
 
      28th Sep 2005
One approach is to hook the ItemDataBound event:

http://www.codeproject.com/aspnet/gr...formatting.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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion date datatype to long datatype raju Microsoft Dot NET Compact Framework 3 9th Jun 2006 12:28 PM
Boolean datatype column refuses to Copy to Bit datatype SQL Table =?Utf-8?B?RmlkZGVsbTM3NDI=?= Microsoft ADO .NET 1 30th May 2006 07:28 PM
Datatype error =?Utf-8?B?WFA=?= Microsoft Access Queries 2 7th Sep 2005 01:05 PM
The Active Directory datatype cannot be converted to/from a native DS datatype Hechmi Microsoft Dot NET 1 1st Mar 2004 07:15 AM
datatype error al Microsoft ASP .NET 0 7th Jan 2004 10:17 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:22 PM.