.NET Framework - Why is this not this cmd.Parameters.AddWithValue working

Asked By Tony Johansson on 19-Jun-12 08:58 AM
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
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


Jeff Johnson replied to Tony Johansson on 19-Jun-12 05:57 PM
Is this your ACTUAL code? Because there is no FROM clause in that SQL
statement.



Also, TicketID looks like an integer, so why are you passing it into the
function as a string?
Jeff Johnson replied to Tony Johansson on 19-Jun-12 05:59 PM
By the way, here is your problem. You're passing the SQL string to the data
adapter, not the command you built. So the parameter you added is
Tony Johansson replied to Jeff Johnson on 21-Jun-12 02:19 AM
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