//1. FIRST CREATE DATABASE:
//2. SECOND CREATE WEB SERVICE:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
SqlConnection conn;
[WebMethod]
public DataSet selectMethod()
{
DataSet ds = new DataSet();
string strCmd = "select * from UserInfo";
conn = new SqlConnection("Data Source=WT49;Initial Catalog=MyWebServiceDataBase;Integrated Security=True");
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strCmd, conn);
sda.Fill(ds);
return ds;
conn.Close();
}
[WebMethod]
public DataSet deleteMethod(String str1)
{
DataSet ds = new DataSet();
string strCmd = "delete from UserInfo where UserID='"+str1+"'";
conn = new SqlConnection("Data Source=WT49;Initial Catalog=MyWebServiceDataBase;Integrated Security=True");
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strCmd, conn);
sda.Fill(ds);
return ds;
conn.Close();
}
[WebMethod]
public DataSet updateMethod(String str1, String str2, String str3, String str4)
{
DataSet ds = new DataSet();
string strCmd = "Update UserInfo set User_Name='" + str1 + "',User_Address='" + str2 + "',E_Mail='" + str3 + "' where UserID='" + str4 + "'";
conn = new SqlConnection("Data Source=WT49;Initial Catalog=MyWebServiceDataBase;Integrated Security=True");
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strCmd, conn);
sda.Fill(ds);
return ds;
conn.Close();
}
[WebMethod]
public DataSet insertMethod(String str1, String str2, String str3)
{
DataSet ds = new DataSet();
string strCmd = "Insert into UserInfo(User_Name,User_Address,E_mail) values('"+str1+"','"+str2+"','"+str3+"')";
conn = new SqlConnection("Data Source=WT49;Initial Catalog=MyWebServiceDataBase;Integrated Security=True");
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter(strCmd, conn);
sda.Fill(ds);
return ds;
conn.Close();
}
}
BEFORE YOU CREATE ASP.NET WEB FORM YOU NEED TO CREATE A WEB REFFERENCE IN YOUR WEB APPLICATION.
FOLLOW THE STEPS TO CREATE A WEB REFERENCE:
RIGHT CLICK ON THE PROJECT > ADD WEB REFERENCE.
//3. NOW CODE IN ASP.NET:
namespace MYWebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
localhost.Service obj = new localhost.Service();
GridView1.DataSource = obj.selectMethod();
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text.ToString();
TextBox2.Text = GridView1.SelectedRow.Cells[2].Text.ToString();
TextBox3.Text = GridView1.SelectedRow.Cells[3].Text.ToString();
TextBox4.Text = GridView1.SelectedRow.Cells[4].Text.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox2.Text == null || TextBox3.Text == null || TextBox4.Text == null || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")
{
Response.Write("<script>alert('User ID Field Can Not Be Empty')</script>");
}
else
{
localhost.Service obj = new localhost.Service();
obj.insertMethod(TextBox2.Text.ToString(), TextBox3.Text.ToString(), TextBox4.Text.ToString());
GridView1.DataSource = obj.selectMethod();
GridView1.DataBind();
TextBox1.Text = null;
TextBox2.Text = null;
TextBox3.Text = null;
TextBox4.Text = null;
//Response.Write("<script>alert('Record Has Been Saved Successfully!')</script>");
}
}
protected void Button3_Click(object sender, EventArgs e)
{
if (TextBox1.Text != null)
{
localhost.Service obj = new localhost.Service();
obj.updateMethod(TextBox2.Text.ToString(), TextBox3.Text.ToString(), TextBox4.Text.ToString(), TextBox1.Text.ToString());
GridView1.DataSource = obj.selectMethod();
GridView1.DataBind();
//Response.Write("<script>alert('Record has been Updated successfully!')</script>");
}
else
{
Response.Write("<script>alert('User Id Is Not Selected')</script>");
}
}
protected void Button4_Click(object sender, EventArgs e)
{
if (TextBox1.Text != null)
{
localhost.Service obj = new localhost.Service();
obj.deleteMethod(TextBox1.Text.ToString());
GridView1.DataSource = obj.selectMethod();
GridView1.DataBind();
TextBox1.Text = null;
TextBox2.Text = null;
TextBox3.Text = null;
TextBox4.Text = null;
}
else
{
Response.Write("<script>alert('User ID Field Can Not Be Empty')</script>");
}
}
protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
if (GridView1.SelectedRow == null)
{
TextBox2.Enabled = false;
TextBox3.Enabled = false;
TextBox4.Enabled = false;
}
}
protected void Button5_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "" || TextBox2.Text != "" || TextBox3.Text != "" || TextBox4.Text != "")
{
TextBox2.Enabled = true;
TextBox3.Enabled = true;
TextBox4.Enabled = true;
TextBox1.Text = null;
TextBox2.Text = null;
TextBox3.Text = null;
TextBox4.Text = null;
}
}
}
}
No comments:
Post a Comment