ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Call ASMX Web Service in web application using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="AliYilmaz"> AliYilmaz</a>,</p> <p>Its totally depends on the data format which is going to be returned&nbsp; and how its going to be handled whether in Json format or you want to give your own way for eg. Property Class Format,DataTable etc. as you can return your data in whichever format you want.</p> <p>I have made a sample in which i have showed that how you can handle Json data and Property Class data like that you can implement your own way of returning data and handling it on the user end.</p> <p>I have used NewtonJson DLL for converting Json Data to DataTable you can get NewtonJson DLL from below link and add reference to your project and use it.</p> <p><a href="http://james.newtonking.com/json/help/index.html" target="_blank">NewtonJson</a></p> <p><strong><span style="text-decoration: underline;">WebService.asmx</span></strong></p> <pre class="brush: html">&lt;%@ WebService Language=&#34;C#&#34; CodeBehind=&#34;~/App_Code/WebService.cs&#34; Class=&#34;WebService&#34; %&gt; </pre> <p><strong><span style="text-decoration: underline;">WebService.cs</span></strong></p> <pre class="brush: csharp">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Script.Services; using System.Web.Script.Serialization; using System.Data.SqlClient; using System.Configuration; using System.Data; using System.Xml; /// &lt;summary&gt; /// Summary description for WebService /// &lt;/summary&gt; [WebService(Namespace = &#34;http://tempuri.org/&#34;)] [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 WebService : System.Web.Services.WebService { [WebMethod] public UserDetail GetDataInClassFormat(string Email, string UserName) { UserDetail us = new UserDetail(); try { SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString); SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Users WHERE Email = @Email AND UserName = @UserName&#34;, cn); DataTable dt = new DataTable(); cmd.Parameters.AddWithValue(&#34;@Email&#34;, Email); cmd.Parameters.AddWithValue(&#34;@UserName&#34;, UserName); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); if (dt.Rows.Count &gt; 0) { us.UserId = Convert.ToInt32(dt.Rows[0][&#34;UserId&#34;].ToString()); us.Name = dt.Rows[0][&#34;Name&#34;].ToString(); us.Email = dt.Rows[0][&#34;Email&#34;].ToString(); us.UserName = dt.Rows[0][&#34;UserName&#34;].ToString(); us.Password = dt.Rows[0][&#34;Password&#34;].ToString(); us.ImageName = dt.Rows[0][&#34;ImageName&#34;].ToString(); } } catch (Exception ex) { } return us; } [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public string GetDataInJsonFormat(string Email, string UserName) { string resultJSON = &#34;&#34;; try { SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString); cn.Open(); SqlCommand cmd = new SqlCommand(); DataTable dt; SqlDataReader reader; cmd.CommandType = CommandType.Text; cmd.CommandText = &#34;SELECT * FROM Users WHERE Email = @Email AND UserName = @UserName&#34;; cmd.Parameters.AddWithValue(&#34;@Email&#34;, Email); cmd.Parameters.AddWithValue(&#34;@UserName&#34;, UserName); cmd.Connection = cn; reader = cmd.ExecuteReader(); dt = new DataTable(&#34;results&#34;); dt.Load(reader); cn.Close(); JavaScriptSerializer serializer = new JavaScriptSerializer(); List&lt;Dictionary&lt;String, Object&gt;&gt; tableRows = new List&lt;Dictionary&lt;string, object&gt;&gt;(); Dictionary&lt;String, Object&gt; row; foreach (DataRow dr in dt.Rows) { row = new Dictionary&lt;string, object&gt;(); foreach (DataColumn col in dt.Columns) { row.Add(col.ColumnName, dr[col].ToString()); } tableRows.Add(row); } return resultJSON = serializer.Serialize(tableRows).ToString(); } catch (Exception ex) { return resultJSON = ex.Message.ToString(); } } public class UserDetail { int _userId; public int UserId { get { return _userId; } set { _userId = value; } } string _name; public string Name { get { return _name; } set { _name = value; } } string _email; public string Email { get { return _email; } set { _email = value; } } string _userName; public string UserName { get { return _userName; } set { _userName = value; } } string _password; public string Password { get { return _password; } set { _password = value; } } string _imageName; public string ImageName { get { return _imageName; } set { _imageName = value; } } } }</pre> <p><strong><span style="text-decoration: underline;">Default.aspx</span></strong></p> <pre class="brush: html">&lt;div&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; Email: &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtEmail&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; UserName: &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtUserName&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:Button ID=&#34;btnGetjSonData&#34; OnClick=&#34;BindJsonData&#34; Text=&#34;Bind Json Data&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Button ID=&#34;btnGetClassData&#34; Text=&#34;Bind Class Data&#34; OnClick=&#34;BindClassData&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvUserDetails&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt;</pre> <p><strong><span style="text-decoration: underline;">Default.aspx</span><span style="text-decoration: underline;">.cs</span></strong></p> <pre class="brush: csharp">protected void BindJsonData(object sender, EventArgs e) { ServiceReference1.WebServiceSoapClient client = new ServiceReference1.WebServiceSoapClient(); string jsonData = client.GetDataInJsonFormat(txtEmail.Text.Trim(), txtUserName.Text.Trim()); JavaScriptSerializer js = new JavaScriptSerializer(); DataTable dt = JsonConvert.DeserializeObject&lt;DataTable&gt;(jsonData); gvUserDetails.DataSource = dt; gvUserDetails.DataBind(); } protected void BindClassData(object sender, EventArgs e) { ServiceReference1.WebServiceSoapClient client = new ServiceReference1.WebServiceSoapClient(); ServiceReference1.UserDetail userDetail = client.GetDataInClassFormat(txtEmail.Text.Trim(), txtUserName.Text.Trim()); List&lt;ServiceReference1.UserDetail&gt; userDetails = new List&lt;ServiceReference1.UserDetail&gt;(); userDetails.Add(userDetail); gvUserDetails.DataSource = userDetails; gvUserDetails.DataBind(); }</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/l1CoxKO.gif" alt="" width="472" height="168" /></p>https://www.aspforums.net:443/Threads/892641/Call-ASMX-Web-Service-in-web-application-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/892641/Call-ASMX-Web-Service-in-web-application-using-C-and-VBNet-in-ASPNet/Fri, 02 Jun 2017 08:32:04 GMT