Wednesday, November 2, 2022

Error The "ResolveComReference" task returned false but did not log an error. - asp.net c# -Microsoft.Office.Interop.Excel.dll

 >Open Visual studio

> Click on Tools

>Click on 'Nuget package manager'

>Click on 'package manager console'

>Run the below command

Install-Package Microsoft.Office.Interop.Excel

reference:- https://stackoverflow.com/questions/10477977/what-reference-do-i-need-to-use-microsoft-office-interop-excel-in-net

Also we have to add Microsoft Office 12.0 Object Library from Com in 'Add reference'

reference:- https://stackoverflow.com/questions/5932794/microsoft-office-core-reference-missing

Also we can download dll(Microsoft.Office.Interop.Excel) file from below link

https://drive.google.com/file/d/15e7KAfD8T6tXShKQZhfpdaQfh_5osU40/view?usp=share_link


Difference between website and webapp

 In website we have codefile in aspx page

example

<%@ Page Title="" Language="C#" MasterPageFile="~/cms.Master" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="cadre_mgmt_system.WebForm2" %>


and in webapp there is codefile property in aspx page



In webapp there is designer file for each aspx page but in website there is no designer file for each aspx page.


For webapp ->When we click on build website, in visual studio-- dll files will be create in debug and bin folders.

Tuesday, November 1, 2022

How to find URL of current website DOMAIN NAME ASP.NET C#

 string Homepage = HttpContext.Current.Request.Url.ToString();

or

string Homepage = WebConfigurationManager.AppSettings["Homepage"].ToString();


web.config


<appSettings>

<add key="Homepage" value="http://pms.icar.gov.in/"/>

</appSettings>


Pleasew see also this:-

reference:- https://kapooryash.blogspot.com/2022/12/how-to-get-host-address-url-localhost.html





How to send mail in ASP.NET C#

 using System.Net.Mail;

using System.Text;


namespace cadre_mgmt_system

{

    public class Email

    {

        public void SendEmail(string To, string Subject, string Body)

        {

            if (To != "" || string.IsNullOrWhiteSpace(To))

            {

                MailMessage mailMessage = new MailMessage();

                mailMessage.From = new MailAddress("support.pms@icar.gov.in");

                mailMessage.To.Add(To);

                mailMessage.Subject = Subject;

                mailMessage.Body = Body;

                mailMessage.IsBodyHtml = true;

                mailMessage.BodyEncoding = Encoding.UTF8;

                mailMessage.Priority = MailPriority.High;

                SmtpClient smtpClient = new SmtpClient();

                smtpClient.Host = "10.100.3.109";

                smtpClient.Port = 25;

                smtpClient.EnableSsl = false;

                smtpClient.Send(mailMessage);

            }

        }

    }

}

Data access layer asp.net C#

 using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Linq;


namespace cadre_mgmt_system

{

    public class DataAccess

    {

        private SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DGMConnCms"].ConnectionString.ToString());


        public DataSet Read(string StoredProcedure, SqlParameter[] param = null)

        {

            SqlCommand sqlCommand = new SqlCommand(StoredProcedure, conn);

            sqlCommand.CommandType = CommandType.StoredProcedure;

            if (param != null)

            {

                sqlCommand.Parameters.AddRange(Enumerable.ToArray<SqlParameter>((IEnumerable<SqlParameter>)param));

            }


            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            DataSet dataSet = new DataSet();

            conn.Open();

            sqlDataAdapter.Fill(dataSet);

            conn.Close();

            return dataSet;

        }


        public DataSet Decrypt(DataSet ds)

        {

            DataSet dataSet = new DataSet();

            dataSet = ds.Clone();

            dataSet.Clear();

            foreach (DataTable table in ds.Tables)

            {

                DataTable dataTable2 = table.Clone();

                dataTable2.Clear();

                foreach (DataRow row in table.Rows)

                {

                    DataRow dataRow2 = dataTable2.NewRow();

                    foreach (DataColumn column in table.Columns)

                    {

                        dataRow2[column.ColumnName] = row[column.ColumnName];

                    }


                    dataTable2.Rows.Add(dataRow2);

                }


                dataSet.Tables.Add(dataTable2);

            }


            return dataSet;

        }


        public DataSet Read(string Query)

        {

            SqlCommand selectCommand = new SqlCommand(Query, conn);

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);

            DataSet dataSet = new DataSet();

            conn.Open();

            sqlDataAdapter.Fill(dataSet);

            conn.Close();

            return dataSet;

        }


        public bool Write(string StoredProcedure, SqlParameter[] param = null)

        {

            try

            {

                new UserLogs().InsertLog(StoredProcedure, param);

            }

            catch

            {

            }


            SqlCommand sqlCommand = new SqlCommand(StoredProcedure, conn);

            sqlCommand.CommandType = CommandType.StoredProcedure;

            if (param != null)

            {

                sqlCommand.Parameters.AddRange(Enumerable.ToArray<SqlParameter>((IEnumerable<SqlParameter>)param));

            }


            conn.Open();

            int num = sqlCommand.ExecuteNonQuery();

            conn.Close();

            if (num == 1)

            {

                return true;

            }


            return false;

        }


        public bool WriteLogs(string StoredProcedure, SqlParameter[] param = null)

        {

            SqlCommand sqlCommand = new SqlCommand(StoredProcedure, conn);

            sqlCommand.CommandType = CommandType.StoredProcedure;

            if (param != null)

            {

                sqlCommand.Parameters.AddRange(Enumerable.ToArray<SqlParameter>((IEnumerable<SqlParameter>)param));

            }


            conn.Open();

            int num = sqlCommand.ExecuteNonQuery();

            conn.Close();

            if (num == 1)

            {

                return true;

            }


            return false;

        }

    }

}