Solved

c#, asp.net

Posted on 2013-10-24
8
137 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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