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

C#
string _serverSelected;
        public string serverSelected
        {
            get { return _serverSelected; }
            set
            {
                if (_serverSelected != value)
                    _serverSelected = value;

                RaisePropertyChanged("serverSelected");
            }
        }

        public BackUpViewModel()
        {

             BackUpContext servObj = new BackUpContext();
            _DBNameList = servObj.GetDBNames(serverSelected);
        }



DAL :

XML
public List<string> GetDBNames(string sqlServerName)
        {
    List<string> lstDBName = new List<string>();


                SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();
                connection.DataSource = sqlServerName;
                connection.IntegratedSecurity = true;
                String strCon = connection.ToString();
                SqlConnection sqlConn = new SqlConnection(strCon);
                sqlConn.Open();

                SqlCommand cmd = new SqlCommand("select * from sys.databases", sqlConn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    lstDBName.Add(dr[0].ToString());
                }
                sqlConn.Close();


                return lstDBName;
}



AppConfig :

XML
<configuration>
  <connectionStrings>

    <add name="CostAllocationEntities2" connectionString="Data Source=pc210090\SQLExpress;Persist Security Info=True;User ID=sa;Password=password1$" providerName="System.Data.SqlClient"/>
    </connectionStrings>

  <appSettings>

    <add key="ServerName1" value="PC210090\SQLEXPRESS"/>
    <add key="ServerName2" value="s2"/>
    <add key="ServerName3" value="s3"/>
    <add key="BackupDestinationPath" value="D:\BackUpDatabase\"/>
    <add key="RestoreDestinationPath" value="D:\RestoreDatabase\"/>

  </appSettings>
</configuration>



Quote:
_DBNameList = servObj.GetDBNames(serverSelected); this serverSelected is always passing as null which is not allowing my code to build and i am not able to select server from server list because of this.
Posted
Updated 3-Nov-15 19:55pm
v2
Comments
Sinisa Hajnal 4-Nov-15 2:44am    
And where are you setting the property?
Member 12002095 4-Nov-15 3:03am    
In the view model..
string _serverSelected;
public string serverSelected
{
get { return _serverSelected; }
set
{
if (_serverSelected != value)
_serverSelected = value;

RaisePropertyChanged("serverSelected");
}
}
Sinisa Hajnal 4-Nov-15 6:07am    
That is definition of the property. Not setting it. You need a line somewhere that says serverSelected = My.Settings.MyServerSetting (this is pseudocode, don't just copy-paste it)
Member 12002095 4-Nov-15 6:35am    
public void fetchServer(object server)
{
serverSelected = server.ToString();
}
Sinisa Hajnal 4-Nov-15 9:26am    
Where are you calling this? This is yet again a method that sets the property and does nothing else (and as such, you can remove it and use the property) - but the code you posted doesn't call it anywhere.

1 solution

Hi, In your code, you did not mention the database name, just mention it like this,

C#
connection.InitialCatalog = "YourDatabaseNameHere";
 
Share this answer
 
Comments
Member 12002095 4-Nov-15 4:06am    
I did, but nothing got changed...same exception.

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