Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c#, asp.net

Posted on 2013-10-24
8
Medium Priority
?
142 Views
Last Modified: 2013-10-30
The attached code is working. However, we won't know how rows will be returned into
string[] x = new string[?].

For now, I hard code to 3. How can I handle the code without knowing the number of rows in a array?
a.txt
0
Comment
Question by:solution1368
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39598932
You could use a List<string> instead. That would get you dynamic addition of items. If you truly need an array in the end, you can simply call the ToArray method of the List class.

e.g.

using System.Collections.Generic;

...

List<string> x = new List<string>();

for (int i = 0; i < dt.Rows.Count; i++)
{
    x.Add(dt.Rows[i]["ViewName"].ToString());
}

// If you need an array
string[] y = x.ToArray();

Open in new window

0
 

Author Comment

by:solution1368
ID: 39598968
can you change the codes based on my codes? and more importantly, how to call the function in the front end?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39598980
At Top Of Code File
using System.Collections.Generic;

Open in new window


public string[] GetBondViewList(string stateCode, string classSubCodeName, int statusID)
       {
           DbCommand com = GenericDataAccess.CreateCommand();
           com.CommandText = "GetBondViewUI";
           DbParameter param = com.CreateParameter();
           //==============================================
           param.ParameterName = "@StateCode";
           param.Value = stateCode;
           param.DbType = DbType.String;
           com.Parameters.Add(param);
           //===============================================   
           param = com.CreateParameter();
           param.ParameterName = "@classSubCodeName";
           param.Value = classSubCodeName;
           param.DbType = DbType.String;
           com.Parameters.Add(param);
           //===============================================           
           param = com.CreateParameter();
           param.ParameterName = "@StatusID";
           param.Value = 1;
           param.DbType = DbType.Int16;
           com.Parameters.Add(param);
           //===============================================
           DataTable dt = GenericDataAccess.ExecuteSelectCommand(com);
           if (dt.Rows.Count > 0)
           {
               List<string> y = new List<string>();

               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   y.Add(dt.Rows[i]["ViewName"].ToString());
               }

               return y.ToArray();
           }
           else
           {
               return null;
           }
       }
    }

Open in new window


Front End
string[] result = GetBondViewList("some code", string "some sub code", 1);

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:solution1368
ID: 39598992
frontend. it is string array. like string[0] = something1 string[1] = something2...how to do that?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39599023
I do not understand the question.
0
 

Author Comment

by:solution1368
ID: 39599052
string[] result = GetBondViewList("some code", string "some sub code", 1);

how to call them in an array?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39599144
Your GetBondViewList method will return an array, so if you are asking how to inspect the values that it returns, then you can do something like:

string[] result = GetBondViewList("some code", string "some sub code", 1);

if (result != null)
{
    for (int i = 0; i < result.Length; i++)
    {
        Console.WriteLine(result[i]);
    }
}

Open in new window

0
 

Author Closing Comment

by:solution1368
ID: 39613178
Thanks. this for loop is working okay.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question