• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Help SqlCommand will not return data

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
ATaiPan
Asked:
ATaiPan
1 Solution
 
oxyooCommented:
Hi,

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

Thanks.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
ATaiPanAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you will need to show more code so we can see where you made the error.
0
 
eguilhermeCommented:
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
 
oxyooCommented:
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
 
oxyooCommented:
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

Technology Partners: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now