string array

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

How to get a list of names from database into string array?

variable name should be string array...

This is the code I use...

public string GetFunctionName(Guid UserID)

{


sSelect = "";

string name="";

if (db!=null)

{


if (nGroupID.Length !=0)

{

for ( int iIndex = 0; iIndex < nGroupID.Length; iIndex++ )

{

if ( iIndex != 0 )

sSelect += " OR ";

sSelect += "GroupID ='" + nGroupID[ iIndex ].ToString () + "'";

}



DataRow[] FGrows = db.dataSetUsers.FunctionsGroups.Select(sSelect);


foreach (DataSetUsers.FunctionsGroupsRow row in FGrows)

{

DataSetUsers.FunctionsRow function =

db.dataSetUsers.Functions.FindByFunctionID( row.FunctionID );

name = function.Name ;

}

}

}

return name;

}
 
Once you concatonate the names in a string, you could just use string split
method and it would create an array for you, or add an iterator to your
foreach and add them directly to a string array.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
I'm not that good in c#, can you give me some example?




John Timney (ASP.NET MVP) said:
Once you concatonate the names in a string, you could just use string
split method and it would create an array for you, or add an iterator to
your foreach and add them directly to a string array.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Hrvoje Voda said:
How to get a list of names from database into string array?

variable name should be string array...

This is the code I use...

public string GetFunctionName(Guid UserID)

{


sSelect = "";

string name="";

if (db!=null)

{


if (nGroupID.Length !=0)

{

for ( int iIndex = 0; iIndex < nGroupID.Length; iIndex++ )

{

if ( iIndex != 0 )

sSelect += " OR ";

sSelect += "GroupID ='" + nGroupID[ iIndex ].ToString () + "'";

}



DataRow[] FGrows = db.dataSetUsers.FunctionsGroups.Select(sSelect);


foreach (DataSetUsers.FunctionsGroupsRow row in FGrows)

{

DataSetUsers.FunctionsRow function =

db.dataSetUsers.Functions.FindByFunctionID( row.FunctionID );

name = function.Name ;

}

}

}

return name;

}
 
I am not totally clear what your doing in your code and what you want to do
so I am going to show you a fresh example of how I would do what the
previous guy mentioned.

Keep in mind I didn't understand your example and you may already have a
loop set up to go through the records and the last 2 lines is probably what
you need. Also note I haven't tested this code, but it should work with a
little debugging.

private string[] GetFirstColFromSql(cmdText);

{

System.Data.SqlClient.SqlConnection conn=this.GetOpenConnection();

System.Data.SqlClient.SqlCommand sqlCommand=new
System.Data.SqlClient.SqlCommand(cmdText, conn);

System.Data.SqlClient.SqlDataReader
r=sqlCommand.ExecuteReader(System.Data.CommandBehavior.Default);

String tmpString="";

while (r.Read())

{

tmpString+="|"+Convert.ToString(r.Value(0));

}

string[] resultArray=tmpString.Split('|');

r.Close();

conn.Close();

return resultArray;

}

Hrvoje Voda said:
I'm not that good in c#, can you give me some example?




John Timney (ASP.NET MVP) said:
Once you concatonate the names in a string, you could just use string
split method and it would create an array for you, or add an iterator to
your foreach and add them directly to a string array.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

Hrvoje Voda said:
How to get a list of names from database into string array?

variable name should be string array...

This is the code I use...

public string GetFunctionName(Guid UserID)

{


sSelect = "";

string name="";

if (db!=null)

{


if (nGroupID.Length !=0)

{

for ( int iIndex = 0; iIndex < nGroupID.Length; iIndex++ )

{

if ( iIndex != 0 )

sSelect += " OR ";

sSelect += "GroupID ='" + nGroupID[ iIndex ].ToString () + "'";

}



DataRow[] FGrows = db.dataSetUsers.FunctionsGroups.Select(sSelect);


foreach (DataSetUsers.FunctionsGroupsRow row in FGrows)

{

DataSetUsers.FunctionsRow function =

db.dataSetUsers.Functions.FindByFunctionID( row.FunctionID );

name = function.Name ;

}

}

}

return name;

}
 
Nathan Kovac said:
I am not totally clear what your doing in your code and what you want to do
so I am going to show you a fresh example of how I would do what the
previous guy mentioned.

Keep in mind I didn't understand your example and you may already have a
loop set up to go through the records and the last 2 lines is probably what
you need. Also note I haven't tested this code, but it should work with a
little debugging.

while (r.Read())

{
tmpString+="|"+Convert.ToString(r.Value(0));
}
string[] resultArray=tmpString.Split('|');
r.Close();
conn.Close();
return resultArray;

There's no point in putting everything into a string and then splitting
it up again.

Just use an ArrayList, and entries to that in the while loop. If an
array is required afterwards, you can then call ToArray(typeof(string))
on the ArrayList and cast to string[].
 
Back
Top