Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Monday, March 26, 2012

Populate an Array with DB records

Hi Friends,
How can I populate an Array with the information of an Table in SQL?
Thanks... and sorry my english...
Grillo"Guilherme Grillo" <webdev@.rncomtotal.com.br> wrote in message
news:usThK6uIIHA.1212@.TK2MSFTNGP05.phx.gbl...

> How can I populate an Array with the information of an Table in SQL?
http://www.google.co.uk/search?hl=e...br />
st&meta=
Mark Rae
ASP.NET MVP
http://www.markrae.net
Thank you Mark Rae...
Solve my problem!
Now I have another problem.
try
{
int[] pointList = new Int32[60];
int[] idList = new Int32[60];
int i;
for (i = 1; i <= 57; i++)
{
// Cria o comando que seleciona os resultados atuais
commSELECT = new SqlCommand("SELECT * FROM contResults WHERE id = " + i +
"", conn);
// Executa o comando e coloca os resultados em uma Matriz
conn.Open();
readerSELECT = commSELECT.ExecuteReader(CommandBehavior.CloseConnection);
readerSELECT.Read();
pointList[i] = readerSELECT["contResults"];
idList[i] = readerSELECT["id"];
readerSELECT.Close();
Error 1 Cannot implicitly convert type 'object' to 'int'. An explicit
conversion exists (are you missing a cast?)
How can I convert this type'
Here I have the same error:
*int[] respostasList = new Int32[20];
respostasList[1] = RadioButtonList1.SelectedItem.Value;
Sorry my english and thanks again.
Grillo
"Mark Rae [MVP]" <mark@.markNOSPAMrae.net> wrote in message
news:OgWpENvIIHA.4296@.TK2MSFTNGP04.phx.gbl...
> "Guilherme Grillo" <webdev@.rncomtotal.com.br> wrote in message
> news:usThK6uIIHA.1212@.TK2MSFTNGP05.phx.gbl...
>
> http://www.google.co.uk/search?hl=e... />
list&meta=
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
Problem Solved!
Thanks everybody...
Grillo
ps: sorry my english..
"Guilherme Grillo" <webdev@.rncomtotal.com.br> wrote in message
news:uJeouewIIHA.4712@.TK2MSFTNGP04.phx.gbl...
> Thank you Mark Rae...
> Solve my problem!
> Now I have another problem.
>
> try
> {
> int[] pointList = new Int32[60];
> int[] idList = new Int32[60];
> int i;
> for (i = 1; i <= 57; i++)
> {
> // Cria o comando que seleciona os resultados atuais
> commSELECT = new SqlCommand("SELECT * FROM contResults WHERE id = " + i +
> "", conn);
> // Executa o comando e coloca os resultados em uma Matriz
> conn.Open();
> readerSELECT = commSELECT.ExecuteReader(CommandBehavior.CloseConnection);
> readerSELECT.Read();
> pointList[i] = readerSELECT["contResults"];
> idList[i] = readerSELECT["id"];
> readerSELECT.Close();
>
> Error 1 Cannot implicitly convert type 'object' to 'int'. An explicit
> conversion exists (are you missing a cast?)
> How can I convert this type'
> Here I have the same error:
> *int[] respostasList = new Int32[20];
> respostasList[1] = RadioButtonList1.SelectedItem.Value;
>
> Sorry my english and thanks again.
> Grillo
> "Mark Rae [MVP]" <mark@.markNOSPAMrae.net> wrote in message
> news:OgWpENvIIHA.4296@.TK2MSFTNGP04.phx.gbl...
>

Populate an Array with DB records

Hi Friends,

How can I populate an Array with the information of an Table in SQL?

Thanks... and sorry my english...

Grillo"Guilherme Grillo" <webdev@.rncomtotal.com.brwrote in message
news:usThK6uIIHA.1212@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

How can I populate an Array with the information of an Table in SQL?


http://www.google.co.uk/search?hl=e...Arraylist&meta=
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Thank you Mark Rae...

Solve my problem!

Now I have another problem.

try

{

int[] pointList = new Int32[60];

int[] idList = new Int32[60];

int i;

for (i = 1; i <= 57; i++)

{

// Cria o comando que seleciona os resultados atuais

commSELECT = new SqlCommand("SELECT * FROM contResults WHERE id = " + i +
"", conn);

// Executa o comando e coloca os resultados em uma Matriz

conn.Open();

readerSELECT = commSELECT.ExecuteReader(CommandBehavior.CloseConn ection);

readerSELECT.Read();

pointList[i] = readerSELECT["contResults"];

idList[i] = readerSELECT["id"];

readerSELECT.Close();

Error 1 Cannot implicitly convert type 'object' to 'int'. An explicit
conversion exists (are you missing a cast?)

How can I convert this type??

Here I have the same error:

*int[] respostasList = new Int32[20];

respostasList[1] = RadioButtonList1.SelectedItem.Value;

Sorry my english and thanks again.

Grillo

"Mark Rae [MVP]" <mark@.markNOSPAMrae.netwrote in message
news:OgWpENvIIHA.4296@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

"Guilherme Grillo" <webdev@.rncomtotal.com.brwrote in message
news:usThK6uIIHA.1212@.TK2MSFTNGP05.phx.gbl...
>

Quote:

Originally Posted by

>How can I populate an Array with the information of an Table in SQL?


>
http://www.google.co.uk/search?hl=e...Arraylist&meta=
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


Problem Solved!

Thanks everybody...
Grillo

ps: sorry my english..

"Guilherme Grillo" <webdev@.rncomtotal.com.brwrote in message
news:uJeouewIIHA.4712@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

Thank you Mark Rae...
>
Solve my problem!
>
Now I have another problem.
>
>
try
>
{
>
int[] pointList = new Int32[60];
>
int[] idList = new Int32[60];
>
int i;
>
for (i = 1; i <= 57; i++)
>
{
>
// Cria o comando que seleciona os resultados atuais
>
commSELECT = new SqlCommand("SELECT * FROM contResults WHERE id = " + i +
"", conn);
>
// Executa o comando e coloca os resultados em uma Matriz
>
conn.Open();
>
readerSELECT = commSELECT.ExecuteReader(CommandBehavior.CloseConn ection);
>
readerSELECT.Read();
>
pointList[i] = readerSELECT["contResults"];
>
idList[i] = readerSELECT["id"];
>
readerSELECT.Close();
>
>
Error 1 Cannot implicitly convert type 'object' to 'int'. An explicit
conversion exists (are you missing a cast?)
>
How can I convert this type??
>
Here I have the same error:
>
*int[] respostasList = new Int32[20];
>
respostasList[1] = RadioButtonList1.SelectedItem.Value;
>
>
Sorry my english and thanks again.
>
Grillo
>
"Mark Rae [MVP]" <mark@.markNOSPAMrae.netwrote in message
news:OgWpENvIIHA.4296@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

>"Guilherme Grillo" <webdev@.rncomtotal.com.brwrote in message
>news:usThK6uIIHA.1212@.TK2MSFTNGP05.phx.gbl...
>>

Quote:

Originally Posted by

>>How can I populate an Array with the information of an Table in SQL?


>>
>http://www.google.co.uk/search?hl=e...Arraylist&meta=
>>
>>
>--
>Mark Rae
>ASP.NET MVP
>http://www.markrae.net


>
>

Populate an array from a sql query?

I think I did something wrong with this. I am trying to create an array of product id's from a database query.


Dim dReader As SqlDataReader
Dim i As Integer = 0
Dim Products(i) As String
Dim strResults As String

conConnection.Open()
Dim cmdCommand As New SqlCommand("SELECT ProductID FROM Products WHERE CategoryID = '4'", conConnection)

dReader = cmdCommand.ExecuteReader()
While dReader.Read
Products(i) = dReader("Subcategory")
i += 1
strResults += dReader("Subcategory")
End While
Label1.Text = strResults 'just to display if I am getting results

conConnection.Close()

I put that StrResults so I can monitor if anything is getting picked up, but nothing happens at all. What did I do wrong?Doing this freehand so might be a little bit off.


Dim myArrayList as new ArrayList

Dim dReader As SqlDataReader

Dim i As Integer = 0

Dim strResults As String

conConnection.Open()

Dim cmdCommand As New SqlCommand("SELECT ProductID FROM Products WHERE CategoryID = '4'", conConnection)

dReader = cmdCommand.ExecuteReader()

While dReader.Read

myArrayList.Add(dReader("Subcategory"))

End While

conConnection.Close()

Dim x as Integer
For x = 0 to myArrayList.Count - 1
labeli.text += myArrayList(x) & " "
Next


Cool, thanks. I also noticed that I have Subcategory as my Datareader field, but CategoryID in my actual query.
 Dim cmdCommand As New SqlCommand("SELECT ProductID FROM Products WHERE CategoryID = '4'", conConnection)

dReader = cmdCommand.ExecuteReader()

While dReader.Read

Products(i) = dReader("Subcategory")

............

What did I do wrong?

-> You forgot to select "Subcategory" in SQL query?
Oh, sorry, didn't see you found it yourself.
Thanks guys. It feels so good when it finally works! :)

Populate array from a dataset ?

Can someone point me to an example of populating an array from a dataset ?

This .net stuff is killing me.What are you trying to do in more detail? What goes in each element of the array? What about multiple tables?

Not that I'm a Datset fan but you have a lot more functionality with your data in a dataset than an array.

populate array with sql table data vb.net

hello,
I wanted to populate an array with the data from sql table, but not
sure how to go about it.
This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.
Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
Any suggestions how to go about this one. Your time is greatly
appreciated.
cheers, Sharon.Simple example (i assume you have basic knowledge about ADO.NET)
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
End Sub
Private Const ConnectionString As String =
" server=ServerName;uid=UserName;password=
Password;database=DatebaseName"
Private Function GetDates() As System.Collections.Generic.List(Of DateTime)
Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader
Try
connection.Open()
reader = command.ExecuteReader()
While reader.Read()
result.Add(reader.GetDateTime(0))
End While
Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try
Return result
End Function
End Class
Milosz Skalecki
MCP, MCAD
"Sharon" wrote:

> hello,
> I wanted to populate an array with the data from sql table, but not
> sure how to go about it.
> This is the array iam using at present, but i dont want to provide the
> values. Instead i want to query them from sql table t_holidays.
> Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
> #7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
> Any suggestions how to go about this one. Your time is greatly
> appreciated.
> cheers, Sharon.
>
Thanks for the Reply, But its giving me an error
System.Collections.Generic.List is not defined, Please let me know
where did it went wrong.
cheers, Sharon.
Milosz Skalecki wrote:
> Simple example (i assume you have basic knowledge about ADO.NET)
> Imports System.Data
> Imports System.Data.SqlClient
> Partial Class _Default
> Inherits System.Web.UI.Page
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
> End Sub
> Private Const ConnectionString As String =
> " server=ServerName;uid=UserName;password=
Password;database=DatebaseName"
> Private Function GetDates() As System.Collections.Generic.List(Of DateTim
e)
> Dim result As New System.Collections.Generic.List(Of DateTime)
> Dim connection As New SqlConnection(ConnectionString)
> Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
> Condition", connection)
> Dim reader As SqlDataReader
> Try
> connection.Open()
> reader = command.ExecuteReader()
> While reader.Read()
> result.Add(reader.GetDateTime(0))
> End While
> Catch ex As Exception
> Throw ex
> Finally
> connection.Dispose()
> End Try
> Return result
> End Function
> End Class
>
> --
> Milosz Skalecki
> MCP, MCAD
>
> "Sharon" wrote:
>
I am using .NET Framework version, is this class available in this
version
Milosz Skalecki wrote:
> Simple example (i assume you have basic knowledge about ADO.NET)
> Imports System.Data
> Imports System.Data.SqlClient
> Partial Class _Default
> Inherits System.Web.UI.Page
> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Me.Load
> Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
> End Sub
> Private Const ConnectionString As String =
> " server=ServerName;uid=UserName;password=
Password;database=DatebaseName"
> Private Function GetDates() As System.Collections.Generic.List(Of DateTim
e)
> Dim result As New System.Collections.Generic.List(Of DateTime)
> Dim connection As New SqlConnection(ConnectionString)
> Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
> Condition", connection)
> Dim reader As SqlDataReader
> Try
> connection.Open()
> reader = command.ExecuteReader()
> While reader.Read()
> result.Add(reader.GetDateTime(0))
> End While
> Catch ex As Exception
> Throw ex
> Finally
> connection.Dispose()
> End Try
> Return result
> End Function
> End Class
>
> --
> Milosz Skalecki
> MCP, MCAD
>
> "Sharon" wrote:
>
Howdy,
i automatically assumed you were using framework 2.0. Please use
System.Collections.ArrayList instead of
System.Collections.Generic.List(Of DateTime)
regards
Milosz Skalecki
MCP, MCAD
"Sharon" wrote:

> I am using .NET Framework version, is this class available in this
> version
> Milosz Skalecki wrote:
>

populate array with sql table data vb.net

hello,

I wanted to populate an array with the data from sql table, but not
sure how to go about it.

This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.

Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}

Any suggestions how to go about this one. Your time is greatly
appreciated.

cheers, Sharon.Simple example (i assume you have basic knowledge about ADO.NET)

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()

End Sub

Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"

Private Function GetDates() As System.Collections.Generic.List(Of DateTime)

Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader

Try

connection.Open()
reader = command.ExecuteReader()

While reader.Read()
result.Add(reader.GetDateTime(0))
End While

Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try

Return result

End Function

End Class

--
Milosz Skalecki
MCP, MCAD

"Sharon" wrote:

Quote:

Originally Posted by

hello,
>
I wanted to populate an array with the data from sql table, but not
sure how to go about it.
>
This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.
>
Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
>
Any suggestions how to go about this one. Your time is greatly
appreciated.
>
cheers, Sharon.
>
>


Thanks for the Reply, But its giving me an error

System.Collections.Generic.List is not defined, Please let me know
where did it went wrong.

cheers, Sharon.

Milosz Skalecki wrote:

Quote:

Originally Posted by

Simple example (i assume you have basic knowledge about ADO.NET)
>
Imports System.Data
Imports System.Data.SqlClient
>
Partial Class _Default
Inherits System.Web.UI.Page
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
>
Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
>
End Sub
>
Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"
>
Private Function GetDates() As System.Collections.Generic.List(Of DateTime)
>
Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader
>
Try
>
connection.Open()
reader = command.ExecuteReader()
>
While reader.Read()
result.Add(reader.GetDateTime(0))
End While
>
Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try
>
Return result
>
End Function
>
End Class
>
>
--
Milosz Skalecki
MCP, MCAD
>
>
"Sharon" wrote:
>

Quote:

Originally Posted by

hello,

I wanted to populate an array with the data from sql table, but not
sure how to go about it.

This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.

Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}

Any suggestions how to go about this one. Your time is greatly
appreciated.

cheers, Sharon.


I am using .NET Framework version, is this class available in this
version
Milosz Skalecki wrote:

Quote:

Originally Posted by

Simple example (i assume you have basic knowledge about ADO.NET)
>
Imports System.Data
Imports System.Data.SqlClient
>
Partial Class _Default
Inherits System.Web.UI.Page
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
>
Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()
>
End Sub
>
Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"
>
Private Function GetDates() As System.Collections.Generic.List(Of DateTime)
>
Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader
>
Try
>
connection.Open()
reader = command.ExecuteReader()
>
While reader.Read()
result.Add(reader.GetDateTime(0))
End While
>
Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try
>
Return result
>
End Function
>
End Class
>
>
--
Milosz Skalecki
MCP, MCAD
>
>
"Sharon" wrote:
>

Quote:

Originally Posted by

hello,

I wanted to populate an array with the data from sql table, but not
sure how to go about it.

This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.

Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}

Any suggestions how to go about this one. Your time is greatly
appreciated.

cheers, Sharon.


Howdy,

i automatically assumed you were using framework 2.0. Please use
System.Collections.ArrayList instead of
System.Collections.Generic.List(Of DateTime)

regards

--
Milosz Skalecki
MCP, MCAD

"Sharon" wrote:

Quote:

Originally Posted by

I am using .NET Framework version, is this class available in this
version
Milosz Skalecki wrote:

Quote:

Originally Posted by

Simple example (i assume you have basic knowledge about ADO.NET)

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Dim dates As System.Collections.Generic.List(Of DateTime) = GetDates()

End Sub

Private Const ConnectionString As String =
"server=ServerName;uid=UserName;password=Password;d atabase=DatebaseName"

Private Function GetDates() As System.Collections.Generic.List(Of DateTime)

Dim result As New System.Collections.Generic.List(Of DateTime)
Dim connection As New SqlConnection(ConnectionString)
Dim command As New SqlCommand("select DateColumn FROM TableName WHERE
Condition", connection)
Dim reader As SqlDataReader

Try

connection.Open()
reader = command.ExecuteReader()

While reader.Read()
result.Add(reader.GetDateTime(0))
End While

Catch ex As Exception
Throw ex
Finally
connection.Dispose()
End Try

Return result

End Function

End Class

--
Milosz Skalecki
MCP, MCAD

"Sharon" wrote:

Quote:

Originally Posted by

hello,
>
I wanted to populate an array with the data from sql table, but not
sure how to go about it.
>
This is the array iam using at present, but i dont want to provide the
values. Instead i want to query them from sql table t_holidays.
>
Dim HolidayList() As Date = {#7/4/2006#, #7/6/2006#, #7/13/2006#,
#7/19/2006#, #12/24/2006#, #12/25/2006#, #12/29/2006#, #1/1/2007#}
>
Any suggestions how to go about this one. Your time is greatly
appreciated.
>
cheers, Sharon.
>
>


>
>

Populate array from XML

Hi.
I have a two dimensional array. Is it possible to fill it with xml data?

Thanks,
ShawnOn Wed, 13 Jul 2005 16:43:49 +0200, "Shawn" <bossman100@.hotmail.com>
wrote:

>Hi.
>I have a two dimensional array. Is it possible to fill it with xml data?

I am guessing that the Xml contains two values that you want into the
array? What is the array and do you have a sample of the Xml.

You could also have a look on MSDN about readin XmlDocuments:

http://msdn.microsoft.com/library/d...lNodeReader.asp

Hope this helps

Tom

Saturday, March 24, 2012

Populate string array with a SQL row

Can someone tell why I get an "Invalid attempt to read when no data is present. " error?

Here is the code snipplet . . .


SqlCommand DBCommand = new SqlCommand("SELECT * FROM Addresses WHERE AddressID='"+ID.ToString()+"'", DBConn );
try
{
DBConn.Open();
dr = DBCommand.ExecuteReader( System.Data.CommandBehavior.CloseConnection ) ;

}
catch
{
values = null ;
return values ;
}
values[0]=dr["AddressStreet1"].ToString() ;
values[1]=dr["AddressStreet2"].ToString() ;
values[2]=dr["City"].ToString() ;
values[3]=dr["StateProvinceID"].ToString() ;
values[4]=dr["ZipPostalCode"].ToString() ;
values[5]=dr["CountryID"].ToString() ;

Thanks in advance!You need to read the first record of the recordset:

dr.read();
It works except the first column is gone. Why is that?
Disregard my last message - it was my error.

Wednesday, March 21, 2012

Populating 2 dim array.

Hello, I have a 7 column data table in ADO.net with 200 rows. I want to learn how to copy the data in my data table to myarray (6,199) as Integar.

Dim myarray (6, 199) As Integar??

For Each myarray In myDataSet.Tables??

??

??

Next??

Hello my friend,

Use the following: -

Dim myarray(6, 199) As Integer

For i As Integer = 0 To dt.Rows.Count - 1

For j As Integer = 0 To dt.Columns.Count - 1
myarray(i, j) = Convert.ToInt32(dt.Rows(i).Item(j))
Next
Next

Kind regards

Scotty

Friday, March 16, 2012

populating a dropdownlist with data from a multidimensional arrays

Hello,

I hope someone can help me. I have an array defined as a two dimensional array that was populated from a dataset. I want to take that array and populate a dropdownlist. The reason I created a two dimensional array was because in the first dimension I store the value of the dropdownlist and in the second dimension I store the text for the dropdownlist. The value is not just a column from the dataset but a string that is pieced together. Please take a look at the code below and tell me what I'm doing wrong. If someone can please help me. I'm really lost.


'This is were I populate the array
Dim objArray(,) As Object

ReDim objArray(intRowCnt - 1, intColCnt - 1)
i = 0
For Each dr In mDSet.Tables(0).Rows
With mDSet.Tables.Item(0).Rows(i)
If .Item("mbo_key").ToString.Length > 0 Then
objArray(i, 0) = .Item(3).ToString.Trim & "|" & .Item(4).ToString.Trim
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim & " *"
Else
objArray(i, 0) = .Item("period_key").ToString
objArray(i, 1) = .Item(1).ToString.Trim & " - " & .Item(2).ToString.Trim
End If
End With
i += 1
Next

With ddlDropDownList
.DataSource = objArray
For i = 0 To intMBO
' this is were it's blowing up!!!!!
.DataValueField = CStr(objArray(i, 0))
.DataTextField = CStr(objArray(i, 1))
Next
.DataBind()
End With

Why again are you not binding it straight to the Dataset? You should be able to do this using a Dataset, even if it is a concatenation of certain columns (you may even be able to this at the DB level)

As for your problem...you may be able to fix it if instead of using a multidimensial array, use a arrayed class that you define, then you should be able to set the .datavalue/textfield to the class properties (yes use properties). It would look much cleaner too. Very simple class

Public Class Items
Public Property Text...
Public Property Value...
End Class

Hope this helps!
--Michael
I thank you for your help but I'm not sure what you mean. I created 2 Properties like you suggested but I get an error saying I can't convert an array to string when I try to assign it to the .DataValueField of the dropdownlist. Can you please show me an example because I'm really fustrated with this problem.

Also the reason I don't bind it to a dataset is because the value of an option changes depending on if a certain field is Null or not. I wasn't sure how to do this.


With selPeriod
.DataValueField = mclsMBO.marstrValue
.DataTextField = mclsMBO.marstrText
End With

I want to thank you for your help. You set a light off in my head and I ran with it. What I did was I added to Columns to the dataset and populated them with the info I needed it and then I binded those fields to the drop down list. Once again I want to say thanks.

Populating a list box from a loop

I was wondering how you would populate a listbox in asp.net with values from an array created by a loop.
here is the loop structure I am using:
Public Sub Page_Load()
Dim MyString, MyArray, Msg
Dim i as integer
Dim AddThis as string

MyString=BillDS.FieldValue("Week Reason 1", nothing)
MyArray=Split(MyString, "F", -1, vbTextCompare)

Msg=MyArray(0)

For i = 1 To UBound(MyArray)
Msg=Msg & "," & MyArray(i)
Next
lblReasonforError.Text=Msg

End Sub

I can get it to populate the first value in the array but I need it to populate all the values in the array which will vary according to the record it is looking at. Then with those value(s) I want to populate a listbox with all the error codes that are stored in a table on sql server according to the results of the split.
For example:
tblErrorCodes
Code ErrorMessage
1 Wrong
2 Bad

Split gets the value of 1 and 2
Populate listbox like so:
Wrong
Bad
Hope this is clear
Thank you

You can simply bind datalist to an array by setting the array as the datasource of the datalist
http://aspnet.4guysfromrolla.com/articles/082504-1.aspx
Hope that helps


uncleb wrote:

You can simply bind datalist to an array by setting the array as the datasource of the datalist


It's the simplest and the only way since ListBox doesn't have Add method.
Madmaxrtw,I'm not sure what your code is about.
But in case you need to use just some items you'd have to transform array into another one.
Or might be use DataTextField and DataValueField properties.

Hey guys I got it here is the loop script

<script language="VB" runat="server">
Public Sub Page_Load()
Dim MyString, MyArray, Msg
Dim i as integer
Dim AddThis as string


MyString=BillDS.FieldValue("Week Reason 1", nothing)
MyArray=Split(MyString, "F", -1, vbTextCompare)

Msg=MyArray(0)


For i = 1 To UBound(MyArray)
Msg=Msg & "," & MyArray(i)
Next

dlErrorList.DataSource=MyArray
dlErrorList.DataBind()

End Sub
</script>

And the DataList:
<asp:datalist CellPadding="10" ID="dlErrorList" RepeatColumns="0"
RepeatDirection="Vertical" runat="server" gridline="Both">
<ItemTemplate>
<b><%# Container.DataItem %></b>
</ItemTemplate>
</asp:datalist>

Thank you so MUCH!!!


Yakov72 wrote:

It's the simplest and the only way since ListBox doesn't have Add method.



Yes it does.
You add to the ListBox Items array. Not directly to the ListBox.

BobT wrote:


Yes it does.
You add to the ListBox Items array. Not directly to the ListBox.


You're right.
Thank you for corrections.

Populating array with color values

Hi,
I'm trying to populate an array with color names. I keep getting an error.
Here is the line that fails:

Dim Colors As String() = Enum.GetValues(enumColor.GetType())Hi Roger,

You should be able to get all of the colors (including system colors) like
this:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim strSelectedColor As String
Dim enumColor As New KnownColor
Dim Colors As Array = _
[Enum].GetValues(enumColor.GetType())
DropDownList1.DataSource = Colors
DropDownList1.DataBind()
strSelectedColor = DropDownList1.Items(0).Text
End If
End Sub 'Page_Load

Private Sub DropDownList1_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
Label1.Text = DropDownList1.SelectedItem.Text
End Sub

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownList id="DropDownList1" runat="server"
AutoPostBack="True"></asp:DropDownList></P>
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>
</form
Ken
MVP [ASP.NET]

"Roger" <TheFlash@.Superhero.com> wrote in message
news:uguKrq64DHA.2412@.TK2MSFTNGP11.phx.gbl...
> Hi,
> I'm trying to populate an array with color names. I keep getting an
> error.
> Here is the line that fails:
> Dim Colors As String() = Enum.GetValues(enumColor.GetType())
That worked thanks!

"Ken Cox [Microsoft MVP]" <BANSPAMken_cox@.sympatico.ca> wrote in message
news:%236cfSE74DHA.1804@.TK2MSFTNGP12.phx.gbl...
> Hi Roger,
> You should be able to get all of the colors (including system colors) like
> this:
> Private Sub Page_Load _
> (ByVal sender As System.Object, _
> ByVal e As System.EventArgs) _
> Handles MyBase.Load
> If Not IsPostBack Then
> Dim strSelectedColor As String
> Dim enumColor As New KnownColor
> Dim Colors As Array = _
> [Enum].GetValues(enumColor.GetType())
> DropDownList1.DataSource = Colors
> DropDownList1.DataBind()
> strSelectedColor = DropDownList1.Items(0).Text
> End If
> End Sub 'Page_Load
> Private Sub DropDownList1_SelectedIndexChanged _
> (ByVal sender As System.Object, _
> ByVal e As System.EventArgs) _
> Handles DropDownList1.SelectedIndexChanged
> Label1.Text = DropDownList1.SelectedItem.Text
> End Sub
>
> <form id="Form1" method="post" runat="server">
> <P>
> <asp:DropDownList id="DropDownList1" runat="server"
> AutoPostBack="True"></asp:DropDownList></P>
> <P>
> <asp:Label id="Label1" runat="server"></asp:Label></P>
> </form>
> Ken
> MVP [ASP.NET]
>
> "Roger" <TheFlash@.Superhero.com> wrote in message
> news:uguKrq64DHA.2412@.TK2MSFTNGP11.phx.gbl...
> > Hi,
> > I'm trying to populate an array with color names. I keep getting an
> > error.
> > Here is the line that fails:
> > Dim Colors As String() = Enum.GetValues(enumColor.GetType())