Convert int to String Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Collins

    Convert int to String Error

    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.Rea dSchema);

    foreach (DataTable table in ds.Tables)
    {

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

    {
    SqlCommand command = new SqlCommand("Imp ortData", conn);
    command.Command Type = CommandType.Sto redProcedure;
    adapter.InsertC ommand = command;

    SqlParameter param;

    param = command.Paramet ers.Add("@colum nID", SqlDbType.Uniqu eIdentifier);
    * 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?
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Convert int to String Error

    Hi,

    Just use

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


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



    "Mike Collins" <MikeCollins@di scussions.micro soft.comwrote in message
    news:25A5B241-DB1D-4DE0-AD53-2E276DE0EE13@mi crosoft.com...
    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.Rea dSchema);
    >
    foreach (DataTable table in ds.Tables)
    {
    >
    for (int index = 0; index <= table.Rows.Coun t; index++)
    {
    >
    {
    SqlCommand command = new SqlCommand("Imp ortData", conn);
    command.Command Type = CommandType.Sto redProcedure;
    adapter.InsertC ommand = command;
    >
    SqlParameter param;
    >
    param = command.Paramet ers.Add("@colum nID",
    SqlDbType.Uniqu eIdentifier);
    * 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?

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Convert int to String Error

      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]
      - mvp@spam.guard. caspershouse.co m

      "Mike Collins" <MikeCollins@di scussions.micro soft.comwrote in message
      news:25A5B241-DB1D-4DE0-AD53-2E276DE0EE13@mi crosoft.com...
      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.Rea dSchema);
      >
      foreach (DataTable table in ds.Tables)
      {
      >
      for (int index = 0; index <= table.Rows.Coun t; index++)
      {
      >
      {
      SqlCommand command = new SqlCommand("Imp ortData", conn);
      command.Command Type = CommandType.Sto redProcedure;
      adapter.InsertC ommand = command;
      >
      SqlParameter param;
      >
      param = command.Paramet ers.Add("@colum nID",
      SqlDbType.Uniqu eIdentifier);
      * 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?

      Comment

      • Mike Collins

        #4
        Re: Convert int to String Error

        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" <MikeCollins@di scussions.micro soft.comwrote in message
        news:25A5B241-DB1D-4DE0-AD53-2E276DE0EE13@mi crosoft.com...
        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.Rea dSchema);

        foreach (DataTable table in ds.Tables)
        {

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

        {
        SqlCommand command = new SqlCommand("Imp ortData", conn);
        command.Command Type = CommandType.Sto redProcedure;
        adapter.InsertC ommand = command;

        SqlParameter param;

        param = command.Paramet ers.Add("@colum nID",
        SqlDbType.Uniqu eIdentifier);
        * 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?
        >
        >
        >

        Comment

        • Mike Collins

          #5
          Re: Convert int to String Error

          Thanks, that helps a lot...especiall y 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]
          - mvp@spam.guard. caspershouse.co m
          >
          "Mike Collins" <MikeCollins@di scussions.micro soft.comwrote in message
          news:25A5B241-DB1D-4DE0-AD53-2E276DE0EE13@mi crosoft.com...
          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.Rea dSchema);

          foreach (DataTable table in ds.Tables)
          {

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

          {
          SqlCommand command = new SqlCommand("Imp ortData", conn);
          command.Command Type = CommandType.Sto redProcedure;
          adapter.InsertC ommand = command;

          SqlParameter param;

          param = command.Paramet ers.Add("@colum nID",
          SqlDbType.Uniqu eIdentifier);
          * 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?
          >
          >
          >

          Comment

          Working...