Thursday, January 9, 2020

Publishing node_modules in .Net Core

How to automate the npm install, when you publish your .net core project,
and make sure the publish operation include the node_modules folder created

please follow the next steps to do this solution


  1. double click on the project or right click on the project and select edit, you will open .csproj file, you will find xml data
  2. exclude the node_modules from your project
  3. remove the ItemGroup which contains the reference the node_modules files
  4. write the following code
    <Target Name="PostBuild" AfterTargets="ComputeFilesToPublish">
    <Exec Command="npm install" />
    </Target>
         <ItemGroup>
    <Content Include="node_modules/**" CopyToPublishDirectory="PreserveNewest" />
    </ItemGroup>
  5. finally run the publish command "dotnet publish -o c:\temp\publish"






Sunday, August 11, 2013

How to write Data In XML with some attributes

                string dir = HttpContext.Current.Server.MapPath("~/Errors/");
                string FileName = HttpContext.Current.Server.MapPath("~/Errors/AKAARERRORFILE.xml");

                string date = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" +                     DateTime.Now.Year.ToString();
                XmlTextReader reader = new XmlTextReader(FileName);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();

                XmlElement parentelement = doc.CreateElement("Date");
                XmlAttribute dateatt=doc.CreateAttribute("date");
                dateatt.Value = date;
                parentelement.Attributes.Append(dateatt);
                XmlElement thepage = doc.CreateElement("page");
                thepage.InnerText = page;
                XmlElement thefunction = doc.CreateElement("fun");
                thefunction.InnerText = function;
                XmlElement thedes = doc.CreateElement("des");
                thedes.InnerText = description;

                parentelement.AppendChild(thepage);
                parentelement.AppendChild(thefunction);
                parentelement.AppendChild(thedes);
                doc.DocumentElement.AppendChild(parentelement);

                doc.Save(FileName);

               Download Example

Tuesday, July 30, 2013

Monday, June 17, 2013

How to Backup and Restore .bak file using C# 2010

references :
http://www.geekpedia.com/tutorial180_Backup-and-restore-SQL-databases.html
http://www.codeproject.com/Articles/123441/SQL-Server-2008-Backup-and-Restore-Databases-using
http://www.c-sharpcorner.com/uploadfile/rohatash/restore-sql-server-backup-file-with-c-sharp/
http://manish4dotnet.blogspot.com/2013/03/RestoreDataBaseusingCSharp.html
......................................................................................................................................................

//Using namespaces :
// these namespace found in "c:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\"
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.SqlEnum;
using System.Collections.Specialized;


                 ServerConnection srvConn = new ServerConnection(Environment.MachineName);
                //my SQL user doesnt have sufficient permissions,
                //so i am using my windows account
                srvConn.LoginSecure = true;
                //connection.LoginSecure = false;
                //connection.Login = "testuser";
                
                //connection.Password = "testuser";
             


  public static void RestoreDB(string backUpFilePath, string databaseName)
        {
            try
            {
                Restore restore = new Restore();
                //Set type of backup to be performed to database
                restore.Database = databaseName;
                restore.Action = RestoreActionType.Database;

                // Replace the Db if already exists
                restore.ReplaceDatabase = true;

                //Set up the backup device to use filesystem.         
                restore.Devices.AddDevice(backUpFilePath, DeviceType.File);
                //set ReplaceDatabase = true to create new database
                //regardless of the existence of specified database
                restore.ReplaceDatabase = true;
                //If you have a differential or log restore to be followed,
                //you would specify NoRecovery = true
                restore.NoRecovery = false;
                Server sqlServer = new Server(srvConn);
                //SqlRestore method starts to restore database           
                restore.SqlRestore(sqlServer);
                MessageBox.Show("Restore operation succeeded");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Restore operation failed");
                MessageBox.Show(ex.Message);
            }
            Console.ReadLine();
        }
===============================
AND to call this Method :

RestoreDB(@"C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\SmallStoreme.bak", "SmallStore");

===============================
  public static void BackUpDB(string databaseName)
        {
                 // If there was a SQL connection created
                  // Create a new backup operation
                    Backup bkpDatabase = new Backup();
                    // Set the backup type to a database backup
                    bkpDatabase.Action = BackupActionType.Database;
                    // Set the database that we want to perform a backup on
                    bkpDatabase.Database =databaseName;

                    // Set the backup device to a file
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName,    
                           DeviceType.File);
                    // Add the backup device to the backup
                    bkpDatabase.Devices.Add(bkpDevice);
                    // Perform the backup

                    bkpDatabase.SqlBackup(srvSql);
         }
===============================
AND to call this Method :

BackUpDB("SmallStore");

===============================

Monday, May 6, 2013

Make Combobox in C# Winforms Autocomplete

               // This Code is working properly
               // Just Copy and paste it 


                AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
                foreach (DataRow row in MyDatatable.Rows)
                {
                    //looping through the datatable and adding values from datatable into autocomplete collection
                    ac.Add(row["CustName"].ToString());
                }
                comboCustomerName.AutoCompleteSource = AutoCompleteSource.CustomSource; //assigning  
                // autocomplete source to the combobox
                comboCustomerName.AutoCompleteCustomSource = ac;

Monday, April 22, 2013

How To get The Focused Control


Import namespaces


using System.Runtime.InteropServices;
using System.Windows.Forms;


Win API

[DllImport("user32.dll")]
static extern IntPtr GetFocus();

Use

Control focused = null;
IntPtr handle = GetFocus();
if (handle != IntPtr.Zero)
{
    focused = Control.FromHandle(handle);
}
messageBox(focused.Name);

Tuesday, July 3, 2012

.NET N-Tier Architecture


