Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,

my problem is :How to call webservice that returns Data Table in Javascript function

then draw this data table in run time.

Thanks for reading

[Edit on OP's behalf]

This is in my codebehind file:

C#
public DataTable GetData(int from_month, int to_month)
{
    int year = DateTime.Now.Year;
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection(sWebCon);
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.StoredProcedure;
    com.CommandText = "usp_tbl_losses";
    SqlParameter prm_yearl = new SqlParameter("@year", SqlDbType.Int);
    prm_yearl.Value = year;
    com.Parameters.Add(prm_yearl);
    SqlParameter prm_from_monthl = new SqlParameter("@from_month", SqlDbType.Int);
    prm_from_monthl.Value = from_month;
    com.Parameters.Add(prm_from_monthl);
    SqlParameter prm_to_monthl = new SqlParameter("@to_month", SqlDbType.Int);
    prm_to_monthl.Value = to_month;
    com.Parameters.Add(prm_to_monthl);
    try
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = com;
        da.Fill(dt);
    }
    catch (Exception ex)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        dt = null;
    }
    finally
    {
        con.Close();
    }
    return dt;
}


and this is my javascript:

XML
<script type="text/javascript">
        var fromMonth , toMonth;
        function GetData()
        {
            fromMonth = document.getElementById('txt_from').value;
            toMonth = document.getElementById('txt_to').value;
            // I need to read returned data table below
            WebService.GetData(fromMonth, toMonth, oncomplate,onfailed);
        }

        function oncomplate() {
            alert("this worked");
        }
        function onfailed() {
            alert("error");
        }
</script>
Posted
Updated 11-May-11 22:55pm
v2
Comments
senguptaamlan 12-May-11 3:55am    
please, post what you have done....and in which part of code you are experiencing problem...

1 solution

HI Manfred,
The solution you are looking for is available in codeproject only
check out this

Calling Web Services from HTML Pages using JavaScript[^]

Let me know if you find any problem in implementing this.

Enjoy life :)
 
Share this answer
 
Comments
Member 7914335 12-May-11 5:21am    
Thanks for your replay ,but I need to return datatable not one value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900