0
DataRow drEmpty = dsResult.Tables[1].NewRow();

dsResult.Tables[1].Rows.InsertAt(drEmpty, 0);

DataRow drBranch = dsResult.Tables[1].Rows[1];

drBranch[1] = "Branch"; <--error

dsResult.Tables[1].Rows.InsertAt(drBranch, 1);

my expected output for this few line of code is to add first empty row data into my dropdownlistbox and second row by adding "Branch" into the it but i failed to do so

Error msg given -- Input string was not in a correct format.Couldn't store in ows_ID Column. Expected type is Int64.

after that i try to change to

drBranch[1] = int64.Parse("Branch");

and i get another error

second error msg -- Input string was not in a correct format.

i get the answer that i want already, i will post the answer after 7 hours ,thanks all

Chee mun Low
  • 1
  • 1
  • 3
  • "Branch" is not Int64 ... you know it's not a number... not an integer at all. – cnd Nov 12 '12 at 07:48
  • specify the datatype for that particular column as string in dsResult.Tables[1] – Karthik Nov 12 '12 at 07:53
  • 2
    Do you want to convert the string "Branch" to integer? It is a very tricky question... – maralfol Nov 12 '12 at 07:53
  • i want to add string "Branch" value into int column table, is this possible? – Chee mun Low Nov 12 '12 at 07:56
  • All those functions that convert a string to int only take those strings which have numeric values like "100", "10". – Muhammad Umar Nov 12 '12 at 07:58
  • What are the columns in that DataRow? It seems that you expect a different column at position 1 than is actually there. Note that they count from 0. Maybe access the column by name instead of position, like `drBranch["value"] = "Branch"`. – Hans Kesting Nov 12 '12 at 08:02
  • Check this link, maybe it will be helpful to you http://stackoverflow.com/q/9028029/1577396 – Mr_Green Nov 12 '12 at 08:17

4 Answers4

0

In you example drBranch[1] represents the ows_ID column, which only accepts values of type integer. That means only numbers without a decimal point are allowed.

You could convert strings that contain numbers like "55" to an integer using Int64.Parse("55"), but you can't convert strings like "Branch" to integer. You're trying to store a string in the ows_ID column. Maybe you're trying to access the wrong index?

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

The problem is that "Branch" is not integer.

To cast a value into integer you need an integer value as a string like "526", "100".

Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
0

I assume you think that the column ordinal is one-based but it's zero-based, hence drBranch[1] is the second column.

In my opinion it's better to use the column's name instead:

drBranch.SetField<String>("Branch", newBranch);

(i'm using the SetField extension method since it's strongly typed and supports nullables)

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

I would suggest using a DataTable before using DataRow:

DataTabel dtResult = dsResult.Tables[1].Clone();

dtResult.Columns[1].DataType = Type.GetType("System.String");

DataRow drEmpty = dtResult.NewRow();

dsResult.Tables[1].Rows.InsertAt(drEmpty, 0);

DataRow drBranch = dsResult.Tables[1].Rows[1];

drBranch[1] = "Branch";

dsResult.Tables[1].Rows.InsertAt(drBranch, 1);