1- Business Logic Layer 
    it is a class 
    we have table in database called Branch with columns(pk_branch_id,mobile,branch_name,address,notes)
    so for each column or field we create property to set and get values


public class BusinessLogicLayer
{
    #region Variables

    private int pk_branch_id, mobile;
    private string branch_name, address, notes;

    #endregion

    #region Properties

    public int Branch_ID
    {
        get { return pk_branch_id; }
        set { pk_branch_id = value; }
    }

    public string Branch_Name
    {
        get { return branch_name; }
        set { branch_name = value; }
    }

    public string Branch_Address
    {
        get { return address; }
        set { address = value; }
    }
    public int Branch_Mobile
    {
        get { return mobile; }
        set { mobile = value; }
    }

    public string Branch_Notes
    {
        get { return notes; }
        set { notes = value; }
    }

    #endregion


2 - Data Access Layer
      it is a class
      which is responsible for creating connection , open, close it, create command and execute it

public class DataAccessLayer
{
    SqlConnection conn;
    SqlCommand cmd;
    public DataAccessLayer() // Constructor
    {
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ADSL_DBConnectionString"].ConnectionString);
        conn.Open();
    }

    public bool Insert_Branch(BusinessLogicLayer bll_branch_INS)
    {
        using (cmd = new SqlCommand("PRC_INSERT_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.AddWithValue("@Branch_Name", bll_branch_INS.Branch_Name);
            cmd.Parameters.AddWithValue("@Mobile", bll_branch_INS.Branch_Mobile);
            cmd.Parameters.AddWithValue("@Address", bll_branch_INS.Branch_Address);
            cmd.Parameters.AddWithValue("@Notes", bll_branch_INS.Branch_Notes);
            int nn = cmd.ExecuteNonQuery();
            if (nn > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public bool Update_Branch( BusinessLogicLayer  bll_branch_UPD)
    {
        using (cmd = new SqlCommand("PRC_UPDATE_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", bll_branch_UPD.Branch_ID);

            cmd.Parameters.AddWithValue("@Branch_Name", bll_branch_UPD.Branch_Name);
            cmd.Parameters.AddWithValue("@Mobile", bll_branch_UPD.Branch_Mobile);
            cmd.Parameters.AddWithValue("@Address", bll_branch_UPD.Branch_Address);
            cmd.Parameters.AddWithValue("@Notes", bll_branch_UPD.Branch_Notes);
            
            if (cmd.ExecuteNonQuery() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public Boolean Delete_Branch(int id)
    {
        using (cmd = new SqlCommand("PRC_DELETE_BRANCH", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", id);

            if (cmd.ExecuteNonQuery() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public void Select_Branch_BYID(int id)
    {
        using (cmd = new SqlCommand("PRC_SELECTBRANCH_BYID", conn))
        {
            BusinessLogicLayer  bll_branch = new BusinessLogicLayer();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Pk_Branch_Id", id);
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            if (rdr.Read())
            {
            bll_branch.Branch_Name = rdr["Branch_Name"].ToString();
            bll_branch.Branch_Mobile = Convert.ToInt32(rdr["Mobile"]);
            bll_branch.Branch_Address = rdr["Address"].ToString();
            bll_branch.Branch_Notes = rdr["Notes"].ToString();
            }
        }
    }

    public void Fill_GridView(GridView GV)
    {
        using (cmd = new SqlCommand("PRC_FILL_GV", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            SqlDataAdapter adpr = new SqlDataAdapter(cmd);
            adpr.Fill(dt);
            GV.DataSource = dt;
            GV.DataBind();
        }
    }

    public void Fill_DropDownList(DropDownList DDL, string datatext, string datavalue)
    {
        using (cmd = new SqlCommand("PRC_FILL_DDR", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdr;
            rdr = cmd.ExecuteReader();
            DDL.DataSource = rdr;
            DDL.DataTextField = datatext;
            DDL.DataValueField = datavalue;
            DDL.DataBind();
        }
    }

}

- MultiView



Multi View (Source Code ASP.NET)

    <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
        <asp:View ID="View1" runat="server">
            <table>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblNameINS" runat="server" Text="User Name "></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtNameINS" runat="server" Width="200"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAddINS" runat="server" Text="User Address "></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAddINS" runat="server" Width="300"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAgeINS" runat="server" Text="User Age "></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAgeINS" runat="server" Width="100"></asp:TextBox>
                    </td>
                    <td width="200" align="right"><asp:LinkButton runat="server" ID="lnkbtnINS" 
                            Text="Go to Update" onclick="lnkbtn_Click"></asp:LinkButton></td>
                </tr>
            </table>
        </asp:View>
        <asp:View ID="View2" runat="server">
            <table>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblNameUPD" runat="server" Text="User Name U"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtNameUPD" runat="server" Width="200"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAddUPD" runat="server" Text="User Address U"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAddUPD" runat="server" Width="300"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td width="200" align="center">
                        <asp:Label ID="lblAgeUPD" runat="server" Text="User Age U"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtAgeUPD" runat="server" Width="100"></asp:TextBox>
                    </td>
                
                <td width="200" align="right">
                    <asp:LinkButton runat="server" ID="lnkbtnUPD" 
                            Text="Go to Insert" onclick="lnkbtnUPD_Click"></asp:LinkButton></td>
                
                </tr>
                
            </table>
        </asp:View>
     
    </asp:MultiView>

 protected void lnkbtn_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }
    protected void lnkbtnUPD_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }

Sunday, July 1, 2012

- SQL


SELECT MAX (stdDegree)FROM dbo.STD WHERE stdDegree IN
(SELECT DISTINCT TOP 2 stdDegree FROM dbo.STD 
ORDER BY stdDegree DESC)