Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I have a window with a DataGrid which I want to populate with a Sql server table column, sorted by a second column. The current value of the second column is in a label that is present in the same window (a header, designating whos window I'm currently visiting).

I am struggling to use that label as a parameter in my INNER JOIN Stored Procedure, in order to properly sort the DataGrid.

Currently, nothing shows up in my DataGrid, as my parameter comes out null, and "not set to an instance of an object".

This is my stored procedure:

XML
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getLicense]
	@Foretaksnavn nvarchar(50)
AS 
SELECT tblLicense.ID, LøyvehaverID, KjøretøyID
FROM tblLicense
INNER JOIN tblLicenseHolder
ON tblLicense.LøyvehaverID = tblLicenseHolder.Foretaksnummer
WHERE tblLicense.LøyvehaverID = @Foretaksnavn


This is my method, which is in my ViewModel (View and ViewModel is automatically coupled, as per Stylet framework.

public void FillDataGridLicenses() {
           // Fyller DataGrid med oppdatert liste over løyver fra databasen

           try {

               using (SqlConnection sqlCon = new(ConnectionString.connectionString))
               using (SqlCommand sqlCmd = new("getLicense", sqlCon))
               using (SqlDataAdapter sqlDaAd = new(sqlCmd))
               using (DataSet ds = new()) {

                   sqlCon.Open();
                   sqlCmd.CommandType = CommandType.StoredProcedure;
                   sqlCmd.Parameters.Add("@Foretaksnavn", SqlDbType.NVarChar).Value = LblHeaderText.ToString();
                   sqlDaAd.Fill(ds);

                   foreach (DataRow dr in ds.Tables[0].Rows) {

                       Licenses.Add(new License {

                           ID = Convert.ToInt32(dr[0].ToString()),
                           LøyvehaverID = dr[1].ToString(),
                           KjøretøyID = dr[2].ToString(),
                       });
                   }
               }

           } catch (Exception ex) {

               MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK, MessageBoxImage.Information);
           }
       }


This is the XAML for the DataGrid:

<DataGrid
           x:Name="dgLicense"
           Canvas.Left="315"
           Canvas.Top="501"
           Width="332"
           Height="214"
           AutoGenerateColumns="False"
           BorderBrush="#FF707070"
           CanUserAddRows="False"
           CanUserDeleteRows="False"
           FontSize="14"
           IsReadOnly="True"
           Loaded="{s:Action FillDataGridLicenses}"
           ItemsSource="{Binding LicenseHolders}"
           SelectedItem="{Binding Selected, Mode=TwoWay}"
           SelectionMode="Single" SelectionUnit="FullRow" >
           <DataGrid.Columns>
               <DataGridTextColumn
                   Header="ID"
                   IsReadOnly="True"
                   Visibility="Collapsed"
                   Binding="{Binding Path=ID, UpdateSourceTrigger=PropertyChanged}"/>
               <DataGridTextColumn
                   Header="Løyve"
                   IsReadOnly="True"
                   Width="122"
                   HeaderStyle="{StaticResource CenterGridHeaderStyle}"
                   Visibility="Visible"
                   Binding="{Binding Path=LøyveID, UpdateSourceTrigger=PropertyChanged}"/>
               <DataGridTextColumn
                   Header="Reg.nr"
                   IsReadOnly="True"
                   Visibility="Visible"
                   Width="92"
                   HeaderStyle="{StaticResource CenterGridHeaderStyle}"
                   Binding="{Binding Path=Registreringsnummer, UpdateSourceTrigger=PropertyChanged}"/>
               <DataGridTextColumn
                   Header="Modell"
                   IsReadOnly="True"
                   HeaderStyle="{StaticResource CenterGridHeaderStyle}"
                   Width="*"
                   Visibility="Visible"
                   Binding="{Binding Path=Kjøretøymodell, UpdateSourceTrigger=PropertyChanged}"/>
               <DataGridTextColumn
                   Header="Operasjonsdato"
                   IsReadOnly="True"
                   Visibility="Collapsed"
                   Binding="{Binding Path=Operasjonsdato, UpdateSourceTrigger=PropertyChanged}"/>
           </DataGrid.Columns>
       </DataGrid>


What I have tried:

I have spent many hours trying to find a solution online, but I have been unable to implement a working solution.
Greatly appreciate any help!
Posted
Comments
Gerry Schmitz 5-Aug-21 17:35pm
   
Your "bindings" bear no relationship to your C# code.

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