SERVICE1.ASMX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DAO;
namespace webservicetask123
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
Sample_Connection con = new Sample_Connection();
string _connectionstring = null;
SqlParameter[] _parameterset = null;
public Service1()
{
_connectionstring = ConfigurationManager.ConnectionStrings["Developer"].ToString();
con = new Sample_Connection(_connectionstring);
}
[WebMethod]
public DataTable LoadEmp()
{
DataSet ds = SqlHelper.ExecuteDataset(_connectionstring, CommandType.StoredProcedure, "[practicegd]");
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
[WebMethod]
public int insert(Details _objdetails)
{
_parameterset = SqlParameterCache.GetSpParameterSet(_connectionstring, "[gdinsert]");
_parameterset[0].Value = _objdetails.EmpName;
_parameterset[1].Value = _objdetails.EmpSal;
_parameterset[2].Value = _objdetails.Address;
_parameterset[3].Value = _objdetails.Email;
int i = SqlHelper.ExecuteNonQuery(_connectionstring, CommandType.StoredProcedure, "[gdinsert]", _parameterset);
return i;
}
[WebMethod]
public int update(Details _objdetails)
{
_parameterset = SqlParameterCache.GetSpParameterSet(_connectionstring, "[gdupdate]");
_parameterset[0].Value = _objdetails.EmpId;
_parameterset[1].Value = _objdetails.EmpName;
_parameterset[2].Value = _objdetails.EmpSal;
_parameterset[3].Value = _objdetails.Address;
_parameterset[4].Value = _objdetails.Email;
int i = SqlHelper.ExecuteNonQuery(_connectionstring, CommandType.StoredProcedure, "[gdupdate]", _parameterset);
return i;
}
[WebMethod]
public int delete(int EmpId)
{
_parameterset = SqlParameterCache.GetSpParameterSet(_connectionstring, "[gddelete]");
_parameterset[0].Value = EmpId;
int i = SqlHelper.ExecuteNonQuery(_connectionstring, CommandType.StoredProcedure, "[gddelete]", _parameterset);
return i;
}
}
[Serializable]
public class Details
{
int _EmpId;
string _EmpName;
string _EmpSal;
string _Address;
string _Email;
public int EmpId
{
set { _EmpId = value; }
get { return _EmpId; }
}
public string EmpName
{
set { _EmpName = value; }
get { return _EmpName; }
}
public string EmpSal
{
set { _EmpSal = value; }
get { return _EmpSal; }
}
public string Address
{
set { _Address = value; }
get { return _Address; }
}
public string Email
{
set { _Email = value; }
get { return _Email; }
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DAO;
namespace webservicetask123
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
Sample_Connection con = new Sample_Connection();
string _connectionstring = null;
SqlParameter[] _parameterset = null;
public Service1()
{
_connectionstring = ConfigurationManager.ConnectionStrings["Developer"].ToString();
con = new Sample_Connection(_connectionstring);
}
[WebMethod]
public DataTable LoadEmp()
{
DataSet ds = SqlHelper.ExecuteDataset(_connectionstring, CommandType.StoredProcedure, "[practicegd]");
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
[WebMethod]
public int insert(Details _objdetails)
{
_parameterset = SqlParameterCache.GetSpParameterSet(_connectionstring, "[gdinsert]");
_parameterset[0].Value = _objdetails.EmpName;
_parameterset[1].Value = _objdetails.EmpSal;
_parameterset[2].Value = _objdetails.Address;
_parameterset[3].Value = _objdetails.Email;
int i = SqlHelper.ExecuteNonQuery(_connectionstring, CommandType.StoredProcedure, "[gdinsert]", _parameterset);
return i;
}
[WebMethod]
public int update(Details _objdetails)
{
_parameterset = SqlParameterCache.GetSpParameterSet(_connectionstring, "[gdupdate]");
_parameterset[0].Value = _objdetails.EmpId;
_parameterset[1].Value = _objdetails.EmpName;
_parameterset[2].Value = _objdetails.EmpSal;
_parameterset[3].Value = _objdetails.Address;
_parameterset[4].Value = _objdetails.Email;
int i = SqlHelper.ExecuteNonQuery(_connectionstring, CommandType.StoredProcedure, "[gdupdate]", _parameterset);
return i;
}
[WebMethod]
public int delete(int EmpId)
{
_parameterset = SqlParameterCache.GetSpParameterSet(_connectionstring, "[gddelete]");
_parameterset[0].Value = EmpId;
int i = SqlHelper.ExecuteNonQuery(_connectionstring, CommandType.StoredProcedure, "[gddelete]", _parameterset);
return i;
}
}
[Serializable]
public class Details
{
int _EmpId;
string _EmpName;
string _EmpSal;
string _Address;
string _Email;
public int EmpId
{
set { _EmpId = value; }
get { return _EmpId; }
}
public string EmpName
{
set { _EmpName = value; }
get { return _EmpName; }
}
public string EmpSal
{
set { _EmpSal = value; }
get { return _EmpSal; }
}
public string Address
{
set { _Address = value; }
get { return _Address; }
}
public string Email
{
set { _Email = value; }
get { return _Email; }
}
}
}
AND DAO LAYER.....
CONSUMING
Default Source
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="webservicetaskconsuming._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>
<asp:GridView ID="GridView1" runat="server" DataKeyNames="EmpId" ShowFooter="true"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" Width="82px"
AutoGenerateColumns="false" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" >
<Columns>
<asp:BoundField DataField="EmpId" HeaderText="EmpId" ReadOnly="true" />
<asp:TemplateField HeaderText="EmpName">
<ItemTemplate>
<%#Eval("EmpName") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpName" runat="server" Text='<%#Eval("EmpName")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmpSal">
<ItemTemplate>
<%#Eval("EmpSal") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpSal" runat="server" Text='<%#Eval("EmpSal")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterSal" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<%#Eval("Address") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAddress" runat="server" Text='<%#Eval("Address")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterAddress" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<%#Eval("Email") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFooterEmail" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Insert">
<FooterTemplate>
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btninsert"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" />
<br />
<asp:GridView ID="GridView2" runat="server"
>
</asp:GridView>
U can send these details to ur mail<br />
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</form>
</body>
</html>
DEFAULT.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.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.IO;
namespace webservicetaskconsuming
{
public partial class _Default : System.Web.UI.Page
{
santhosh.Service1 _objgrid = new santhosh.Service1();
santhosh.Details _objdetails = new santhosh.Details();
DataTable _dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Focus();
gridbind();
ViewState["sample"] = (DataTable)GridView1.DataSource;
}
}
public void gridbind()
{
_dt = _objgrid.LoadEmp();
GridView1.DataSource = _dt;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
GridView1.EditIndex = -1;
gridbind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gridbind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtEmpName = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtEmpName"));
TextBox txtEmpSal = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtEmpSal"));
TextBox txtAddress = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtAddress"));
TextBox txtEmail = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtEmail"));
_objdetails.EmpId = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
_objdetails.EmpName = txtEmpName.Text;
_objdetails.EmpSal = txtEmpSal.Text;
_objdetails.Address = txtAddress.Text;
_objdetails.Email = txtEmail.Text;
int i = _objgrid.update(_objdetails);
if (i > 0)
{
lblMsg.Text = "UpDated Sucessfully";
}
else
{
lblMsg.Text = "Updation Falied";
}
GridView1.EditIndex = -1;
gridbind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int EmpId = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
int i = _objgrid.delete(EmpId);
if (i > 0)
{
lblMsg.Text = "deleted Successfully";
}
else
{
lblMsg.Text = "deletion failed";
}
GridView1.EditIndex = -1;
gridbind();
}
protected void btninsert(object sender, EventArgs e)
{
TextBox txtFooterName = (TextBox)(GridView1.FooterRow.FindControl("txtFooterName"));
TextBox txtFooterSal = (TextBox)(GridView1.FooterRow.FindControl("txtFooterSal"));
TextBox txtFooterAddress = (TextBox)(GridView1.FooterRow.FindControl("txtFooterAddress"));
TextBox txtFooterEmail = (TextBox)(GridView1.FooterRow.FindControl("txtFooterEmail"));
_objdetails.EmpName = txtFooterName.Text;
_objdetails.EmpSal = txtFooterSal.Text;
_objdetails.Address = txtFooterAddress.Text;
_objdetails.Email = txtFooterEmail.Text;
int i = _objgrid.insert(_objdetails);
if (i > 0)
{
lblMsg.Text = "Inserted Successfully";
}
else
{
lblMsg.Text = "Insertion failed";
}
GridView1.EditIndex = -1;
gridbind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
_dt = (DataTable)ViewState["sample"];
//_dt = _objgrid.LoadEmp();
GridView2.DataSource = _dt;
GridView2.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string to =TextBox1.Text;
string From = "rajshekarsanthosh@gmail.com";
string subject = "Required info";
string Body = "sir ,<br> Got the Output <br><br>";
Body += GridViewToHtml(GridView2); //Elaborate this function detail later
Body += "<br><br>Regards,<br>Santhosh";
bool send = send_mail(to, From, subject, Body);//Elaborate this function detail later
if (send == true)
{
string CloseWindow = "alert('Mail Sent Successfully!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
else
{
string CloseWindow = "alert('Problem in Sending mail...try later!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
}
public bool send_mail(string to, string from, string subject, string body)
{
MailMessage msg = new MailMessage(from, to);
msg.Subject = subject;
AlternateView view;
SmtpClient client;
StringBuilder msgText = new StringBuilder();
msgText.Append(" <html><body><br></body></html> <br><br><br> " + body);
view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html");
msg.AlternateViews.Add(view);
client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("rajshekarsanthosh@gmail.com", "raj123456");
client.EnableSsl = true; //Gmail works on Server Secured Layer
client.Send(msg);
bool k = true;
return k;
}
private string GridViewToHtml(GridView gv)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
gv.RenderControl(hw);
return sb.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.IO;
namespace webservicetaskconsuming
{
public partial class _Default : System.Web.UI.Page
{
santhosh.Service1 _objgrid = new santhosh.Service1();
santhosh.Details _objdetails = new santhosh.Details();
DataTable _dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Focus();
gridbind();
ViewState["sample"] = (DataTable)GridView1.DataSource;
}
}
public void gridbind()
{
_dt = _objgrid.LoadEmp();
GridView1.DataSource = _dt;
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
e.Cancel = true;
GridView1.EditIndex = -1;
gridbind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gridbind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtEmpName = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtEmpName"));
TextBox txtEmpSal = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtEmpSal"));
TextBox txtAddress = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtAddress"));
TextBox txtEmail = (TextBox)(GridView1.Rows[GridView1.EditIndex].FindControl("txtEmail"));
_objdetails.EmpId = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
_objdetails.EmpName = txtEmpName.Text;
_objdetails.EmpSal = txtEmpSal.Text;
_objdetails.Address = txtAddress.Text;
_objdetails.Email = txtEmail.Text;
int i = _objgrid.update(_objdetails);
if (i > 0)
{
lblMsg.Text = "UpDated Sucessfully";
}
else
{
lblMsg.Text = "Updation Falied";
}
GridView1.EditIndex = -1;
gridbind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int EmpId = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
int i = _objgrid.delete(EmpId);
if (i > 0)
{
lblMsg.Text = "deleted Successfully";
}
else
{
lblMsg.Text = "deletion failed";
}
GridView1.EditIndex = -1;
gridbind();
}
protected void btninsert(object sender, EventArgs e)
{
TextBox txtFooterName = (TextBox)(GridView1.FooterRow.FindControl("txtFooterName"));
TextBox txtFooterSal = (TextBox)(GridView1.FooterRow.FindControl("txtFooterSal"));
TextBox txtFooterAddress = (TextBox)(GridView1.FooterRow.FindControl("txtFooterAddress"));
TextBox txtFooterEmail = (TextBox)(GridView1.FooterRow.FindControl("txtFooterEmail"));
_objdetails.EmpName = txtFooterName.Text;
_objdetails.EmpSal = txtFooterSal.Text;
_objdetails.Address = txtFooterAddress.Text;
_objdetails.Email = txtFooterEmail.Text;
int i = _objgrid.insert(_objdetails);
if (i > 0)
{
lblMsg.Text = "Inserted Successfully";
}
else
{
lblMsg.Text = "Insertion failed";
}
GridView1.EditIndex = -1;
gridbind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
_dt = (DataTable)ViewState["sample"];
//_dt = _objgrid.LoadEmp();
GridView2.DataSource = _dt;
GridView2.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string to =TextBox1.Text;
string From = "rajshekarsanthosh@gmail.com";
string subject = "Required info";
string Body = "sir ,<br> Got the Output <br><br>";
Body += GridViewToHtml(GridView2); //Elaborate this function detail later
Body += "<br><br>Regards,<br>Santhosh";
bool send = send_mail(to, From, subject, Body);//Elaborate this function detail later
if (send == true)
{
string CloseWindow = "alert('Mail Sent Successfully!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
else
{
string CloseWindow = "alert('Problem in Sending mail...try later!');";
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
}
}
public bool send_mail(string to, string from, string subject, string body)
{
MailMessage msg = new MailMessage(from, to);
msg.Subject = subject;
AlternateView view;
SmtpClient client;
StringBuilder msgText = new StringBuilder();
msgText.Append(" <html><body><br></body></html> <br><br><br> " + body);
view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html");
msg.AlternateViews.Add(view);
client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("rajshekarsanthosh@gmail.com", "raj123456");
client.EnableSsl = true; //Gmail works on Server Secured Layer
client.Send(msg);
bool k = true;
return k;
}
private string GridViewToHtml(GridView gv)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
gv.RenderControl(hw);
return sb.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
}
No comments:
Post a Comment