Monday, July 14, 2014

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

Thursday, July 10, 2014

Difference Between String And String Builder?

string 

String is immutable. Immutable means once we create string object we cannot modify. Any operation like insert, replace or append happened to change string simply it will discard the old value and it will create new instance in memory to hold the new value.


string str = "hi";
// create a new string instance instead of changing the old one
str += "test";
str += "help";

String Builder

String builder is mutable it means once we create string builder object we can perform any operation like insert, replace or append without creating new instance for every time.


StringBuilder sb = new StringBuilder("");

sb.Append("hi");
sb.Append("test ");
string str = sb.ToString();


Differences


String
StringBuilder
It’s an immutable
It’s mutable
Performance wise string is slow because every time it will create new instance
Performance wise stringbuilder is high because it will use same instance of object to perform any action
In string we don’t have append keyword
In StringBuilder we can use append keyword
String belongs to System namespace
Stringbuilder belongs to System.Text namespace

What is Serialization

Serialization:

It Is The Process of Converting Complex Objects into Stream Of Bytes For Storage.


Advantage:

Using serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent storage medium in a non-proprietary format.

Serialization is used by Remoting, Web Services SOAP for transmitting data between a server and a client. The Remoting technology of .NET makes use of serialization to pass objects by value from one application domain to another.

De-serialization is the reverse; it is the process of reconstructing the same object later.


Types of Serialization

Serialization can be of the following types:

1.Binary Serialization

2.SOAP Serialization

3.XML Serialization



Binary Serialization:


Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically. The term binary in its name implies that the necessary information that is required to create an exact binary copy of the object is saved onto the storage media.

Difference between Binary serialization and XML serialization is that Binary serialization preserves instance identity while XML serialization does not. In other words, in Binary serialization the entire object state is saved while in XML serialization only some of the object data is saved. 

Binary serialization can handle graphs with multiple references to the same object; XML serialization will turn each reference into a reference to a unique object

SOAP Serialization:


The SOAP protocol is ideal for communicating between applications that use heterogeneous architectures. In order to use SOAP serialization in .NET we have to add a reference to System.Runtime.Serialization.Formatters.Soap in the application. The basic advantage of SOAP serialization is portability. The SoapFormatter serializes objects into SOAP messages or parses SOAP messages and extracts serialized objects from the message. 


XML Serialization:


· According to MSDN, "XML serialization converts (serializes) the public fields and properties of an object or the parameters and returns values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document.



· XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport. Because XML is an open standard, the XML stream can be processed by any application, as needed, regardless of platform." Implementing XML Serialization in .Net is quite simple.