While saving images and important settings/details into the database and accessing it from the database we can define its default path in the Web.config.
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add connectionString="Data Source=myserver;Initial Catalog=demo;User ID=sa;Password=demo@123" name="DBConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="DefaultProfileImage" value="DefaultProfile.png"/>
<add key="BaseAddress" value="http://192.178.12.1/" />
<add key="ProfileImageBaseAddress" value="ProfileImages/" />
<add key="PasswordResetSubject" value="Password Reset - Nudge Energy"/>
<add key="ForgotPasswordLength" value="5"/>
<!-- Mail Setting (Testing) -->
<add key="SenderEmailID" value="demomail@gmail.com" />
<add key="SenderPassword" value="demo@02" />
<add key="EnableSSl" value="true" />
<add key="SenderAddress" value="demo@solutions.com" />
<add key="DisplayName" value="DeoModule" />
<add key="Host" value="smtp.gmail.com" />
<add key="Port" value="587" />
</appSettings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="2147483647">
<readerQuotas
maxDepth="64"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="4096"
maxNameTableCharCount="16384"/>
</binding>
</basicHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="NudgeDemoAPI.NudgeServiceBehavior"
name="NudgeDemoAPI.NudgeService">
<endpoint behaviorConfiguration="webBehavior" binding="webHttpBinding"
contract="NudgeDemoAPI.INudgeService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="NudgeDemoAPI.NudgeServiceBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
And we can access it while coding like this
public EmployeeResponse RegisterEmployee(EmployeeRequest empobj)
{
try
{
int result = 0;
string accessToken = Guid.NewGuid().ToString();
empResponse = new EmployeeResponse();
using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
{
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
SqlCommand cmd = new SqlCommand("uspRegisterUser", sqlConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = empobj.FirstName;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 50).Value = empobj.LastName;
cmd.Parameters.Add("@ProfileImage", SqlDbType.VarChar, 500).Value = ConfigurationManager.AppSettings["ProfileImageBaseAddress"].ToString();
cmd.Parameters.Add("@JobRoleID", SqlDbType.BigInt).Value = empobj.JobRoleID;
cmd.Parameters.Add("@Email", SqlDbType.VarChar, 200).Value = empobj.Email;
cmd.Parameters.Add("@CompanyID", SqlDbType.Int).Value = empobj.CompanyId;
cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = empobj.Password;
cmd.Parameters.Add("@PasswordSalt", SqlDbType.VarChar, 100).Value = Guid.NewGuid().ToString();
cmd.Parameters.Add("@AccessToken", SqlDbType.VarChar, 100).Value = accessToken;
cmd.Parameters.Add("@DeviceUUID", SqlDbType.VarChar, 50).Value = empobj.DeviceUUID;
cmd.Parameters.Add("@DeviceType", SqlDbType.VarChar, 50).Value = empobj.DeviceType;
cmd.Parameters.Add("@CreatedDate", SqlDbType.VarChar, 50).Value = System.DateTime.Now;
cmd.Parameters.Add("@IsDeleted", SqlDbType.VarChar, 50).Value = empobj.IsDeleted;
cmd.Parameters.Add("@DeletedDate", SqlDbType.Date).Value = System.DateTime.Now;
result = Convert.ToInt32(cmd.ExecuteNonQuery());
switch (result)
{
case 1:
empResponse.Status = true;
empResponse.Message = "User successfully registered.";
empobj.AccessToken = accessToken;
break;
case 2:
empResponse.Status = false;
empResponse.Message = "Email already exists.";
break;
default:
empResponse.Status = false;
empResponse.Message = "Registration failed.";
break;
}
}
}
catch (SqlException ex)
{
Logger.LogException(ex);
empResponse.Status = false;
empResponse.Message = ex.Message;
}
return empResponse;
}
0 Comment(s)