Link to home
Start Free TrialLog in
Avatar of solution1368
solution1368

asked on

c#, asp.net

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of solution1368
solution1368

ASKER

can you change the codes based on my codes? and more importantly, how to call the function in the front end?
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

frontend. it is string array. like string[0] = something1 string[1] = something2...how to do that?
I do not understand the question.
string[] result = GetBondViewList("some code", string "some sub code", 1);

how to call them in an array?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. this for loop is working okay.