Solved

Help SqlCommand will not return data

Posted on 2008-10-08
7
374 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#.net split string and then check appropriate checkboxlist 4 55
need to convert function to c# 5 55
C# DateTime Help 6 46
VS C# how to design libary of functions 7 33
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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