Why is this not this cmd.Parameters.AddWithValue working

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
 
J

Jeff Johnson

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?
 
J

Jeff Johnson

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."
 
T

Tony Johansson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top