Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help SqlCommand will not return data

Posted on 2008-10-08
7
Medium Priority
?
389 Views
Last Modified: 2012-06-27
I cannot seem to get this command to return data, an anyone tell me what I am doing wrong?

SqlCommand queryCommand = new SqlCommand(string.Format("Select * from table where Account = '{0}'", txtAccountInput.Text));
SqlCommand queryCommand = new SqlCommand(string.Format("Select * from table where Account = '{0}'", txtAccountInput.Text));

Open in new window

0
Comment
Question by:ATaiPan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Expert Comment

by:oxyoo
ID: 22668766
Hi,

Could you post the entire C# code, the connection part as well as
where you run your command?

Thanks.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22668805
please try this instead, replacing the data type and size as needed:
SqlCommand queryCommand = new SqlCommand("Select * from table where Account = @acc");
SqlParameter p = new System.Data.SqlClient.SqlParameter("@acc", System.Data.SqlDbType.NVarChar, 10, txtAccountInput.Text);
queryCommand.Parameters.Add(p);

Open in new window

0
 

Author Comment

by:ATaiPan
ID: 22669746
Well, now I have a new error "The parameterized query '(acc nvarchar(10))Select * from table where Account = @acc' expects the parameter @acc" which is not supplied.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22669763
you will need to show more code so we can see where you made the error.
0
 
LVL 10

Expert Comment

by:eguilherme
ID: 22670096
SqlCommand queryCommand = new SqlCommand(string.Format("Select * from table where Account = '{0}'", txtAccountInput.Text));

after this do:

SqlDataReader Reader = queryCommand.ExecuteReader();
while(Reader.Read())
{
   // use Reader[field_name] to get the values
}
0
 
LVL 10

Accepted Solution

by:
oxyoo earned 200 total points
ID: 22718448
Try to modify the code as done below and see if it helps..

Good Luck!

SqlCommand queryCommand = new SqlCommand("Select * from table where Account = @acc");
queryCommand.CommandType = CommandType.Text
queryCommand.Parameters.AddWithValue("@acc", txtAccountInput.Text);

Open in new window

0
 
LVL 10

Expert Comment

by:oxyoo
ID: 22718474
Here is a more complete example...
using(SqlConnection connection = new SqlConnection("connectionstring") ) {
  SqlCommand command = new SqlCommand("Select * from table where Account = @acc", connection);
  command.CommandType = CommandType.Text;
  command.Parameters.AddWithValue("@acc", txtAccountInput.Text);
  connection.Open();
 
  SqlDataReader reader = command.ExecuteReader();
  while(reader.Read()) {
    // magic goes here...
  }
 
  connection.Close();
}

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question