Monday, July 14, 2014

Creating C# Class Library (DLL) Using Visual Studio .NET

Step 1:- File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory click OK
After Clicking on button ‘OK’, solution explorer adds one C# class ‘Class1.cs’. In this class we can write our code.


When we double click on Class1.cs, we see a namespace CreatingDLL. We will be use this namespace in our project to access this class library.

Step 2:- Within Class1.cs we create a method named ‘sum’ that takes two integers value and return sum to witch method passed numbers.
using System;
namespace CreatingDLL
{
    public class Class1
    {
        /// <summary>
        /// sum is method that take two integer value and return that sum
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int sum(int x, int y)
        {
            return x + y;
        }
     }
}

Step 3:- Now build the Application and see bin\debug directory of our project. ‘CreatingDLL.dll’ is created.
Now we create another application and take this DLL (CreatingDLL.dll) reference for accessing DLL’s method.

Steps for accessing created DLL
Step 4:- File->New->Project->Visual C# Projects->Windows Form Application.
Step 5:- Designed windows form as bellow figure.
Step 6:- Add reference of DLL (CreatingDLL) which we created before few minutes.
After adding reference of DLL, following windows will appear.

Step 7:- Write code on button click of Windows Form Application. Before creating object and making method of Add DLL, add namespace CreatedDLLin project as bellow code.
using System;
using System.Windows.Forms;
using CreatingDLL;
namespace AccessingDLL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Class1 c1 = new Class1();
            try
            {
                txtResult.Text =Convert.ToString(c1.sum(Convert.ToInt32(txtNumber1.Text),Convert.ToInt32(txtNumber2.Text)));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

}

Step 8:- Now build the application and execute project and see output.

Inserted data to Creating Xml Tag

Add Xml File And Create Root Tag like <bookstore>  </bookstore>
Xml Name like BookStore.xml

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <table style="width: 100%;">
            <tr>
                <td class="auto-style1">
                    Title:
                </td>
                <td>
                    <asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Author:
                </td>
                <td>
                    <asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Year:
                </td>
                <td>
                    <asp:TextBox ID="tbYear" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Price:
                </td>
                <td>
                    <asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
        <asp:GridView ID="gvBookStoreRecords" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        XmlDocument XmlDocObj = new XmlDocument();
        XmlDocObj.Load(Server.MapPath("BookStore.xml"));
        XmlNode RootNode = XmlDocObj.SelectSingleNode("bookstore");
        XmlNode bookNode = RootNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "book", ""));
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Title", "")).InnerText = tbTitle.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Author", "")).InnerText = tbAuthor.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Year", "")).InnerText = tbYear.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Price", "")).InnerText = tbPrice.Text;

        XmlDocObj.Save(Server.MapPath("BookStore.xml"));

        gridDataBind();

    }
    public void gridDataBind()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("BookStore.xml"));
        gvBookStoreRecords.DataSource = ds;
        gvBookStoreRecords.DataBind();
    }
}





Sunday, July 13, 2014

How to Insert data using after trigger... SubString


create database mydata121
 CREATE TABLE CONCATE
(
Empid varchar(50)
,Ename varchar(50)
,Salary money,
associatID numeric
)

insert into CONCATE(Empid,Ename,Salary)
values('101','Ashok',25000)
insert into CONCATE(Empid,Ename,Salary)
values('202','shaarth',60000)

select * from CONCATE

select SUBSTRING(empid,1,1)+'_'+SUBSTRING(ename,1,1)+'_'+
SUBSTRING(cast(Salary as varchar(50)),1,2) as Associate,
Empid,Ename,Salary from CONCATE



CREATE TRIGGER AFTERINSERT ON CONCATE AFTER INSERT
AS
BEGIN
DECLARE @EMPID VARCHAR(50)
DECLARE @ENAME VARCHAR(50)
DECLARE @SALARY VARCHAR(50)


SELECT @EMPID= i.empid FROM inserted i
select @ENAME=i.ename from inserted i
select @SALARY=i.Salary from inserted i


DECLARE @SUBSTRING VARCHAR(50)=SUBSTRING(@EMPID,1,1)+'_'+SUBSTRING(@ENAME,1,1)+'_'+
SUBSTRING(cast(@SALARY as varchar(50)),1,2)


update concate set associatID=@SUBSTRING where empid=@EMPID


END

GO

Sending Mail

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    TO: <asp:TextBox ID="txtto" runat="server"></asp:TextBox>
        <br />
        Sub: <asp:TextBox ID="txtsub" runat="server"></asp:TextBox>
        <br />
        Body:<asp:TextBox ID="txtbody" runat="server" TextMode="MultiLine"></asp:TextBox>
        <br />
    <asp:Button ID="btnmail" runat="server" Text="Send" onclick="btnmail_Click" />
    </div>
    </form>
</body>
</html>

-------------------------------------------------
.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;
using System.Net.Mail;

public partial class Default2 : System.Web.UI.Page
{

    string gmailusername = System.Configuration.ConfigurationManager.AppSettings["Gmailusername"].ToString();
    string pwd = System.Configuration.ConfigurationManager.AppSettings["Password"].ToString();
    string port = System.Configuration.ConfigurationManager.AppSettings["Port"].ToString();
    string protocval = System.Configuration.ConfigurationManager.AppSettings["protocal"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {

    }

   
    protected void btnmail_Click(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage newmsg = new System.Net.Mail.MailMessage();

        newmsg.From = new MailAddress(gmailusername);

            newmsg.Subject = txtsub.Text;
            newmsg.To.Add(txtto.Text);
            newmsg.Body = txtbody.Text;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = protocval;
            smtp.Port = Convert.ToInt32(port);
            smtp.Credentials = new System.Net.NetworkCredential(gmailusername,pwd);

            smtp.EnableSsl = true;
            smtp.Send(newmsg);
            Response.Write("msg is send");

      
    }
}
===============================================================
Web.config

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>

    <add name="myconnreader" connectionString="Data Source=KCLINK-PC\SQLEXPRESS;Initial Catalog=mydataworld;Integrated Security=True"/>
  </connectionStrings>

  <appSettings>
    <add key="Gmailusername" value="x@gmail.com"/>
    <add key="Password" value="xx"/>
    <add key="Port" value="587"/>
    <add key="protocal" value="smtp.gmail.com"/>
   
  </appSettings>
     <system.web>
           <compilation debug="true" targetFramework="4.0"/>
     </system.web>
</configuration>
================================================================
Note: Where X is Ur USERNAME
Where XX is Ur PASSWORD