Dataset: Fieldaccess using DB Tablename

  • Thread starter Thread starter garak
  • Start date Start date
G

garak

Hi,

I store an SQL Result with two tables in my DATASET.
Now I want to access the fields int_width which existin both tables.
I only found the solution shown below.

Is there a solution where I can use the name of the Database tabel
and the fieldname ([database table].fieldname)
e.g. tbl_Root_Rooms.intWidth ?

I tried but it didn’t work…

My code so far:
---------------
String Query =
String.Format ("SELECT * FROM tbl_Root_Rooms INNER JOIN
tbl_Root_Objects ON tbl_Root_Rooms.REF_ID_ObjectWall_H
tbl_Root_Objects.ID_Root_Object WHERE STR_RoomName = '{0}'",
RoomName)

DataSet DS_RRoom = PMain.myPDbAccess.QueryDb (Query, "RRoom");
RoomWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width"];
ObjectWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width1"];
 
What do you do with the row number then ? the field being within a row ...

If it is just the general idea of having object orientation, then you can
get it by using typed DataSets ... you get datatset_instance.TableName and
you get row_instance.field_Name


garak said:
Hi,

I store an SQL Result with two tables in my DATASET.
Now I want to access the fields int_width which existin both tables.
I only found the solution shown below.

Is there a solution where I can use the name of the Database tabel
and the fieldname ([database table].fieldname)
e.g. tbl_Root_Rooms.intWidth ?

I tried but it didn’t work…

My code so far:
---------------
String Query =
String.Format ("SELECT * FROM tbl_Root_Rooms INNER JOIN
tbl_Root_Objects ON tbl_Root_Rooms.REF_ID_ObjectWall_H
tbl_Root_Objects.ID_Root_Object WHERE STR_RoomName = '{0}'",
RoomName)

DataSet DS_RRoom = PMain.myPDbAccess.QueryDb (Query, "RRoom");
RoomWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width"];
ObjectWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width1"];
 
Hi garek,

Code seems ok at first glance.
Are you sure there is a table RRoom in your dataset?
Are you sure it has at least one row?
Are you sure that there is int_width field?

Other than that you might consider (as malek suggested) using strong typed
dataset.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

garak said:
Hi,

I store an SQL Result with two tables in my DATASET.
Now I want to access the fields int_width which existin both tables.
I only found the solution shown below.

Is there a solution where I can use the name of the Database tabel
and the fieldname ([database table].fieldname)
e.g. tbl_Root_Rooms.intWidth ?

I tried but it didn’t work…

My code so far:
---------------
String Query =
String.Format ("SELECT * FROM tbl_Root_Rooms INNER JOIN
tbl_Root_Objects ON tbl_Root_Rooms.REF_ID_ObjectWall_H
tbl_Root_Objects.ID_Root_Object WHERE STR_RoomName = '{0}'",
RoomName)

DataSet DS_RRoom = PMain.myPDbAccess.QueryDb (Query, "RRoom");
RoomWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width"];
ObjectWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width1"];
 
sorry, i might have problems to explain my concern properly in
english...
My program works fine, but I think the systax ist not very intuitive.
Instead of usind "int_Width" (for the first table) and "int_Width1"
for the second table, I'd like to use the fieldname in combination
with the tablename. Just to make the code more readable.

RoomWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width"];
ObjectWidth = (int) DS_RRoom.Tables["RRoom"].Rows[0]["int_Width1"];
 
Back
Top