thenna
thenna

Reputation: 421

how to change connection string db value app setting to web config

currently MVC project.previous developer database connected app settings now i change to Web config

i have changed code Like this appsettings to webconfig

<appSettings>
<add key="TickPortalSql" value="Server=1.34.34.4.;Database=montage;User ID=Montage;Password=pwd;Trusted_Connection=False;" />
</appSettings>

this code change to web config

<connectionStrings>

      <add key="TickPortalSql" value="Server=1.34.34.4.;Database=montage;User ID=Montage;Password=pwd;Trusted_Connection=False;" />
  </connectionStrings>

Previous developer using AppSettingsReader

public abstract class HomeSqlDBRepository
    {
        public string ConnectionString { get; set; } = AppSettingsReader.ReadString("thenna");
        const int MaxItemCount = 1000;
        public enum SqlExecuteType
        {
            Scalar, NonQuery, Reader
        }
        public async Task<string> ExecuteSqlAsync(string procname, SqlExecuteType executiontype, IDictionary<string, string> parameters)
        {
            string ret = "";

            using (SqlConnection sqlConn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand sqlCmd = new SqlCommand(procname, sqlConn))
                {
                    sqlCmd.CommandType = CommandType.StoredProcedure;
                    foreach (KeyValuePair<string, string> kvp in parameters)
                    {
                        sqlCmd.Parameters.AddWithValue(kvp.Key, kvp.Value);
                    }
                    sqlConn.Open();
                    if (executiontype == SqlExecuteType.Scalar)
                        ret = (await sqlCmd.ExecuteScalarAsync()).ToString();
                    else if (executiontype == SqlExecuteType.NonQuery)
                        ret = (await sqlCmd.ExecuteNonQueryAsync()).ToString();
                    sqlConn.Close();
                }
            }

            return ret;
        }

but i getting error ex exception here code

using Microsoft.Azure;

  public static class AppSettingsReader
    {
        public static string ReadString(string key)
        {
            try
            {
                return CloudConfigurationManager.GetSetting(key).ToString();
            }
            catch(System.Exception ex)
            {
                throw ex;//error 
            }
        }

    public static int ReadInt(string key)
    {
        try
        {
            return int.Parse(CloudConfigurationManager.GetSetting(key).ToString());
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }

    public static decimal ReadDecimal(string key)
    {
        try
        {
            return decimal.Parse(CloudConfigurationManager.GetSetting(key).ToString());
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }

    public static bool ReadBool(string key)
    {
        try
        {
            return bool.Parse(CloudConfigurationManager.GetSetting(key).ToString());
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
}

any one tell how to change azure app settings read db to web config db read? i have tired many method getting Error ..

Upvotes: 0

Views: 492

Answers (1)

Aaron Baker
Aaron Baker

Reputation: 76

Change web.config to:

<connectionStrings>
    <add name="TickPortalSql"
         value="Server=1.34.34.4.;Database=montage;UserID=Montage;Password=pwd;Trusted_Connection=False;" />
</connectionStrings>

And to access the string

var connectionString  = ConfigurationManager.ConnectionStrings["TickPortalSql"].ConnectionString;

Source: https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings(v=vs.110).aspx

Upvotes: 1

Related Questions