Solved

Help SqlCommand will not return data

Posted on 2008-10-08
7
357 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
7 Comments
 
LVL 10

Expert Comment

by:oxyoo
Comment Utility
Hi,

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

Thanks.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 142

Expert Comment

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

Expert Comment

by:eguilherme
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now