Solved

Help SqlCommand will not return data

Posted on 2008-10-08
7
372 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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 50 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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