Why is this not this cmd.Parameters.AddWithValue working

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

Why is not this row working cmd.Parameters.AddWithValue("@TicketID",
TicketID);
When this row da.Fill(ds); is executing
I get runtime exception error saying Must declare the scalar variable
"@TicketID"
So the problem is why is not cmd.Parameters.AddWithValue("@TicketID",
TicketID);
workin ?

Here is the method
public DataSet GetTickets(string TicketID)
{
try
{
string connectionstring =
WebConfigurationManager.ConnectionStrings["ConnectionStringTicket"].ConnectionString;
using (SqlConnection connect = new
SqlConnection(connectionstring))
{
string selectString = "select HeadLine, Description,
Priority, Created, Changed, Users.Name, Finished where TicketID =
@TicketID";
SqlCommand cmd = new SqlCommand(selectString, connect);

cmd.Parameters.AddWithValue("@TicketID", TicketID);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(selectString,
connect);

da.Fill(ds);
return ds;
}
}
catch (Exception ex)
{
throw;
}
}

//Tony
 
string selectString = "select HeadLine, Description, Priority, Created,
Changed, Users.Name, Finished where TicketID = @TicketID";

Is this your ACTUAL code? Because there's no FROM clause in that SQL
statement.

public DataSet GetTickets(string TicketID)

Also, TicketID looks like an integer, so why are you passing it into the
function as a string?
 
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(selectString,
connect);

da.Fill(ds);

By the way, here's your problem. You're passing the SQL string to the data
adapter, not the command you built. So the parameter you added is
"elsewhere."
 
many thanks.
I have been looking for a long time so at last I used string.format which is
bad so no I swith to using parameterized query.

//Tony
 
Back
Top