Solved

c#, asp.net

Posted on 2013-10-24
8
134 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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