Cannot access the added row using Table.Select method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,
I am working on a windows application. I am facing a problem in accessing
the added row of a table using Table.Select method.
Following explains the problem
Following is my table

Table: BatchData
PrimaryKey : BatchId INT IDENTITY (AutoIndex Column)
I am accesing the table's data in a dataset with FillSchema and Fill ,and I
made the primarykey.readonly = false

DS.Tables("BatchData").PrimaryKeyColumn.Readonly = false

I am binding the part of the table to a DataGrid (Based on Selectction
criterion). I am adding the rows to the grid, which inturn adding rows to my
data table. I am able to access the added rows. Now I want to change the
primaryKey value of the added rows.I am able to assign the value to the
primary Key. When I am trying to access the row with the newly assigned
value, I am not able to retrieve the row.

Following are the initial values

DataRow dr = DS.Tables("BatchData").GetChanges(RowState.Added)(0)
dr("PrimaryKey") is 850 now -- Initial Value
I want to assign value - 10001 to the primary key
DS.Tables("BatchData").Select("BatchId = 850")(0)("BatchId") = 10001 -- New
Value

I am able to assign the value, Now I am trying to retrieve the row like the
following

DS.Tables("BatchData").Select("BatchId = 10001")
Here I am facing the problem, I am not able to retrieve the row data if I
use the newly assigned value..

Please help me in solving this problem..


Regards,
RaviKiran
 
Ravi,

I can try to see what is the problem, however when you are busy dynamicly
mostly the dataview.rowfilter is a much better solution.

I hope this helps?

Cor
 
I think you may have a problem with assignment. Do you have Option Strict
on by the way? I was having trouble doing a direct conversion of that code
to C# but this code works fine and does essentially the same thing:
DataTable dt = new DataTable();

DataColumn dc = new DataColumn("BatchId", Type.GetType("System.Int32"));

dt.Columns.Add(dc);

DataRow dro = dt.NewRow();

dt.Rows.Add(dro);

dt.Rows[0][0] = "850";

DataRow dro2 = dt.NewRow();

dt.Rows.Add(dro2);

dt.Rows[1][0] = "1000";


//dv.Count = 0 here but if I change the values of R to ~ then I get 2

dt.PrimaryKey = new DataColumn[] { dt.Columns["ValueName"] };

DataRow[] dros = dt.Select("BatchId = '850'");

dros[0][0] = "1001";

DataRow[] dros2 = dt.Select("BatchId= '1001'");

MessageBox.Show(dt.Rows[0][0].ToString()); //1001
 
Hi Ryan ,
I am able to assign the values. If I see the values of the row values in the
quick watch window , it is showing me the latest value only(1001) , But If I
use the select method (1001) , it is not returning me the datarow, if I query
it with (850), it is returning me the data row which has value (1001 for
column 0)

Primary Key of my table is an auto indexed column in my table.

Can u please suggest any other solution.

Regards,
RaviKiran



W.G. Ryan eMVP said:
I think you may have a problem with assignment. Do you have Option Strict
on by the way? I was having trouble doing a direct conversion of that code
to C# but this code works fine and does essentially the same thing:
DataTable dt = new DataTable();

DataColumn dc = new DataColumn("BatchId", Type.GetType("System.Int32"));

dt.Columns.Add(dc);

DataRow dro = dt.NewRow();

dt.Rows.Add(dro);

dt.Rows[0][0] = "850";

DataRow dro2 = dt.NewRow();

dt.Rows.Add(dro2);

dt.Rows[1][0] = "1000";


//dv.Count = 0 here but if I change the values of R to ~ then I get 2

dt.PrimaryKey = new DataColumn[] { dt.Columns["ValueName"] };

DataRow[] dros = dt.Select("BatchId = '850'");

dros[0][0] = "1001";

DataRow[] dros2 = dt.Select("BatchId= '1001'");

MessageBox.Show(dt.Rows[0][0].ToString()); //1001


RaviKiran said:
Hi All,
I am working on a windows application. I am facing a problem in accessing
the added row of a table using Table.Select method.
Following explains the problem
Following is my table

Table: BatchData
PrimaryKey : BatchId INT IDENTITY (AutoIndex Column)
I am accesing the table's data in a dataset with FillSchema and Fill ,and I
made the primarykey.readonly = false

DS.Tables("BatchData").PrimaryKeyColumn.Readonly = false

I am binding the part of the table to a DataGrid (Based on Selectction
criterion). I am adding the rows to the grid, which inturn adding rows to my
data table. I am able to access the added rows. Now I want to change the
primaryKey value of the added rows.I am able to assign the value to the
primary Key. When I am trying to access the row with the newly assigned
value, I am not able to retrieve the row.

Following are the initial values

DataRow dr = DS.Tables("BatchData").GetChanges(RowState.Added)(0)
dr("PrimaryKey") is 850 now -- Initial Value
I want to assign value - 10001 to the primary key
DS.Tables("BatchData").Select("BatchId = 850")(0)("BatchId") = 10001 -- New
Value

I am able to assign the value, Now I am trying to retrieve the row like the
following

DS.Tables("BatchData").Select("BatchId = 10001")
Here I am facing the problem, I am not able to retrieve the row data if I
use the newly assigned value..

Please help me in solving this problem..


Regards,
RaviKiran
 
Back
Top