Sort Numbers than send to DB?

  • Thread starter Thread starter Leon
  • Start date Start date
L

Leon

I have six textbox controls on my webform that allows the user to enter any
numbers from 1 to 25 in any order. However, I would like to sort those
numbers from least to greatest before sending them to the database. How can
accomplish this task? Thanks!
 
Easiest way is to use the ArrayList Sort method.

//sorry if you were looking for this in vb
ArrayList list = new ArrayList();

list.Add( Int32.Parse( txt1.Text ) );
list.Add( Int32.Parse( txt2.Text ) );
list.Add( Int32.Parse( txt3.Text ) );
list.Add( Int32.Parse( txt4.Text ) );
list.Add( Int32.Parse( txt5.Text ) );
list.Add( Int32.Parse( txt6.Text ) );

//this will place them in order inside the array.
list.Sort();

//this will pull them back out in order now.
foreach( int i in list )
{

}

HTH,

bill
 
Thanks for your response! But...
but then how work apply the sorted numbers to my database stored procedure
parameters?
 
but then how work apply the sorted numbers to my database stored procedure
parameters?

After looking over your stored procedure, and its parameters, and the code
that populates them from your current code, I can say that you never posted
your stored procedure, its parameters, or any of your code.

One at a time? Very carefully? Post more information?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
Thanks for your response.. But
I don't understand what you mean by..

"After looking over your stored procedure, and its parameters, and the code
that populates them from your current code, I can say that you never posted
your stored procedure, its parameters, or any of your code."

This is my original question..
I have six textbox controls on my webform that allows the user to enter any
numbers from 1 to 25 in any order. However, I would like to sort those
numbers from least to greatest before sending them to the database. How can
accomplish this task? Thanks again!
 
what do u mean by "but then how work apply the sorted numbers to my database
stored procedure
parameters? " ???

If you can give details about ur stored proc and its parameters with your
source code, somebody might give a solution.

Vinay
 
Here is the stored procedure, but I have not wrote any source code because I
trying to figure-out the best way to go about accomplishing my mission which
is I have six textbox controls on my webform that allows the user to enter
any numbers from 1 to 25 in any order. However, I would like to sort those
numbers from least to greatest before sending them to the database. Thanks!

CREATE PROCEDURE AddNumbers
(
@Num1 tinyint,
@Num2 tinyint,
@Num3 tinyint,
@Num4 tinyint,
@Num5 tinyint,
@Num6 tinyint
)
AS
INSERT Number (
Num1,
Num2,
Num3,
Num4,
Num5,
Num6
) VALUES (
@Num1,
@Num2,
@Num3,
@Num4,
@Num5,
@Num6
)
GO
 
We need to see the code that you have now that is sending to database. I
will try to guess what you want.

ArrayList list = new ArrayList();

//add the numbers to the list
list.Add( Int32.Parse( txt1.Text ) );
list.Add( Int32.Parse( txt2.Text ) );
list.Add( Int32.Parse( txt3.Text ) );
list.Add( Int32.Parse( txt4.Text ) );
list.Add( Int32.Parse( txt5.Text ) );
list.Add( Int32.Parse( txt6.Text ) );

//this will sort the numbers
list.Sort();

SendToDatabase( list[0] );
SendToDatabase( list[1] );
SendToDatabase( list[2] );
SendToDatabase( list[3] );
SendToDatabase( list[4] );
SendToDatabase( list[5] );

This will send the numbers in order to the database. This is assuming you
have a method defined called SendToDatabase(). And inside this method you
make your database connection, create your command text, etc. If you need
help on inserting something to a database then ask that question, but add
things like....Sql Server, Access, using Stored Procedures, Xml DataSet,
etc.

We will all need more information if this is not what you are looking for.
Your original question was how to sort them BEFORE sending to the database.
The list.Sort() method will sort them.

If you have another question, please ask it, but repeating your first
question will just get a similiar answer.

bill
 
You would have to create a parameter to hold each of your values in the new
array.
So..
Dim cmd as new Sqlcommand("what ever")

cmd.parameters.add("@value1" , list(0))
cmd.parameters.add("@value2" , list(1))
.......
cmd.parameters.add("@value5" , list(4))
cmd.parameters.add("@value6" , list(5))

list is your sorted arrayList.

Is this what you mean?
 
thanks!

William F. Robertson said:
We need to see the code that you have now that is sending to database. I
will try to guess what you want.

ArrayList list = new ArrayList();

//add the numbers to the list
list.Add( Int32.Parse( txt1.Text ) );
list.Add( Int32.Parse( txt2.Text ) );
list.Add( Int32.Parse( txt3.Text ) );
list.Add( Int32.Parse( txt4.Text ) );
list.Add( Int32.Parse( txt5.Text ) );
list.Add( Int32.Parse( txt6.Text ) );

//this will sort the numbers
list.Sort();

SendToDatabase( list[0] );
SendToDatabase( list[1] );
SendToDatabase( list[2] );
SendToDatabase( list[3] );
SendToDatabase( list[4] );
SendToDatabase( list[5] );

This will send the numbers in order to the database. This is assuming you
have a method defined called SendToDatabase(). And inside this method you
make your database connection, create your command text, etc. If you need
help on inserting something to a database then ask that question, but add
things like....Sql Server, Access, using Stored Procedures, Xml DataSet,
etc.

We will all need more information if this is not what you are looking for.
Your original question was how to sort them BEFORE sending to the
database.
The list.Sort() method will sort them.

If you have another question, please ask it, but repeating your first
question will just get a similiar answer.

bill



Leon said:
Thanks for your response.. But
I don't understand what you mean by..

"After looking over your stored procedure, and its parameters, and the code
that populates them from your current code, I can say that you never posted
your stored procedure, its parameters, or any of your code."

This is my original question..
I have six textbox controls on my webform that allows the user to enter any
numbers from 1 to 25 in any order. However, I would like to sort those
numbers from least to greatest before sending them to the database. How can
accomplish this task? Thanks again!
 
yes! thanks.
Tampa .NET Koder said:
You would have to create a parameter to hold each of your values in the
new
array.
So..
Dim cmd as new Sqlcommand("what ever")

cmd.parameters.add("@value1" , list(0))
cmd.parameters.add("@value2" , list(1))
......
cmd.parameters.add("@value5" , list(4))
cmd.parameters.add("@value6" , list(5))

list is your sorted arrayList.

Is this what you mean?
 
Hi Leon,

I was responding to your response, which is quoted directly under mine in
this message. However, I can see that you asked this question in 2 separte
threads, and that you did post the Stored Procedure code after I had posted
my response, and also got the correct answer, which is, to add the
parameters in the order in which they appear after sorting.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.
 
Back
Top