Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 639887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:53:32+00:00 2026-05-13T20:53:32+00:00

I am pretty new at C# and .NET, but I’ve made this code to

  • 0

I am pretty new at C# and .NET, but I’ve made this code to call a stored procedure, and I then want to take the returned DataTable and convert it to JSON.

    SqlConnection con = new SqlConnection("connection string here");
    SqlDataAdapter da = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand("getDates", con);
    SqlParameter par = new SqlParameter("@PlaceID", SqlDbType.Int);
    par.Value = 42;
    da.SelectCommand = cmd;
    cmd.Parameters.Add(par);
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();

    con.Open();

    try{
        cmd.CommandType = CommandType.StoredProcedure;
        da.Fill(ds);
    }

My question then is what is the best/simplest way to do that? An example would be great as I’m still very new to this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:53:33+00:00Added an answer on May 13, 2026 at 8:53 pm

    Instead of a datatable you should use a datareader. Your code is inefficient and somewhat hard to read – you may want to do something like this:

    StringBuilder json = new StringBuilder();
    
    using(SqlConnection cnn = new SqlConnection(your_connection_string)) 
    {
        cnn.open();
    
        using(SqlCommand cmd = new SqlCommand("name_of_stored_procedure", cnn)) 
        {
            cmd.Paramters.AddWithValue("@Param", "value");
    
            using(SqlDataReader reader = cmd.ExecuteReader()) 
            {
                while(reader.Read()) 
                {
                    json.AppendFormat("{{\"name\": \"{0}\"}}", reader["name"]);
                }
            }
        }
    
        cnn.close();
    } 
    

    you can then use json.ToString to get the outpt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question ONE: I'm still pretty new to .net, but have used Visual Studio for
I'm pretty new with TeamCity but have 2-3 years experience with CC.NET -t. I'm
I'm pretty new to C# and asp.net so apologies if this is a really
I'm pretty new to ASP.Net and I'm not sure I'm going about this the
Pretty new to ASP.NET and trying to figure out how to have the code
I am pretty new to asp.net mvc. I want to get the parameter string
How would i go about this? I tried Net::HTTP.new(@site).head('/').kind_of? Net::HTTPOK but I receive this:
I'm pretty new to Asp.net so sorry if this is elementary. I'm trying to
I am pretty new to web services, C#, and .NET. I want to use
I'm pretty new to asp net 2.0 programming and I was wondering how can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.