PC Review


Reply
Thread Tools Rate Thread

Convert int to String Error

 
 
=?Utf-8?B?TWlrZSBDb2xsaW5z?=
Guest
Posts: n/a
 
      5th Sep 2006
This worked in the command window while in debug mode

?table.Rows[0].ItemArray["ColumnName"].ToString()
"f0165f94-648f-4997-b578-11d89c8b1f61"

But gives the error below when I compile.

Cannot implicitly convert type 'string' to 'int' and the word ColumnName is
underlined.

Here is a code snippett (with error line marked with an asterisk):

DataSet ds = new DataSet();
ds.ReadXml(@"D:\Data\test.xml", XmlReadMode.ReadSchema);

foreach (DataTable table in ds.Tables)
{

for (int index = 0; index <= table.Rows.Count; index++)
{

{
SqlCommand command = new SqlCommand("ImportData", conn);
command.CommandType = CommandType.StoredProcedure;
adapter.InsertCommand = command;

SqlParameter param;

param = command.Parameters.Add("@columnID", SqlDbType.UniqueIdentifier);
* param.Value = new
Guid(table.Rows[index].ItemArray["ColumnName"].ToString());
}
}
}

Did I do something wrong that I am not seeing, or is there another way to
reference a column?
 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      5th Sep 2006
Hi,

Just use

table.Rows[0]["ColumnName"].ToString()


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



"Mike Collins" <(E-Mail Removed)> wrote in message
news:25A5B241-DB1D-4DE0-AD53-(E-Mail Removed)...
> This worked in the command window while in debug mode
>
> ?table.Rows[0].ItemArray["ColumnName"].ToString()
> "f0165f94-648f-4997-b578-11d89c8b1f61"
>
> But gives the error below when I compile.
>
> Cannot implicitly convert type 'string' to 'int' and the word ColumnName
> is
> underlined.
>
> Here is a code snippett (with error line marked with an asterisk):
>
> DataSet ds = new DataSet();
> ds.ReadXml(@"D:\Data\test.xml", XmlReadMode.ReadSchema);
>
> foreach (DataTable table in ds.Tables)
> {
>
> for (int index = 0; index <= table.Rows.Count; index++)
> {
>
> {
> SqlCommand command = new SqlCommand("ImportData", conn);
> command.CommandType = CommandType.StoredProcedure;
> adapter.InsertCommand = command;
>
> SqlParameter param;
>
> param = command.Parameters.Add("@columnID",
> SqlDbType.UniqueIdentifier);
> * param.Value = new
> Guid(table.Rows[index].ItemArray["ColumnName"].ToString());
> }
> }
> }
>
> Did I do something wrong that I am not seeing, or is there another way to
> reference a column?



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Sep 2006
Mike,

This should not have worked in the command window.

The reason it fails when you compile is because the ItemArray property
returns an array of values representing the row. Arrays are indexed on
integers, not strings, which is why you can't compile it.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Mike Collins" <(E-Mail Removed)> wrote in message
news:25A5B241-DB1D-4DE0-AD53-(E-Mail Removed)...
> This worked in the command window while in debug mode
>
> ?table.Rows[0].ItemArray["ColumnName"].ToString()
> "f0165f94-648f-4997-b578-11d89c8b1f61"
>
> But gives the error below when I compile.
>
> Cannot implicitly convert type 'string' to 'int' and the word ColumnName
> is
> underlined.
>
> Here is a code snippett (with error line marked with an asterisk):
>
> DataSet ds = new DataSet();
> ds.ReadXml(@"D:\Data\test.xml", XmlReadMode.ReadSchema);
>
> foreach (DataTable table in ds.Tables)
> {
>
> for (int index = 0; index <= table.Rows.Count; index++)
> {
>
> {
> SqlCommand command = new SqlCommand("ImportData", conn);
> command.CommandType = CommandType.StoredProcedure;
> adapter.InsertCommand = command;
>
> SqlParameter param;
>
> param = command.Parameters.Add("@columnID",
> SqlDbType.UniqueIdentifier);
> * param.Value = new
> Guid(table.Rows[index].ItemArray["ColumnName"].ToString());
> }
> }
> }
>
> Did I do something wrong that I am not seeing, or is there another way to
> reference a column?



 
Reply With Quote
 
