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 9270063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:16:46+00:00 2026-06-18T15:16:46+00:00

C# code: command = new SqlCommand(null, connection); command = createSQLQuery(command); // returns a valid

  • 0

C# code:

command = new SqlCommand(null, connection);
command = createSQLQuery(command); // returns a valid SQL Query that returns results in SQL Management Studio 2012
//dataGridView1.DataSource = GetData(command);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
connection.Close();
dataGridView1.DataSource = GetData(command);
debugMySQL();

public DataTable GetData(SqlCommand cmd) {
    //SqlConnection con = new SqlConnection(connString);
    //SqlCommand cmd = new SqlCommand(sqlcmdString, cn);

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    connection.Open();
    DataTable dt = new DataTable();
    da.Fill(dt);
    connection.Close();
    return dt;
}

I am using C# with WinForm and developing with Visual Studio 2012.

  • 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-06-18T15:16:47+00:00Added an answer on June 18, 2026 at 3:16 pm

    Your question is rather vague, but I’ll try to answer as best as I could.

    I think, normally, you need to define the type of command that you are going to pass into the SqlCommand, before executing that command.

    command.Commandtype = CommandType.StoredProcedure;

    (OR)

    command.CommandText = CommandType.Text;

    You can set the SQL query in string value after that.

    command.CommandText = ""; // your SQL query, or the name of the stored procedure

    Also, you don’t need to set the DataSource of the grid twice. You only need to set it after you got the result of you SQL query execution is returned.
    However you receive the result is your choice. But, you seemed to executed the command twice, with both GetData() method and command.ExecuteReader() method. And also, you are setting the DataSource of your grid twice too.

    I think even if you managed to set up the command object properly, because of your double executions, data might be lost somehow between them.

    And, by the way, what data type is returned from the createSQLQuery() method. You said it returned a valid SQL Query, but is it returned a valid sql query value in string, or your custom command object to parse into the SqlCommand?

    I’ll include the best operable procedure I know for reading from the SQL database and bind that data into the grid control.

    // I suppose you get the connection object as 'con'
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "...";   // You desired SQL query goes here, or if you want to execute a stored procedure, set the command type as 'StoredProcedure' and text as sp name.
    
    SqlDataAdapter dap = new SqlDataAdapter();
    dap.SelectCommand = cmd;
    DataTable tbl = new DataTable();
    dap.Fill(tbl);
    <br>
    if (tbl.Rows.Count > 0)
    {
        grid.DataSource = tbl;
        grid.DataBind();
    }
    

    I recommend you to receive the result as DataTable rather than SqlDataReader. Cause DataTable is more flexible and versatile.

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

Sidebar

Related Questions

I have the code below: using (SqlCommand command = new SqlCommand()) { command.CommandType =
I ran the SQL Query in SQL Server Management Studio and it worked. Here
I'm trying to use Command.Prepare with a CommandType.Text query that has a single input
my code fires off ExecuteNonQueryAsyc. In debug I see it hit command.BeginExecuteNonQuery(new AsyncCallback(AsyncCommandCompletionCallback), command);
I have following code for specifying parameters for SQL query. I am getting following
I wrote C# code which returns a DataTable . All parameters i.e. Connection, sQuery
I'm interested in what the code (command line and php) would look like where
I run the following code from command line: public class MemoryTest { public static
When I use the following code in command prompt, it works fine. But when
One of my pet peeves with debugging Perl code (in command line debbugger, perl

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.