I'm a newbie in programming world. I was given a project and now stuck with the database connectivity. Thankful for responses!
formMain entry = new formMain(dt.Rows[0][0].ToString());
What does this mean? Especially Rows[0][0]
?
Please help.
I'm a newbie in programming world. I was given a project and now stuck with the database connectivity. Thankful for responses!
formMain entry = new formMain(dt.Rows[0][0].ToString());
What does this mean? Especially Rows[0][0]
?
Please help.
"formMain entry = new formMain" is a constructor syntax which means the algorithm is creating an object of the type formMain and calling that object formMain.
Rows[0][0] is a multidimensional array reference inside of the multidimensional array called Rows. Rows[0][0] itself might be any type of data including a value, an array, a multidimensional array, an object...
If X is an array then X[0] is the first value in the array, eg: X = [3,4,2,8,9]; X[0] = 3.
If Y is a multidimensional array then Y[0] is the first sub-array and Y[0][0] is the first entry in the array Y[0], eg: Y = [[2,4,1],[2,3],[9,9,9,9]]; Y[1][1] = 3.
Also because it is dt.Rows that means Rows is a property of an object called 'dt'. dt almost universally denotes a data table (although this is a normal/best practice, not part of the language).
Assuming a normal RDB (relational database) reference in the code (now I may be assuming too much, so double check) Rows[0][0] looks like the top-left cell in a particular database table which is stored in the object dt.
More on multidimensional arrays in c#:
More on constructors in c#: https://msdn.microsoft.com/en-us/library/ace5hbzh.aspx
dt
is most probably an object of DataTable
class (think of DataTable
as a normal database table), which exposes a property Row
of type DataRow[]
(a DataRow
array that is), using which you can access a single row of that DataTable
by passing it the row index; e.g. dt.Rows[4]
will give you the 5th row of your DataTable
.
A DataRow
object (think of a DataRow
as a single row of data) in turn exposes its own indexer of type object[]
(an object
array that is), which gives you access to the individual values of all columns of that DataRow
. So if your DataRow
object is named dr
, you can use dr[2]
to access or change the value of 3rd column of this dr
. So if you want to access or change the value of 3rd column of 5th row, you can do this:
var dr = dt[4]; //5th row is returned and stored in dr
var MyVal = dr[2]; //Value of dr's 3rd column is stored in MyVal
or you can do this in one line:
var MyVal = dt[4][2];
which is exactly what your code is doing, i.e. it is basically passing the value of first column of first row of dt
to the constructor of frmMain
.
My suspicion is that your DataTable
is only being used as a container of a single value (an single integer, one string etc.), i.e. dt
contains only one row and that row has only one value in it, which may be the ID of something. If that is the case, you should better check your code and see where it actually prepares and populates the dt
object. There are better ways of accessing single values from databases (google for ExecuteScalar()
), which will get you rid of creating a DataTable
first and then accessing its rows and columns.