MVVM/WPF : How can I set a @Parameter for my Stored Procedure in my ViewModel?

Ole Martin Gulbrandsen 26 Reputation points
2021-07-17T22:56:52.73+00:00

Hello!

Just to give some context:

I have a label in a child-window, which gets its content from a TextBox in the same child-window, which in turn is bound to the selected row of a DataGrid in a parent-window. I am using a framework (Stylet, akin to Caliburn.Micro) which automatically binds View and ViewModel together by name. But to be able to reference the parent-window DataContext, I have set the DataContext for the canvas in my child-window to be that of the parent-window, like so:

DataContext="{Binding Path=(viewmodel:LicenseHolderViewModel.Selected)}"  

This allows me to get the selected value of each column into their respective TextBoxes, by binding the TextBoxes with the propertyname for the columns, like so:

Text="{Binding Foretaksnummer}"  

So, to my question. In the child-window, I have two DataGrids, which I am trying to fill with a collection from my MS Sql table, and sorted based on whos child-window I am currently in (they are essentially business profiles).

It looks like this:

115547-image.png

It gets filled nicely with the contents of the DataGrid selected row, but I am not able to reference anything in order to have a value for the @Parameter I want to pass in to my Stored Procedure in MS Sql.

This is my Stored Procedure:

USE [Ridel.Hub]  
GO  
/****** Object:  StoredProcedure [dbo].[getLicense]    Script Date: 18.07.2021 00:09:58 ******/  
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  

And I am then trying to set value to the parameter in my ViewModel method, like so:

sqlCmd.Parameters.Add("@Foretaksnavn", SqlDbType.NVarChar).Value = ???  

But I have no idea how to reference it.
The value I need is found in the TextBox in the child window with the "Foretaksnavn" label infront of it.
How can I access it without breaking MVVM design principles?

Appreciate any assistance!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,925 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
809 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.