0

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.

Asif Patankar
  • 97
  • 4
  • 15

2 Answers2

0

"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

Community
  • 1
  • 1
John Vandivier
  • 2,158
  • 1
  • 17
  • 23
  • Given that `dt` is a `DataTable`, `dt.Rows[0][0]` has nothing whatsoever to do with multi-dimensional arrays, except that it *looks like* that. See my answer for exactly what's happening here. – dotNET Aug 31 '15 at 05:38
0

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.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Thanks a lot! It explained the full concept in a better way. . – Asif Patankar Sep 05 '15 at 09:53
  • @AsifPatankar: Good to know. Plz consider accepting the answer to help others like you. – dotNET Sep 05 '15 at 11:14
  • Done already, but it says i require a few reputations to display the change in number. Thanks again! – Asif Patankar Sep 07 '15 at 04:47
  • @AsifPatankar: You have to click the green tick mark, not the number. – dotNET Sep 07 '15 at 04:50
  • Done with it. As i'm new to the thing, requires a push everytime. – Asif Patankar Sep 07 '15 at 04:56
  • Can you help me with some more concepts? – Asif Patankar Sep 26 '15 at 04:21
  • @AsifPatankar: Sure. Post your questions on SO and we'll see if we can be of any help. – dotNET Sep 26 '15 at 05:56
  • people put me negative marking. So decided for next time to question and answer both. So, waiting for a good concept to come my way. – Asif Patankar Sep 26 '15 at 10:54
  • I wanted to ask, if i want to check whether the user has entered some value in the textbox or left null/blank, do i need to use tryParse or any other alternative according to you. Thank you for your reply! – Asif Patankar Sep 26 '15 at 10:57
  • @AsifPatankar: Though I'm answering your query, plz do not make it a habit to ask questions in comments section. It is not meant for that. Answering your question, try learning more about design-time data-binding. That will take care of nullable values (and many other common data entry related stuff) for you. No need to reinvent the wheel by calling TryParse unless you're expecting some specific form of input. – dotNET Sep 26 '15 at 16:57
  • @AsifPatankar: Apart from that, don't be afraid of asking questions. However, you must also take care that StackOverflow is meant to answer specific questions that arise in specific situations. People here expect you to have some knowledge of the area that your question is related to. Asking general questions or guidelines would be considered off-topic by many (and hence downvoted/closed). Spend time learning the stuff yourself. Internet is full of tutorials and help material. Then implement it and come to SO if you get stuck with something specific, show your effort and ask your question. – dotNET Sep 26 '15 at 17:03
  • i got thoroughly. i'll see that next time i be prepared. Thank you for the guidelines so far! – Asif Patankar Sep 28 '15 at 03:56
  • Hi! @dotNET can i be in contact with you? Please let me know if you pleased. I'll initiate with the contact details. Thank you! – Asif Patankar Oct 19 '15 at 09:27
  • @AsifPatankar: SO would be the best way to keep in contact. You'll sure get a faster response than contacting me privately. – dotNET Oct 20 '15 at 06:50
  • dear @dotNET this is my email asifpatankar42@gmail.com. I'm very much happy to see your response! You really made my day! Thank you! – Asif Patankar Oct 23 '15 at 04:15