有谁知道为什么我会得到这个?我不是一个asp.net的人,所以试图为某人修理一些东西。
Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.
Source Error:
Line 135: {
Line 136: dataSet = new DataSet();
Line 137: adapter.Fill(dataSet);
Line 138: }
Line 139: finally
完整C#
public DataSet pendingCourses()
{
DataSet dataSet = new DataSet();
User user = (User)Context.Items["CurrentUser"];
SqlConnection selectConnection = new SqlConnection(ConfigurationSettings.AppSettings["DBConnectStr"]);
SqlDataAdapter adapter = new SqlDataAdapter("dbo.procUsersGetInProgressCourses", selectConnection);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
// get results
adapter.SelectCommand.Parameters.Add("@Limit", SqlDbType.Int).Value = 5;
adapter.SelectCommand.Parameters.Add("@Culture", SqlDbType.VarChar, 6).Value = "en-GB";
adapter.SelectCommand.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = "EC9D439A-A084-48A4-80DD-80F7BB00AEF2";
try
{
dataSet = new DataSet();
adapter.Fill(dataSet);
}
finally
{
if (selectConnection.State == ConnectionState.Open)
{
selectConnection.Close();
}
}
return dataSet;
}
答案 0 :(得分:3)
您可以尝试:
.Value = new Guid("EC9D439A-A084-48A4-80DD-80F7BB00AEF2");
SqlDbType.UniqueIdentifier
的相应.NET类型为System.Guid
,因此您需要为其指定Guid
类型的值。
答案 1 :(得分:2)
试
adapter.SelectCommand.Parameters.Add("@UserID", SqlDbType.UniqueIdentifier).Value = Guid.Parse("EC9D439A-A084-48A4-80DD-80F7BB00AEF2");
主要观点是
Guid.Parse("EC9D439A-A084-48A4-80DD-80F7BB00AEF2")
sql期待一个uniqueidentifier(guid)并且你正在发送一个字符串。这将发送正确的类型。