How to do Two Dimensional Array in VB.NET

323 views
Skip to first unread message

Will

unread,
Feb 12, 2010, 9:44:10 AM2/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
As I am new to programming in VB.Net, I would like to know how to
create a 2 dimensional array.

I need to make a select call to the DB and bring back the values,
store the column name with the data associated with it.

For example,

Colums and data for table users_table

user_id last_access_dt upd_by upd_dt
Row 1: TESTER 12-February 2010 WILLV 12-February 2010
Row 2: DASANI 12-February 2010 WILLV 12-February 2010
etc. etc.

//Select statement
SELECT * FROM users_table

I do understand the code below populates a one dimensional array. I
just can't seem to find anything that I can wrap my brain around. One
of those days.

//The declaration below is from a function that calls PopulateRS
Function ProcessSuccessfulConnection
Dim arrReturn() As String

ls_sql_String = " SELECT * FROM users_table"
arrReturn = SQLHelper.PopulateRS(ls_sql_String)
array_count = arrReturn.Length

If array_count > 0 Then

For cc = 0 To array_count - 1
ls_data= arrReturn(cc)

**Here I would like to search for specific column names and
its values.**
**Based on the column names and its values, I have to
manipulate some more code.**

Next

End If

End Function

//In SQLHelper
Public Shared Function PopulateRS(ByVal SQL As String) As String()

Dim rsArray(0 To -1) As String
Dim i As Integer = 0
Dim ds3 As System.Data.DataSet = New System.Data.DataSet
Dim conn As System.Data.OracleClient.OracleConnection = Nothing
Dim da3 As System.Data.OracleClient.OracleDataAdapter
Dim ls_error_msg As String = Nothing
Dim ls_error_msg_redirect As String = Nothing

BasePage.OpenConnection(conn)
da3 = New System.Data.OracleClient.OracleDataAdapter(SQL, conn)
da3.Fill(ds3)

If ds3.Tables(0).Rows.Count > 0 Then
'ReDim Preserve rsArray(ds3.Tables(0).Rows.Count)
ReDim Preserve rsArray(ds3.Tables(0).Rows.Count -1)
For Each dr As System.Data.DataRow In ds3.Tables(0).Rows
rsArray(i) = CStr(dr(0).ToString)
'Check if it is the last row
If i < ds3.Tables(0).Rows.Count Then
i = i + 1
Else
Exit For
End If
Next
End If

BasePage.CloseConnection(conn)

PopulateRS = rsArray

End Function

Any help would be much appreciated.

Thanks,

William

Cerebrus

unread,
Feb 12, 2010, 11:10:07 AM2/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You mentioned you are a beginner. That leads me to deduce that you are
not the author of the SQLHelper class (because I doubt if a beginner
would be assigned to write a DataLayer class.)

Someone appears to have misled you into believing that this is
VB.NET ! This is all VBScript with ASP. Anyone who's ever worked in
ASP would instantly recognize the coding style. I've seen it too often
because it is a common ploy used by rogue software firms to misle
clients who insist on moving to the latest technologies and making
some dough during the so-called "migration phase".

To expand upon your questions:

1. The PopulateRS() function returns a string array containing ONLY
values of the *first column* returned via the SQL script, no matter
how many columns you wanted to retrieve. So you might as well be
asking us how to create a 10-dimensional array and it still would be
no use because the function returns a single dimensional array,
period.

Furthermore, the function does not care about column names. Lastly,
the function does not care about DBNulls. So, unless you have the
professional authority in your firm to invoke another function that
returns something more reasonable or to rewrite your SQLHelper class,
you're in a fix.

I'm sorry to say but the person who wrote the SQLHelper class or
atleast this function has no concept of writing VB.NET code. If that
person happens to be you, then I mean no offense and I would be happy
to show you how it should have been written.

Cimpy

unread,
Feb 12, 2010, 7:40:33 PM2/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
On 12 Feb, 17:10, Cerebrus <zorg...@sify.com> wrote:
[...]> Someone appears to have misled you into believing that this is

VB.NET ! This is all VBScript with ASP.
[...] >I mean no offense and I would be happy to show you how it
should have been written.