=?Utf-8?B?TWlrZSBDb2xsaW5z?=
Guest
Posts: n/a
 
      5th Sep 2006
That syntax works...thanks.

"Ignacio Machin ( .NET/ C# MVP )" wrote:

> Hi,
>
> Just use
>
> table.Rows[0]["ColumnName"].ToString()
>
>
> --
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
>
>
> "Mike Collins" <(E-Mail Removed)> wrote in message
> news:25A5B241-DB1D-4DE0-AD53-(E-Mail Removed)...
> > This worked in the command window while in debug mode
> >
> > ?table.Rows[0].ItemArray["ColumnName"].ToString()
> > "f0165f94-648f-4997-b578-11d89c8b1f61"
> >
> > But gives the error below when I compile.
> >
> > Cannot implicitly convert type 'string' to 'int' and the word ColumnName
> > is
> > underlined.
> >
> > Here is a code snippett (with error line marked with an asterisk):
> >
> > DataSet ds = new DataSet();
> > ds.ReadXml(@"D:\Data\test.xml", XmlReadMode.ReadSchema);
> >
> > foreach (DataTable table in ds.Tables)
> > {
> >
> > for (int index = 0; index <= table.Rows.Count; index++)
> > {
> >
> > {
> > SqlCommand command = new SqlCommand("ImportData", conn);
> > command.CommandType = CommandType.StoredProcedure;
> > adapter.InsertCommand = command;
> >
> > SqlParameter param;
> >
> > param = command.Parameters.Add("@columnID",
> > SqlDbType.UniqueIdentifier);
> > * param.Value = new
> > Guid(table.Rows[index].ItemArray["ColumnName"].ToString());
> > }
> > }
> > }
> >
> > Did I do something wrong that I am not seeing, or is there another way to
> > reference a column?

>
>
>

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBDb2xsaW5z?=
Guest
Posts: n/a
 
      5th Sep 2006
Thanks, that helps a lot...especially in knowing why it did not work.

"Nicholas Paldino [.NET/C# MVP]" wrote:

> Mike,
>
> This should not have worked in the command window.
>
> The reason it fails when you compile is because the ItemArray property
> returns an array of values representing the row. Arrays are indexed on
> integers, not strings, which is why you can't compile it.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "Mike Collins" <(E-Mail Removed)> wrote in message
> news:25A5B241-DB1D-4DE0-AD53-(E-Mail Removed)...
> > This worked in the command window while in debug mode
> >
> > ?table.Rows[0].ItemArray["ColumnName"].ToString()
> > "f0165f94-648f-4997-b578-11d89c8b1f61"
> >
> > But gives the error below when I compile.
> >
> > Cannot implicitly convert type 'string' to 'int' and the word ColumnName
> > is
> > underlined.
> >
> > Here is a code snippett (with error line marked with an asterisk):
> >
> > DataSet ds = new DataSet();
> > ds.ReadXml(@"D:\Data\test.xml", XmlReadMode.ReadSchema);
> >
> > foreach (DataTable table in ds.Tables)
> > {
> >
> > for (int index = 0; index <= table.Rows.Count; index++)
> > {
> >
> > {
> > SqlCommand command = new SqlCommand("ImportData", conn);
> > command.CommandType = CommandType.StoredProcedure;
> > adapter.InsertCommand = command;
> >
> > SqlParameter param;
> >
> > param = command.Parameters.Add("@columnID",
> > SqlDbType.UniqueIdentifier);
> > * param.Value = new
> > Guid(table.Rows[index].ItemArray["ColumnName"].ToString());
> > }
> > }
> > }
> >
> > Did I do something wrong that I am not seeing, or is there another way to
> > reference a column?

>
>
>

 
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
Convert 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' to '1-dimensional array of String'. roidy Microsoft VB .NET 12 17th Jul 2009 10:53 AM
compiler error: cannot convert type void to string puzzlecracker Microsoft C# .NET 6 8th Oct 2008 05:01 PM
to to fix this can not convert string error?? Ron Microsoft C# .NET 5 17th May 2007 04:41 PM
Error: Failed to convert parameter value from a String to a Byte[]. (Need Help Urgently) weird0 Microsoft C# .NET 0 5th Apr 2007 06:46 PM
Generate Dataset Error: 'string' does not contain a definition for 'Convert' Doug Microsoft ADO .NET 1 4th May 2004 05:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:44 PM.