Reputation: 139
Good evening,
I have now in my project a few windows. The first is a standard login window. This window specifies the server name, user name and password for a login on a SQL Server. If the entry is correct, you will be connected and routed to the next window. This works flawlessly.
In the second window, there is a ListBox with a list of existing databases from the logged in server and a ListView that displays the tables of the selected (from the listbox) database. This also works
BUT
it only works because I have entered the SQLConnection data manually in the code. so:
SqlConnection con = new SqlConnection("Server=servername;User Id=username;Password=password;");
Now how do I get the previously entered login data into the second window so i can use them to show the databases from the logged in server?
In the login window, it is easy to use the entered datas by "Server = listboxservername.Text" etc. but this I can't use in the second screen. Code:
public const string CONSTRING = "Server={0};User Id={1};Password={2};";
SqlConnection con = new SqlConnection(String.Format(CONSTRING, txtbServer.Text, txtbUsername.Text, txtbPassword.Password));
Now how do I get the previously entered data into the second window in order to be able to continue to use them?
Thanks for support!
Dave
(please excuse my bad IT english)
Upvotes: 0
Views: 515
Reputation: 6961
This sounds like you are writing a tightly coupled application with your windows holding all your data. This was a pattern that was common in WinForms but has been surpassed in WPF for an MVC pattern commonly known as MVVM, or Model-View-ViewModel.
The big advantage this gives you is that allows sharing of your data between different views, whether those views are windows or controls. So in your case you would create an object to hold all your data, lets call it MyModel
and then share that with both of your windows.
This however is just the first half of what MVVM is about. Normally in addition to the Model, you also define a ViewModel class that converts data into forms directly usable by your UI, and uses INotifyPropertyChanged
or a base class that inherits from that. Finally the view picks up the data from the VM by Binding to it and listening for property changes so it can update.
So putting it all together you might have something like
public class MyModel
{
public string User {get;set;}
public SqlConnection Connection {get;set;}
}
public class ViewModel : INotifyPropertyChanged
{
private MyModel _model = new MyModel();
public string Server
{
get { return _model.Connection.DataSource; }
set
{
_model.Connection.DataSource= value;
OnPropertyChanged("Server");
}
}
public string User
{
get { return _model.User; }
set
{
_model.User = value;
OnPropertyChanged("User");
}
}
public string Password
{
set { _model.Connection.Credential = new Credential(_model.user, value); }
}
// Syntax varies depending on which MVVM library you are using
public XXXXCommand ConnectCommand
{
get
{
return new XXXXCommand(
canExecute => !Connection.IsConnected,
() => Connection.Connect()
);
}
}
And in your window
<TextBox x:Name="txtbServer" Text="{Binding Server}"/>
<TextBox Text="{Binding User}"/>
<TextBox Text="{Binding Password, Mode=OneWayToSource}"/> <!--Although you would really use a PasswordBox here-->
<Button Content="Connect" Command="{Binding ConnectCommand}"/>
And finally you link the View
and the ViewModel
together by setting the window's DataContext
..
var w = new MainWindow();
w.DataContext = myViewModel;
w.Show();
Good luck.
Upvotes: 1
Reputation: 193
note that it is BAD to store passwords in an unencrypted meaner\ store them at all in your application. you should get the password, pass it ASASP to the server and forget about it. then you should encapsulate the connection in some object and pass it around.
that said, the easiest way to do that is through constructor to the next window.
Upvotes: 0