Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Ordering

Author  Topic 

Taurib
Starting Member

5 Posts

Posted - 2011-06-07 : 14:39:37
Greetings, does anyone know, that how can i order my database by "Name" column instead of ID column. I need to do this after, i have done my database module and LINQ to SQL. Well, i need to order them on my website in GridView.

Also i have already made an event:

protected void Name(object sender, GridViewSortEventArgs e)
{

}

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-06-07 : 15:14:54
You're gonna have to give us a bit more information on what you're doing, what you want...

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Taurib
Starting Member

5 Posts

Posted - 2011-06-07 : 15:23:27
I updated mine first post, well, i have done that i enabled sorting and in events below sorting i wrote there "Name", then it made me this

protected void Name(object sender, GridViewSortEventArgs e)
{

}

Problem is that, that i don't know, what do i have to write inside { }
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-06-07 : 17:46:44
From that, I don't know either.

We can't see your machine, we can't read your mind, we don't know the background to the problem. Please be specific and detailed as to what you are trying to do

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Taurib
Starting Member

5 Posts

Posted - 2011-06-08 : 10:46:04
Well, ok i try to explain as good as i can
1: I have made database module in MS Web developer.
2: I generated database module from it and made it to MS SQL server manager as a Database
3: I use that database in my web page
4: (Problem starts here) Every time i make a new item to database, it orders it by ID, but in web page i would like that they would be ordered by name

For conclusion, i have problem with ordering items in Database and/or Gridview. Because it shows the order by items ID not by it's name.
example:
Product 1
Product 3
Product 2

Ordering is done by this that, when the items are added and so what is their ID number.

I hope you understand my point
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-06-08 : 11:07:00
SELECT <some columns> FROM <your table>
ORDER BY name
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-06-08 : 11:23:41
How are you doing data access? SQL statement? Entity framework? Linq? Something else?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Taurib
Starting Member

5 Posts

Posted - 2011-06-08 : 11:42:09
quote:
Originally posted by russell

SELECT <some columns> FROM <your table>
ORDER BY name



This didn't work, i forgot to say that my language is C# (if it matters)

quote:
Originally posted by GilaMonster
How are you doing data access? SQL statement? Entity framework? Linq? Something else?



First i make entity, then i generate from module. Then i go to MS server manager R2 and paste the code and execute it. Then i use LINQ to SQL and there is my database connected. On web page i use GridView to see the database data.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-06-08 : 11:49:21
I don't believe that it didn't work if you pasted a SQL statement.

Order By is the ONLY way to sort data server-side.

In your case, use the LINQ orderby method. Or you can sort in your C# code
Go to Top of Page

Taurib
Starting Member

5 Posts

Posted - 2011-06-08 : 12:47:41
quote:
Originally posted by russell

I don't believe that it didn't work if you pasted a SQL statement.

Order By is the ONLY way to sort data server-side.

In your case, use the LINQ orderby method. Or you can sort in your C# code



LINQ orderby method? Well i don't know what does it exactly means, but i have tried to write

SELECT <some columns> FROM <your table>
ORDER BY name

code to database, it allowed me to only leave "ORDER BY name" there, but nothing changed.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-06-08 : 13:24:33
If you're just sucking the whole table in, then somewhere (LINQ) it is converting your code to "SELECT * " with no ORDER BY clause.

In this case, you can't guarentee the sort order.

I don't know what you pasted that code snippet into, but if you paste it into your query editor, it will return as expected.

You can google out the LINQ methods you need -- I'm no LINQ expert. But it is almost always more efficient to write a stored procedure and use that instead.
Go to Top of Page
   

- Advertisement -