1

I'm new to C# and javacript. I'm doing my first program on Visual Studio and I have one problem to retrieve data from a SQL Server database.

I want to retrieve data from SQL Server and convert it to Json by using an ajax method. But when I start my web page I can see only the table empty, so I think I have not a good method to connect to my data base.

Here is the code for my WebMethod used to retrieve data and convert to Json:

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;
using Newtonsoft.Json;

public partial class _Default : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {                         
    }

    [WebMethod]
    public static string LoadData()
    {
        string strcon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("select * from kit", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);            
        return JsonConvert.SerializeObject(ds.Tables[0]); 
    }  

Please I need your Help I have done several days on this program. Thanks for all your help.

2
  • in addition to what @DevSlick has said below in his answer, any reason why you want to give back a bloated ungeneric object? Best practice: Map the data to a concrete type and return that back to the caller Commented Dec 11, 2014 at 18:08
  • Thanks for your help @Ahmed ilyas but can you help me by showing how to map data to a concrete type? I don't know how to do it. Commented Dec 12, 2014 at 16:50

1 Answer 1

1

Your CommandType should be Text and not StoredProcedure since you are using a string SQL command and not a Stored Procedure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.