Joseph Michael Pesch
VP Programming

Simple Web Serivce Call (without Registering Service) Using HttpWebRequest Method

by 22. December 2010 08:13

SimpleWebService.ASMX:

<%@ WebService Language="C#" Class="SimpleWebService" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://JMP-Sample.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 SimpleWebService  : System.Web.Services.WebService {
  [WebMethod]
  public string WsTestMethod(long TestId, string TestData)
  {
    return string.Join(" ""You sent TestId:", TestId.ToString(), "; TestData:", TestData);
  }
}

 

SimpleWebService.ASPX:

 

<%@ Page Language="C#" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text" %>
<script runat="server">
  protected string RootUrl
  {
    get
    {
      string query = (Request.QueryString == null) ? "" : "?" + Request.QueryString.ToString();
      return Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, string.Empty).Replace(query, string.Empty);
    }
  }
  protected void SubmitButton_Click(object sender, EventArgs e)
  {
    string soap =
    @"<?xml version=""1.0"" encoding=""utf-8""?>
      <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
        xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
        xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
      <soap:Body>
        <WsTestMethod xmlns=""http://JMP-Sample.org/"">
          <TestId>" + this.TestId.Text + @"</TestId>
          <TestData>" + this.TestData.Text + @"</TestData>
        </WsTestMethod>
      </soap:Body>
    </soap:Envelope>";

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
      string.Join("", RootUrl, ResolveUrl("~/SimpleWebService.asmx")));
    req.Headers.Add("SOAPAction""\"http://JMP-Sample.org/WsTestMethod\"");
    req.ContentType = "text/xml;charset=\"utf-8\"";
    req.Accept = "text/xml";
    req.Method = "POST";

    Stream str = req.GetRequestStream();
    using (StreamWriter wri = new StreamWriter(str)) wri.Write(soap);

    WebResponse res = req.GetResponse();
    str = res.GetResponseStream();
    this.WsResults.Text = StreamToString(str);

  }

  private string StreamToString(Stream str)
  {
    using (BinaryReader rdr = new BinaryReader(str))
    {
      using (MemoryStream mem = new MemoryStream())
      {
        byte[] buf = new byte[256];
        int bt;
        int bts = 0;
        while ((bt = rdr.Read(buf, 0, 256)) > 0)
        {
          mem.Write(buf, 0, bt);
          bts += bt;
        }
        mem.Position = 0;
        byte[] bytes = new byte[bts];
        mem.Read(bytes, 0, bytes.Length);
        return Encoding.ASCII.GetString(bytes);
      }
    }
  }
</script>
<!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:TextBox ID="TestId" Text="12345" runat="server"></asp:TextBox>
    <asp:TextBox ID="TestData" Text="Simple Web Service" runat="server"></asp:TextBox>
    <asp:Button ID="SubmitButton" OnClick="SubmitButton_Click" Text="Run Web Service"
      runat="server" />
    <asp:Literal ID="WsResults" runat="server"></asp:Literal>
  </div>
  </form>
</body>
</html>

 

Tags:

ASP.Net | C# | Web Service

Invalid postback or callback argument... Client side dynamic drop-down list values cannot be posted back to server

by 21. December 2010 11:39

If you attempt to change drop-down list options dynamically on the client side you may have issues with postback as the drop-down options will not match what the server originally sent to the client.  One way to overcome this is to have a server control with all the possible values (that is hidden on the UI) and a visible client side only drop-down list that is dynamically changed.  As the client side drop-down values are changed keep the server side control synchronized and then use the server side control value on post-back.  Simple sample below:

<%@ Page Language="C#" %>
 <script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      this.ServerSelect.DataSource = new string[] { "one""two""three""four""five" };
      this.ServerSelect.DataBind();
    }
  }
  protected void SubmitButton_Click(object sender, EventArgs e)
  {
    this.ResultLabel.Text = string.Join(" ""You selected value:"this.ServerSelect.SelectedValue.ToString());
  }
</script>
<!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>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function () {
      $.each(["one""two""three""four""five"], function (index, value) {
        $('select#LocalSelect').append($('<option></option>').val(value).html(value));
      });

      $('select#LocalSelect').change(function () {
        $('select#<%=ServerSelect.ClientID%>').val($('select#LocalSelect').val());
      });
    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:DropDownList ID="ServerSelect" runat="server" Style="display: none;" />
    <select id="LocalSelect">
    </select>
    <asp:Button ID="SubmitButton" Text="Submit" OnClick="SubmitButton_Click" runat="server" />
    <asp:Label ID="ResultLabel" runat="server" />
  </div>
  </form>
</body>
</html>

Tags:

Could not load type 'System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider'

by 21. December 2010 10:51

Make sure you have the assemblies referenced as follows:

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
  </assemblies>
  <buildProviders>
    <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
  </buildProviders>
</compilation>

Tags:

Asp.Net Get Root Url

by 21. December 2010 07:39

In the page:

<%=RootUrl%><%=ResolveUrl("~/InmanNewsFeedWs.asmx")%>

 

Code behind:

protected string RootUrl
{
  get
  {
    string query = (Request.QueryString == null) ? "" : "?" + Request.QueryString.ToString();
    return Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, string.Empty).Replace(query, string.Empty);
  }
}

Tags: