Click here to Skip to main content
15,895,833 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
for a drug-drug interaction software, the following code contains an array for drugs/ medications that will appear in the autocomplete list.

VB
<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()>
   Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String()
       ' Create array of Drugs


       Dim drugs() As String = {"Methotrexate", "Mannitol", "Allopurinol", "Aspirin", "Vitamin A", "Vitamin C", "Vitamin D", "Vitamin E", "Amoxicillin", "Mercaptopurine", "Diclofenac"}

       ' Return matching drugs
       Return (
           From m In drugs
           Where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase)
           Select m).Take(count).ToArray()
   End Function

i need a way to dynamically load a datatable of 5,000 medication in this array instead of writing the medication list manually

i used this code but doenot seem to help :(
VB
Dim druggv As New GridView
       druggv.DataSource = drugsds
       druggv.DataBind()
       Dim i As Integer = druggv.Rows.Count
       For x = 0 To i - 1
           Dim singleItem As String = druggv.Rows(x).Cells(1).Text.ToString
           Dim list As String = list + ", " + singleItem

       Next
Posted
Comments
Sergey Alexandrovich Kryukov 3-May-13 14:40pm    
You did not tell us where the problem is...
—SA

The simplest way would be to use a text file, where each drug is on a single line:
Methotrexate
Mannitol
Allopurinol
Aspirin
Vitamin A
Vitamin C
Vitamin D
Vitamin E
Amoxicillin
Mercaptopurine
Diclofenac
You could then load them all into an array using File.ReadAllLines:
VB
Dim drugs() As String = File.ReadAllLines("D:\Temp\DrugsList.txt")
And then continue ar you are.

From there on up, there are a huge number of solutions: XML files, CSV files, databases, and so forth.
 
Share this answer
 
Comments
Mohamed Kamal 3-May-13 17:01pm    
thanks a lot. To use a path relative to app root i used the following and worked fine with me

Dim spath As String = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "drugs.txt")

Dim drugs() As String = File.ReadAllLines(spath, Encoding.Default)
If you are going to use linq to find the drugs that start with a certain set of characters, then you do not NEED to have a string array hold the names in the first place. Just pull them right from the datatable. If you need help with that, I suggest reading LINQ Introduction Part 1 Of 3[^].

In regards to the code you tried...if you are going to iterate through a datatable, there is an easier way than the For Loop that you have setup...you can use a For Each Statement[^]. It's just a lot nicer because you don't have to mess with figuring out how many rows there are and stuff like that.

And the reason that your code isn't working has to do with scope. You are declaring variables inside your For statement. Any variable that is declare in the For loop will be thrown away and not accessible when it hits the Next statment and goes to the next row. If you want to keep data that you've gotten from inside a loop, you need to declare the variable that is going to contain it outside the loop.

I'm also not sure what you were doing with the GridView...I thought you wanted a string array? Perhaps you should explain more if what I've said isn't really what you are trying to accomplish.

I hope this helps.
 
Share this answer
 
Comments
Mohamed Kamal 3-May-13 17:11pm    
thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900