Showing posts with label helloi. Show all posts
Showing posts with label helloi. Show all posts

Wednesday, March 28, 2012

Substring

Hello

I have the following string:

myName = "John Smith Curtis"

I want to get the first word, i.e. "John", which means I want to get everything from the start to the first space.

I know I should use Substring but I don't know what to do to remove the first word.

Thanks,
Miguelstring myName = "John Smith Curtis";
myName = myName.Substring( 0, myName.IndexOf( " " ) );

The best way to go though would be to do

myName.Split(" ")(0) -> That will return the first element before the space

If you really want to us substring then do

myName.Substring(0,4)


myName.Remove(myName.IndexOf(' ')) will return "John"


try the following ( i didnt check for going out of bound to keep the example simple)

string myName = "John Smith Curtis";

//get the index of the frist space
int index = myName.IndexOf(" ");

//get the first word
string firstname = myName.Substring(0, index);

//remove the first word
myName = myName.Remove(0, index + 1);

Saturday, March 24, 2012

Summary of data in ASP.NET

hello

i am pretty new to asp.net and would like advice on how to best summarise data from an sql server database.

i have some data in a table, each record containing someones name and a 'type' field (contains positive or negative) among other stuff . i need to summarise the count of how many times a persons name appears in the table and display how many positive/negative entries there are.

i've used VB6 and VBA for ages and know how to do this by stepping through recordsets etc but want to know what the best approach is in ADO.NET, i'm still trying to get my head round data readers, datassets, data adapters, data tables etc.

any help is greatly appreciated
thanks
mickThis aggregation would probably be best left to your SQL engine of choice.

Based on the table:

PersonName varchar(50)
Active bit

With the values

PersonName Active
Tom 0
Tom 0
Tom 1
Jane 1
Jim 0

The query:

SELECT PersonName, COUNT(PersonName) HitCount, Active
FROM Person
GROUP BY PersonName, Active
ORDER BY PersonName

Should result in:

PersonName HitCount Active
Jane 1 1
Jim 1 0
Tom 2 0
Tom 1 1

From here, any tutorial on querying a database via SQL (a data reader should be fine) should apply.
thanks very much.