Cerebrus ( you're great, you know?) I agree with all, and hope to see
your way of building the "SqlOracleHelper".

Will, my two cents: if you could try to call a different function, you
should get back a DataTable instead of an array (yes, you culd use two
dimensionals arrays, but why as you could get a container able to keep
data, colums, rows and table informations?), have at least a couple
of overloads of this function : at least one more to use parameters
instead of a sql query with all inside (I am sure it happens to you to
call different queries where you specify something in a where
clause...) , and it could come at hand an overload (better two: with
and without parameters) to call stored procedures (packages) instead
of using inline queryes.
I would also avoid to call basepage in Dal - SqlHelper should be a
class library or in any case a class that should not speak directly
with pages, but with class(es) of Business Logic (Now, this last is a
bit advanced topic for a beginner, so do not mind too much if you do
not follow immediately : simply remember these names and, when you get
a better working SqlHelper and some spear time, have a search for them
and for "three tier" developement).

Cimpy

unread,
Feb 12, 2010, 7:50:13 PM2/12/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Do believe the SqlHelper comes from the web, and original code is
really Asp:
http://groups.google.it/group/dotnetdevelopment/browse_thread/thread/2bc04d9930f9cf6a
It might be Will is charged to make a migration all alone...


Cerebrus

unread,
Feb 13, 2010, 11:58:35 AM2/13/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Why, thank you ! ;-)

Charles A. Lopez

unread,
Feb 13, 2010, 2:10:21 PM2/13/10
to dotnetde...@googlegroups.com
Interesting. What exactly are you trying to do? 

You state you want to create a 2 dimensional array. Then what? 
--
Charles A. Lopez
charle...@gmail.com

Registered Microsoft Partner

New York City, NY

I'm running on Windows 7 Build 7100

Quality Software Works

Eder Sousa

unread,
Feb 13, 2010, 4:12:38 PM2/13/10
to dotnetde...@googlegroups.com
To create a 2 dimensional array:
Exemplo:
Dim Array(X, Y) as String
'X - Lines
'Y- Rows
--
Eder Sousa
Technology Information
espnet...@gmail.com
eesp...@hotmail.com
esp...@uol.com.br

Will

unread,
Feb 18, 2010, 7:41:22 AM2/18/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I haven't really had a chance to look over everyones responses as I
was away for almost a week. I appreciate everyones help and once I get
back into the working groove, I will take a closer look all responses.

Thanks again.


On Feb 13, 4:12 pm, Eder Sousa <espnet.sa...@gmail.com> wrote:
> To create a 2 dimensional array:
> Exemplo:
> Dim Array(X, Y) as String
> 'X - Lines
> 'Y- Rows
>
> On Sat, Feb 13, 2010 at 5:10 PM, Charles A. Lopez

> <charlesalo...@gmail.com>wrote:


>
>
>
>
>
> > Interesting. What exactly are you trying to do?
>
> > You state you want to create a 2 dimensional array. Then what?
>

> > charlesalo...@gmail.com


>
> > Registered Microsoft Partner
>
> > New York City, NY
>
> > I'm running on Windows 7 Build 7100
>
> > Quality Software Works
>
> --
> Eder Sousa
> Technology Information

> espnet.so...@gmail.com
> eespn...@hotmail.com
> esp...@uol.com.br- Hide quoted text -
>
> - Show quoted text -

Cesare Imperiali

unread,
Feb 20, 2010, 9:09:55 PM2/20/10
to dotnetde...@googlegroups.com
the minimal conversion could be:

Public Shared Function PopulateRS(ByVal SQL As String) As DataTable

Dim dt As New System.Data.DataTable


Dim conn As System.Data.OracleClient.OracleConnection = Nothing
Dim da3 As System.Data.OracleClient.OracleDataAdapter
Dim ls_error_msg As String = Nothing
Dim ls_error_msg_redirect As String = Nothing

BasePage.OpenConnection(conn)
da3 = New System.Data.OracleClient.OracleDataAdapter(SQL, conn)

da3.Fill(dt)


BasePage.CloseConnection(conn)

Return dt

End Function

you could then play with datable this way:

Dim dt As DataTable = sqlHelper.PopulateRS("select * from testdb")
'print all clomns names
For Each cl As DataColumn In dt.Columns
Response.Write(cl.ColumnName)

Next

'print all values of each row
For Each dr As DataRow In dt.Rows
For Each cl As DataColumn In dt.Columns
Response.Write(dr(cl).ToString())
Next
Next

2010/2/18 Will <william...@tc.gc.ca>:

Will

unread,
Feb 24, 2010, 7:37:49 AM2/24/10
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I've finally had a chance to take a look at all responses, thanks for
all the help. Upon doing further research I really don't need to use
two dimensional arrays, but instead use a data table or data set(as
noted above). This makes more sense as I can retrieve the column name
and value.

William.

On Feb 20, 9:09 pm, Cesare Imperiali <cesare.imperi...@gmail.com>
wrote:

> 2010/2/18 Will <william.vikt...@tc.gc.ca>:

> >> - Show quoted text -- Hide quoted text -

Reply all
Reply to author
Forward
0 new messages