asp.net 中无法识别的 Guid 格式错误
本文关键字:Guid 格式 错误 识别 net asp | 更新日期: 2023-09-27 18:36:30
大家好,我在 asp.net 项目中遇到了这个问题。"System.FormatException: 无法识别的 Guid 格式。 at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName, Exception innerException) at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult&result) 在系统。ctor(字符串 g) at UserAuthentication.HumanResources.ViewAllJobs() in HumanResources.cs:line 105"
我的人力资源.cs的代码和存储过程如下 第一个是人力资源代码,第二个是存储程序
public static List<HumanResources> ViewAllJobs()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand("spHumanResourcesViewAllJobs", conn);
cmd.CommandType = CommandType.StoredProcedure;
List<HumanResources> jobs = new List<HumanResources>();
try
{
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
HumanResources hr = new HumanResources();
hr.jobTitle = rdr["JobTitle"].ToString();
Guid guid = new Guid(rdr["Guid"].ToString());
hr.guid = guid;
jobs.Add(hr);
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
cmd.Dispose();
conn.Close();
}
return jobs;
}
Store Procedure:
CREATE PROCEDURE spHumanResourcesViewAllJobs
AS
BEGIN
SET NOCOUNT ON;
SELECT Guid,JobTitle
FROM HR
WHERE Active=1
结束去
错误位于此行:
Guid guid = new Guid(rdr["Guid"].ToString());
可以在代码中放置一个保护子句,以便在对象没有有效 GUID 时不添加hr
对象:
Guid guid;
if (Guid.TryParse(rdr["Guid"].ToString(), out guid))
{
hr.guid = guid;
jobs.Add(hr);
}