Asked by:
return more than one value with ajax in web method

Question
-
User181930479 posted
im trying to fetch the data from the table using ajax , its working great but my query is asking for 3 values from the table row , im only getting 1
below is my code
<script> $(document).ready(function () { //$("#btntst").click(function() { $("#askme").keyup(function () { $("#ora").empty(); $.ajax({ type: "POST", url: "Default.aspx/GetData", data: "{'Word' : '" + $("#askme").val() + "'}", dataType: "json", contentType: "application/json; charset=utf-8", success: function (res) { // alert(res.d); $("#ora").append(res.d); }, error: function (xhr, textStatus, err) { alert("readyState: " + xhr.readyState); alert("responseText: " + xhr.responseText); alert("status: " + xhr.status); alert("text status: " + textStatus); alert("error: " + err); } }); }); }); </script>
webmethod:
public static string GetData(string Word) { string sql = " SELECT meaning, syn1, syn2 from tbl_dic where Word=N'" + Word +"'" ; DataTable DATA = new DataTable(); SqlDataAdapter dta = new SqlDataAdapter(sql,con); dta.Fill(DATA); if(DATA.Rows.Count > 0 ) { return DATA.Rows[0].ItemArray[1].ToString(); } return ""; }
IM ONLY GETTING ONE VALUE , CAUSE ITEM ARRAY IS [1] , I CANT FIGURE OUT HOW TO RETURN ALL THE VALUE .... IS IT USING A LOOP OR ????
ANY HELP WOULD BE APPRECIATED.
Tuesday, August 28, 2018 6:42 AM
All replies
-
User753101303 posted
Hi,
Yes you could copy a column to a string array and return so that on the client side you'll get a string array rather than a single string.
You could use a "classic" loop or Linq (are you familiar with that ?). See for example https://stackoverflow.com/questions/20629547/how-to-save-datatable-first-column-in-array-c-sharp
Tuesday, August 28, 2018 7:11 AM -
User181930479 posted
can you implement in code ?
Tuesday, August 28, 2018 7:51 AM -
User283571144 posted
Hi NAF,
As far as I know,
DATA.Rows[0].ItemArray[1].ToString()
will just return one value.If you want to return multiple value in that row, I suggest that you could define a List<String> to save the return data.
More details, you could refer to the code below.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#Button1").on('click', function (e) { e.preventDefault(); $.ajax({ type: "POST", url: "ReturnVal.aspx/GetData", data: "{'Eid' : '" + $("#TextBox1").val() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { console.log(data.d); var employeeTable = $('#tblEmployee tbody'); employeeTable.empty(); employeeTable.append('<tr><td>' + data.d[0] + '</td><td>' + data.d[1] + '</td><td>' + data.d[2] + '</td></tr>'); }, error: function (err) { } }); }) }) </script> </head> <body> <form id="form1" runat="server"> <div> <div class="showTable"> <div class="container"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" Text="Show" runat="server" /> <br /> <table id="tblEmployee" class="table"> <thead class="bg-primary text-white"> <tr> <th>Ename</th> <th>age</th> <th>sex</th> </tr> </thead> <tbody></tbody> </table> </div> </div> </div> </form> </body> </html>
Code-behind:
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.Configuration; using System.Web.Services; [WebMethod] public static List<String> GetData(string Eid) { string constr = ConfigurationManager.ConnectionStrings["EmployeeManagementConnectionString"].ConnectionString; List<String> stringArr = new List<String>(); using (SqlConnection con = new SqlConnection(constr)) { string myQuery = "SELECT * FROM tb_info WHERE Eid = @test"; con.Open(); using (SqlDataAdapter sda = new SqlDataAdapter(myQuery, con)) { sda.SelectCommand.Parameters.AddWithValue("@test", Eid); using (DataTable dt = new DataTable()) { sda.Fill(dt); for(int i = 1; i < 4; i++) { stringArr.Add(dt.Rows[0][i].ToString()); } } } } return stringArr; }
Best Regards,
Brando
Friday, August 31, 2018 2:37 AM