Solved

c#, asp.net

Posted on 2013-10-24
8
139 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

628 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