C# stored procedure with parameters

前端 未结 9 2373
误落风尘
误落风尘 2020-12-10 11:49

I am receiving an error in my application and i can not figure out how to resolve it. Here is the code:

SqlConnection myConnection = new SqlConnection(Confi         


        
相关标签:
9条回答
  • 2020-12-10 12:08
      myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t;
    

    t is a Task not a string. This should probably by name.

    0 讨论(0)
  • 2020-12-10 12:11

    I think the problem is with this line:

    myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t;
    

    t cannot be converted to a string. You could try t.Name or t.ToString() (not sure what properties are available on that class off the top of my head.)

    0 讨论(0)
  • 2020-12-10 12:16

    In the line:

    myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t;
    

    I think you have to pass t.Name or t.ToString() up on the task's name (I don't know it).

    0 讨论(0)
  • 2020-12-10 12:20

    t is of type Task but when you pass it to your stored procedure you are passing it as nvarchar.

    Here is your code:

    myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t;
    
    0 讨论(0)
  • 2020-12-10 12:20

    T is type of Task and and ado try convert it to string you should put t.GetType().Name or the real task name here because we can't pass objects as parameter the only type are known sqltypes

    0 讨论(0)
  • 2020-12-10 12:23

    The issue is on this line:

    myCommand2.Parameters.Add("@TaskName", SqlDbType.NVarChar, 50).Value = t;
    

    You are passing the task object in instead of the name of the task.

    0 讨论(0)
提交回复
热议问题