1

I'm getting an error

Conversion failed when converting from a character string to unique identifier.

and I don't even have any conversion of dates, so I'm pretty lost here.

I am using a parametrized query and adding 2 values that come from a datarowview. In this case if I debug and check the value for the date I get it in this format {8/17/2016 9:08:44 PM}

string query = @"SELECT TOP 1 
                     SensorValue,
                     ActivityDateTime,
                     (Select TimeAlert from Vehicle_config 
                      where VehicleID = '@guid') as TimeAlert,
                     (Select DistanceAlert from Vehicle_config 
                      where VehicleID = '@guid') as DistanceAlert,
                     (Select TimeInterval from Vehicle_config 
                      where VehicleID = '@guid') as TimeInterval,
                     (Select DistanceInterval from Vehicle_config 
                      where VehicleID = '@guid') as DistanceInterval,
                     (Select AlertFlag from Vehicle_config 
                      where VehicleID = '@guid') as AlertFlag, 
                     (Select Litros from Vehicle_config 
                      where VehicleID = '@guid') as Litros
                 FROM 
                     navman_Fuel_activity 
                 WHERE 
                     ActivityDateTime < DATEADD(MI, -5, @date) 
                     AND VehicleId = '@guid' 
                 ORDER BY ActivityDateTime DESC;";

using (SqlConnection sqlConn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(query, sqlConn))
{
    cmd.Parameters.AddWithValue("date", (DateTime)row["ActivityDateTime"]);
    cmd.Parameters.AddWithValue("guid",(Guid)row["VehicleID"]);

    sqlConn.Open();  

    using (var reader = cmd.ExecuteReader())
    {
        if (reader.Read()) // here is where I get the error
2
  • 2
    your issue is here cmd.Parameters.AddWithValue("guid",(Guid)row["VehicleID"]); Commented Aug 17, 2016 at 21:19
  • 1
    make sure you have valid Guid in each row for row["VehicleID"] Commented Aug 17, 2016 at 21:20

1 Answer 1

1

I believe the failure is happening on this line

cmd.Parameters.AddWithValue("guid",(Guid)row["VehicleID"]);

i think you need to do this instead of the cast

cmd.Parameters.AddWithValue("guid",new Guid(row["VehicleID"]));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.