Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello!

My question is this: How can I sort the ComboBox data (column from table1) based on which row (row in table2) I currently have selected.
I double click a row in a datagridview in the parent form. This then opens a child form specific to that row (just a way to edit the row info, basically). On this child form, I have a button to open a second child form, in which I have a ComboBox.

I am trying to fill this ComboBox with data from table1 in SQL, which works perfectly fine. BUT, I also want to sort the data based on which row I originally clicked in the parent forms datagridview. I have a label with the ID name of that row brought with me into the child-child form. So that's all fine.

But I still don't know how to sort the data in the ComboBox based on the ID of the child-child form I'm in.

Ok - I am confusing my self now.

I have a stored procedure which goes like this:

SELECT [ID] as ID
,[LøyvehaverID] as LøyvehaverID
,[KjøretøyID] as KjøretøyID
FROM [Løyve]

Can I add a WHERE command here to solve my issue?

Here is my code that I use to populate the ComboBox:

********************* My DataSet **********************
C#
DataSet GetLøyveDataFromDB() {

    try {

        DataSet ds = new DataSet();

        using (SqlConnection sqlCon = new SqlConnection(ConnectionString.connectionString))
        using (SqlDataAdapter sqlDaAd = new SqlDataAdapter("hentLøyver", sqlCon)) {

            sqlDaAd.SelectCommand.CommandType = CommandType.StoredProcedure;
            sqlDaAd.Fill(ds); 
        }

        return ds;

     } catch (Exception) {

                throw;
            }
        }


********************* On form load **********************
C#
    try {

        DataSet dsDataFromDB = GetLøyveDataFromDB();
        cbLøyve.DataSource = dsDataFromDB.Tables[0];
        cbLøyve.DisplayMember = "KjøretøyID";
        cbLøyve.ValueMember = "IDL";
        cbLøyve.SelectedItem = null;

    } catch (Exception ex) {

     MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}


What I have tried:

I have tried many different variations with the stored procedure.
Posted
Updated 14-May-21 3:52am
Comments
CHill60 14-May-21 6:59am    
You don't want a WHERE clause to sort anything, you want an ORDER BY clause in the SQL. If you are going to pass it in as a variable you may need to create some dynamic SQL.
Although reading your question again, regarding ID of the row, then you can pass that ID into the stored procedure as @Parameter and use WHERE ID = @Parameter on the SQL
Flidrip 14-May-21 9:07am    
Thanks!

SQL in general is very new to me, I'm not sure how I would do that.
How can I pass in a column from another table as a parameter?

Am I way off here?

ALTER PROCEDURE [dbo].[myStoredProcedure]
AS
@table2.ColumnName // <-- is this where i declare the parameter, and is that
the way to reference it?
BEGIN
SELECT * FROM table1
WHERE ID = @table2.ColumnName

(because that doesn't seem to work...)

Appreciate you taking the time!
Richard MacCutchan 14-May-21 7:01am    
What about ORDER BY?

1 solution

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)
SQL
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.
C#
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[^]
 
Share this answer
 
v2
Comments
Flidrip 14-May-21 17:04pm    
Here I am once again...
Thank you so much for your informative post.
I learned a lot from your comment, but I still can't get it to work.
I had to edit your stored procedure suggestion somewhat, as my @parameter is a nvarchar. And [ID] is an int. (so with your example I got a error). I set [KjøretøyID] in the WHERE clause (WHERE [KjøretøyID] = @parameter.

Like this:

ALTER PROCEDURE [dbo].[hentLøyver]
@Foretaksnavn nvarchar
AS
BEGIN
SELECT [ID], [LøyvehaverID], [KjøretøyID] as ID
FROM [Løyve]
WHERE [KjøretøyID] = @Foretaksnavn
END

So, all those columns are nvarchar, except [ID].
And I then passed the parameter into my code like per your suggestion. My code runs with no error, but still the ComboBox is empty.

Thank you for the links, I've seen most of them before, and revisited some, but I couldn't quite find a solution that worked for me. Any ideas what could be the issue?
CHill60 15-May-21 9:53am    
I have made a mistake in my solution - it should read
ALTER PROCEDURE [dbo].[myStoredProcedure] @parameter int<pre>That should fix the error in the where clause.
In your version here in the comment you have put <code>[KjøretøyID] as ID</code> but there is already an <code>[ID]</code> column in the select so you will have to call <code>[KjøretøyID]</code> something else e.g. <pre>ALTER PROCEDURE [dbo].[hentLøyver]
@Foretaksnavn int
AS
BEGIN
SELECT [ID], [LøyvehaverID], [KjøretøyID] as KID
FROM [Løyve]
WHERE [KjøretøyID] = @Foretaksnavn
END
I'll fix the error in my solution
Flidrip 15-May-21 14:32pm    
Mr. CHill60, you are very much appreciated!
Unfortunately, I have still been unable to handle my issue.
Your last suggestion wasn't a success for me either.
I'm sure your solution is a good one, but there's something I'm not grasping.
So I wrote up my issue again and posted it on stack overflow. I believe I made my self a bit clearer.
Here's the link, if you want to have a look:
https://stackoverflow.com/questions/67550038/sql-inner-join-parameter-does-not-filter-results

If you want to post your solution here, I'll be back to check up on this thread also.
CHill60 16-May-21 10:10am    
I looked at your stackoverflow qustion - you still have @Foretaksnavn as a varchar. It should be an int

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