As per our conversation in the comments:
What you seem to be trying to do is FILTER the results based on ID rather than SORT (which would need an
ORDER BY clause[
^]).
To filter set of results returned in SQL you need to use a
WHERE Clause[
^]. To make that dynamic you will need to pass a parameter into your Stored Procedure, probably something like this..(NB untested)
ALTER PROCEDURE [dbo].[myStoredProcedure] @Foretaksnavn int
AS
SELECT [ID] as ID
,[LøyvehaverID]
,[KjøretøyID]
FROM [Løyve]
WHERE [ID] = @parameter
There is a tutorial
here at MSSqlTips.com[
^] .
Couple of things to note here
- It is best to avoid "special" characters in table and column names e.g. can ø become o or oe instead
- You don't need the
as LøyvehaverID
because the column is already called
LøyvehaverID
. You only need the AS if you want to rename the column or if you have a calculation result that needs a name.
You then pass the parameter into the SP from C# using a Command Parameter e.g.
sqlDaAd.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@Foretaksnavn", SqlDbType.Int).Value = id;
Where
id
is the id you captured from parent forms datagridview. There is more information on using Stored Procedures in .NET here -
Working with SQL Server Stored Procedures and .NET[
^]
When it comes to passing information between forms, I recommend the series of tips by OriginalGriff here on CodeProject. Start here -
Transferring information between two forms, Part 1: Parent to Child[
^]