力不能支显著标准表达式的品种,职业总计
分类:计算机编程

例子:(报错了)

可空值类型

服务器无法在发送 HTTP 标头之后修改 cookie。
我新增了一个a页面复制了b页面的代码配置好连接后,从b页面点击跳转到a页面之后报:服务器无法在发送 HTTP 标头之后修改 cookie。这个异常,以下是代码
HttpCookie ck = HttpContext.Current.Request.Cookies[CookiesName];
if ( ck == null )
{
ck = new HttpCookie( CookiesName );
}
ck.Expires = DateTime.Now.AddYears;
ck.Domain = CookiesDomain;
foreach ( KeyValuePair pair in dic )
{
ck.Values[pair.Key] = HttpUtility.UrlEncode( pair.Value.ToString;
ck.Values[pair.Key ExpireTimeStr] = !timeout.HasValue ? HttpUtility.UrlEncode( CookiesExpire.ToString( "yyyy-MM-dd HH:mm:ss" ) ) : HttpUtility.UrlEncode( DateTime.Now.AddMinutes( timeout.Value ).ToString( "yyyy-MM-dd HH:mm:ss" ) );
}
ck.Values[ValidateName] = GetValidateStr;
HttpContext.Current.Response.Cookies.Add;

protected void Page_Load(object sender, EventArgs e)
{
System.Data.DataTable dt = GetExcelDatatable("D:\_ExportData.xlsx", "mapTable");
InsetData(dt);
}

  public DateTime? time { get; set; }

图片 1

 

  time = item.HospOutDate.HasValue ? DateTime.Parse(item.HospOutDate.Value.ToString("yyyy-MM-dd HH:mm:ss")) : null;

 

 

错误提示:无法确定条件表达式的类型,因为“<null>”和“System.DateTime”之间没有隐式转换

备注

/// <summary>
/// Excel数据导入Datable
/// </summary>
/// <param name="fileUrl"></param>
/// <param name="table"></param>
/// <returns></returns>
public System.Data.DataTable GetExcelDatatable(string fileUrl, string table)
{
//office2007之前 仅支持.xls
//const string cmdText = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1';";
//支持.xls和.xlsx,即包括office2010等版本的 HDR=Yes代表第一行是标题,不是数据;
const string cmdText = "Provider=Microsoft.Ace.OleDb.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
System.Data.DataTable dt = null;
//建立连接
OleDbConnection conn = new OleDbConnection(string.Format(cmdText, fileUrl));
try
{
//打开连接
if (conn.State == ConnectionState.Broken || conn.State == ConnectionState.Closed)
{
conn.Open();
}

 

 

System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//获取Excel的第一个Sheet名称
string sheetName = schemaTable.Rows[0]["TABLE_NAME"].ToString().Trim();
//查询sheet中的数据
string strSql = "select * from [" sheetName "]";
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
DataSet ds = new DataSet();
da.Fill(ds, table);
dt = ds.Tables[0];
return dt;
}
catch (Exception exc)
{
throw exc;
}
finally
{
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// 从System.Data.DataTable导入数据到数据库
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public int InsetData(System.Data.DataTable dt)
{
int i = 0;
int iMISId = 0;
string MemberType = "";
string Status = "";
string Prefix = "";
string FirstName = "";
string MiddleName = "";
string LastName = "";
string Organisation = "";
string Address1 = "";
string Address2 = "";
string Address3 = "";
string Town = "";
string County = "";
string PostCode = "";
string Country = "";
string CountryofOrigin = "";
string Region = "";
string IncomeLevel = "";
string MainEmail = "";
string AlternateEmail = "";
string AddressPurpose = "";
DateTime JoinDate = DateTime.Parse("1900-01-01 00:00:00");
string Gender = "";
DateTime DateofBirth = DateTime.Parse("1900-01-01 00:00:00");
decimal TotalFeeOwed = 0;
decimal TotalPaid = 0;
decimal TotalOutstanding = 0;
string DelegateSource = "";
string CompanyType = "";
int CompanyId = 0;
DateTime DateAdded = DateTime.Parse("1900-01-01 00:00:00"); ;
string Fax = "";
DateTime LastUpdated = DateTime.Parse("1900-01-01 00:00:00"); ;
DateTime MemberTypeChangeDate = DateTime.Parse("1900-01-01 00:00:00"); ;
DateTime PaidThrough = DateTime.Parse("1900-01-01 00:00:00"); ;
string PreviousMemberType = "";
string Website = "";
string WorkPhone = "";
DateTime RenewedThru = DateTime.Parse("1900-01-01 00:00:00"); ;
string ExtraNote = "";
DateTime SuspendedDate = DateTime.Parse("1900-01-01 00:00:00"); ;
decimal CreditAmount = 0;
decimal OpeningBalance = 0;
DateTime PaidThru = DateTime.Parse("1900-01-01 00:00:00"); ;
decimal PaymentAmount = 0;
decimal PreviousBalance = 0;
string ProductCode = "";
string ProductTitle = "";
string CompanyIpAddress = "";
string OldMemberType = "";
int OldMemberNumber = 0;
string RemovalCode = "";
string RemovalReason = "";
string SkypeAddress = "";
bool UseOriginCountry = false;
string CommitteePreference1 = "";
string CommitteePreference2 = "";
string CommitteePreference3 = "";
string CommitteePreference4 = "";
bool OfferTrialMembership = false;

解决办法:

 

foreach (DataRow dr in dt.Rows)
{

添加: DateTime? TimeNull = null;

一种类型认为是可以为 null,如果它可以分配一个值,也可以分配null,这意味着类型具有无论如何没有值。 默认情况下,所有都引用类型,如String,是否可以为 null,但所有值类型,如Int32,不是。

if (!string.IsNullOrEmpty(dr["iMIS Id"].ToString()))
{
iMISId = int.Parse(dr["iMIS Id"].ToString().Trim());
}
MemberType = dr["Member Type"].ToString().Trim().Replace("'", "''");
Status = dr["Status"].ToString().Trim().Replace("'", "''");
Prefix = dr["Prefix"].ToString().Trim().Replace("'", "''");
FirstName = dr["First Name"].ToString().Trim().Replace("'", "''");
MiddleName = dr["Middle Name"].ToString().Trim().Replace("'", "''");
LastName = dr["Last Name"].ToString().Trim().Replace("'", "''");
Organisation = dr["Organisation"].ToString().Trim().Replace("'", "''").Replace("'", "''");
Address1 = dr["Address 1"].ToString().Trim().Replace("'", "''");
Address2 = dr["Address 2"].ToString().Trim().Replace("'", "''");
Address3 = dr["Address 3"].ToString().Trim().Replace("'", "''");
Town = dr["Town"].ToString().Trim().Replace("'", "''");
County = dr["County"].ToString().Trim().Replace("'", "''");
PostCode = dr["Post Code"].ToString().Trim().Replace("'", "''");
Country = dr["Country"].ToString().Trim().Replace("'", "''");
CountryofOrigin = dr["Country of Origin"].ToString().Trim().Replace("'", "''");
Region = dr["Region"].ToString().Trim().Replace("'", "''");
IncomeLevel = dr["Income Level"].ToString().Trim().Replace("'", "''");
MainEmail = dr["Main Email"].ToString().Trim().Replace("'", "''");
AlternateEmail = dr["Alternate Email"].ToString().Trim().Replace("'", "''");
AddressPurpose = dr["Address Purpose"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Join Date"].ToString()))
{
JoinDate = DateTime.Parse(DateTime.ParseExact(dr["Join Date"].ToString().Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
Gender = dr["Gender"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Date of Birth"].ToString()))
{
DateofBirth = DateTime.Parse(DateTime.ParseExact(dr["Date of Birth"].ToString().Trim(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
if (!string.IsNullOrEmpty(dr["Total Fee Owed"].ToString()))
{
TotalFeeOwed = decimal.Parse(dr["Total Fee Owed"].ToString().Trim());
}
if (!string.IsNullOrEmpty(dr["Total Paid"].ToString()))
{
TotalPaid = decimal.Parse(dr["Total Paid"].ToString().Trim());
}
if (!string.IsNullOrEmpty(dr["Total Outstanding"].ToString()))
{
TotalOutstanding = decimal.Parse(dr["Total Outstanding"].ToString().Trim());
}
DelegateSource = dr["Delegate Source"].ToString().Trim().Replace("'", "''");
CompanyType = dr["Company Type"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Company Id"].ToString()))
{
CompanyId = int.Parse(dr["Company Id"].ToString().Trim());
}
if (!string.IsNullOrEmpty(dr["Date Added"].ToString()))
{
DateAdded = DateTime.Parse(DateTime.ParseExact(dr["Date Added"].ToString().Trim(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
Fax = dr["Fax"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Member Type Change Date"].ToString()))
{
MemberTypeChangeDate = DateTime.Parse(DateTime.ParseExact(dr["Member Type Change Date"].ToString().Trim(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
if (!string.IsNullOrEmpty(dr["Paid Through"].ToString()))
{
PaidThrough = DateTime.Parse(DateTime.ParseExact(dr["Paid Through"].ToString().Trim(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
PreviousMemberType = dr["Previous Member Type"].ToString().Trim().Replace("'", "''");
Website = dr["Website"].ToString().Trim().Replace("'", "''");
WorkPhone = dr["WorkPhone"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["RenewedThru"].ToString()))
{
RenewedThru = DateTime.Parse(DateTime.ParseExact(dr["RenewedThru"].ToString().Trim(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
ExtraNote = dr["ExtraNote"].ToString().Trim().Replace("'", "''").Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Suspended Date"].ToString()))
{
SuspendedDate = DateTime.Parse(DateTime.ParseExact(dr["Suspended Date"].ToString().Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
if (!string.IsNullOrEmpty(dr["Last Updated"].ToString()))
{
LastUpdated = DateTime.Parse(DateTime.ParseExact(dr["Last Updated"].ToString().Trim(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
if (!string.IsNullOrEmpty(dr["CreditAmount"].ToString()))
{
CreditAmount = decimal.Parse(dr["CreditAmount"].ToString().Trim());
}
if (!string.IsNullOrEmpty(dr["OpeningBalance"].ToString()))
{
OpeningBalance = decimal.Parse(dr["OpeningBalance"].ToString().Trim());
}
if (!string.IsNullOrEmpty(dr["PaidThru"].ToString()))
{
PaidThru = DateTime.Parse(DateTime.ParseExact(dr["PaidThru"].ToString().Trim(), "dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.GetCultureInfo("en-US")).ToString("yyyy-MM-dd HH:mm:ss"));
}
if (!string.IsNullOrEmpty(dr["PaymentAmount"].ToString()))
{
PaymentAmount = decimal.Parse(dr["PaymentAmount"].ToString().Trim());
}
if (!string.IsNullOrEmpty(dr["PreviousBalance"].ToString()))
{
PreviousBalance = decimal.Parse(dr["PreviousBalance"].ToString().Trim());
}
ProductCode = dr["ProductCode"].ToString().Trim().Replace("'", "''");
ProductTitle = dr["ProductTitle"].ToString().Trim().Replace("'", "''");
CompanyIpAddress = dr["Company Ip Address"].ToString().Trim().Replace("'", "''");
OldMemberType = dr["Old Member Type"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Old Member Number"].ToString()))
{
OldMemberNumber = int.Parse(dr["Old Member Number"].ToString().Trim());
}
RemovalCode = dr["Removal Code"].ToString().Trim().Replace("'", "''");
RemovalReason = dr["Removal Reason"].ToString().Trim().Replace("'", "''");
SkypeAddress = dr["Skype Address"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Use Origin Country"].ToString()))
{
UseOriginCountry = bool.Parse(dr["Use Origin Country"].ToString().Trim());
}
CommitteePreference1 = dr["Committee Preference 1"].ToString().Trim().Replace("'", "''");
CommitteePreference2 = dr["Committee Preference 2"].ToString().Trim().Replace("'", "''");
CommitteePreference3 = dr["Committee Preference 3"].ToString().Trim().Replace("'", "''");
CommitteePreference4 = dr["Committee Preference 4"].ToString().Trim().Replace("'", "''");
if (!string.IsNullOrEmpty(dr["Offer Trial Membership"].ToString()))
{
OfferTrialMembership = bool.Parse(dr["Offer Trial Membership"].ToString().Trim());
}
//sw = string.IsNullOrEmpty(sw) ? "null" : sw;
//kr = string.IsNullOrEmpty(kr) ? "null" : kr;
string strSql = string.Format("INSERT INTO IAHR_Member (Uid,iMISId,MemberType,Status,Prefix,FirstName,MiddleName,LastName,Organisation,Address1,Address2,Address3,Town,County,PostCode,Country"
",CountryofOrigin,Region,IncomeLevel,MainEmail,AlternateEmail,AddressPurpose,JoinDate,Gender,DateofBirth,TotalFeeOwed,TotalPaid,TotalOutstanding,DelegateSource,CompanyType,CompanyId,DateAdded"
",Fax,LastUpdated,MemberTypeChangeDate,PaidThrough,PreviousMemberType,Website,WorkPhone,RenewedThru,ExtraNote,SuspendedDate,CreditAmount,OpeningBalance,PaidThru,PaymentAmount,PreviousBalance"
",ProductCode,ProductTitle,CompanyIpAddress,OfferTrialMembership,OldMemberNumber,OldMemberType,RemovalCode,RemovalReason,SkypeAddress,UseOriginCountry,CommitteePreference1,CommitteePreference2"

time = item.HospOutDate.HasValue ? DateTime.Parse(item.HospOutDate.Value.ToString("yyyy-MM-dd HH:mm:ss")) : TimeNull ;

在 C# 和 Visual Basic 中,你可以将值类型作为可以为 null 标记使用?后的值类型的表示法。 例如,int?在 C# 或Integer?在 Visual Basic 中声明可以分配的整数值类型null

  • ",CommitteePreference3,CommitteePreference4) VALUES (0,{0},'{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}',{24},{25},{26},'{27}','{28}',{29},'{30}','{31}','{32}','{33}','{34}','{35}','{36}','{37}','{38}','{39}','{40}',{41},{42},'{43}',{44},{45},'{46}','{47}','{48}','{49}','{50}','{51}','{52}','{53}','{54}','{55}','{56}','{57}','{58}','{59}')", iMISId, MemberType, Status, Prefix, FirstName, MiddleName, LastName, Organisation, Address1, Address2, Address3, Town, County, PostCode, Country, CountryofOrigin, Region, IncomeLevel, MainEmail, AlternateEmail, AddressPurpose, JoinDate, Gender, DateofBirth, TotalFeeOwed, TotalPaid, TotalOutstanding, DelegateSource, CompanyType, CompanyId, DateAdded, Fax, LastUpdated, MemberTypeChangeDate, PaidThrough, PreviousMemberType, Website, WorkPhone, RenewedThru, ExtraNote, SuspendedDate, CreditAmount, OpeningBalance, PaidThru, PaymentAmount, PreviousBalance, ProductCode, ProductTitle, CompanyIpAddress, OfferTrialMembership, OldMemberNumber, OldMemberType, RemovalCode, RemovalReason, SkypeAddress, UseOriginCountry, CommitteePreference1, CommitteePreference2, CommitteePreference3, CommitteePreference4);
    string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection sqlConnection = new SqlConnection(strConnection);
    try
    {
    // SqlConnection sqlConnection = new SqlConnection(strConnection);
    sqlConnection.Open();
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandText = strSql;
    sqlCmd.Connection = sqlConnection;
    SqlDataReader sqlDataReader = sqlCmd.ExecuteReader();
    i ;

Nullable<T>结构支持使用值类型为 null 的类型,因为引用类型可以为 null 设计使然。

labNum.InnerText = i.ToString();
sqlDataReader.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlConnection.Close();
}
//if (opdb.ExcSQL(strSql))
// i ;
}
return i;
}

Nullable类支持互补Nullable<T>结构。 Nullable类支持获取可以为 null 的类型的基础类型,其基础值类型可以为 null 的类型的对比较和相等操作不支持泛型比较和相等性操作。

基本属性

 

两个基本成员Nullable<T>结构不是HasValue和Value属性。 如果HasValue属性Nullable<T>对象是true,可通过访问对象的值Value属性。 如果HasValue属性是false,对象的值是不确定并尝试访问Value属性将引发InvalidOperationException。

装箱和取消装箱

 

可以为 null 的类型进行装箱时,公共语言运行时自动框的基础值Nullable<T>对象,而不Nullable<T>对象本身。也就是说,如果HasValue属性是true的内容Value属性进行装箱。 未装箱可以为 null 的类型的基础值时,公共语言运行时创建一个新Nullable<T>结构初始化为基础的值。

如果HasValue可以为 null 的类型的属性是false,装箱操作的结果是null。 因此,如果装箱可以为 null 的类型传递给需要的对象自变量的方法,该方法必须准备好处理这种情况,其中的参数是null。 当null是取消装箱到可以为 null 的类型,公共语言运行时创建一个新Nullable<T>结构并初始化其HasValue属性false

 

 

 

图片 2

图片 3

  GetUnderlyingType(Type nullableType)方法是用来返回一个可为空类型的基础类型,如果 nullableType 参数不是一个封闭的Nullable<T>泛型,则返回null。 

图片 4

 1     Console.WriteLine(Nullable.GetUnderlyingType(typeof(Nullable<int>)));
 2     //输出结果:System.Int32
 3 
 4     Console.WriteLine(Nullable.GetUnderlyingType(typeof(Nullable<>)) == null);
 5     //输出结果:True
 6 
 7     Console.WriteLine(Nullable.GetUnderlyingType(typeof(int)) == null);
 8     //输出结果:True
 9 
10     Console.WriteLine(Nullable.GetUnderlyingType(typeof(string)) == null);
11     //输出结果:True

 

 

支持 可以分配 的值类型null。 此类不能被继承。

备注

一种类型认为是可以为 null,如果它可以分配一个值,也可以分配null,这意味着类型具有无论如何没有值。 默认情况下,所有都引用类型,如String,是否可以为 null,但所有值类型,如Int32,不是。

在 C# 和 Visual Basic 中,你可以将值类型作为可以为 null 标记使用?后的值类型的表示法。 例如,int?在 C# 或Integer?在 Visual Basic 中声明可以分配的整数值类型null

Nullable类支持互补Nullable<T>结构。 Nullable类支持获取可以为 null 的类型的基础类型,其基础值类型可以为 null 的类型的对比较和相等操作不支持泛型比较和相等性操作。

装箱和取消装箱

 

可以为 null 的类型进行装箱时,公共语言运行时自动框的基础值Nullable<T>对象,而不Nullable<T>对象本身。 也就是说,如果HasValue属性是true的内容Value属性进行装箱。 如果HasValue属性是falsenull进行装箱。 未装箱可以为 null 的类型的基础值时,公共语言运行时创建一个新Nullable<T>结构初始化为基础的值。

 

 

 

 遇到的问题

 图片 5

DataRow 里面 取数据 取出来 Object  如果有值 赋到 模型上,没有就赋 null

字段也为可空的值类型  但就是报错 

 图片 6

在外面定义个对应可空值类型  然后赋值 就解决了

图片 7

 

 图片 8

图片 9

这样也行 图片 10

给Guid 的 空

图片 11

给 int 的空 报错 

 

 

 

 

 

图片 12

 

 

 

无法确定条件表达式的类型,因为“<null>”和“System.DateTime”之间没有隐式转换 解决办法

 

无法确定条件表达式的类型,因为“<null>”和“System.DateTime”之间没有隐式转换----解决办法

        当在C#使用可空类型时要注意类型间的一致,出现上述和DateTime无法转换的问题就是没有注意类型间的一致,例如,当给一个可空的时间类型字段赋值时不能使用null,而要使用一个DateTime? 类型。

例子:(报错了)

  public DateTime? time { get; set; }

  time = item.HospOutDate.HasValue ? DateTime.Parse(item.HospOutDate.Value.ToString("yyyy-MM-dd HH:mm:ss")) : null;

错误提示:无法确定条件表达式的类型,因为“<null>”和“System.DateTime”之间没有隐式转换

 

解决办法:

添加: DateTime? TimeNull = null;

time = item.HospOutDate.HasValue ? DateTime.Parse(item.HospOutDate.Value.ToString("yyyy-MM-dd HH:mm:ss")) : TimeNull ;

 

 图片 13

 

 

 

 

说明: 总结 

可空值类型 是个泛型结构体

图片 14

Nullable<int>

Nullable<Guid>

Nullable<DataTime>

虽然初始值都是 null

但 是不同类型的null

所有不能直接赋  会报错

 

图片 15

 

 

 

 

 

本文由pc28.am发布于计算机编程,转载请注明出处:力不能支显著标准表达式的品种,职业总计

上一篇:Android下的POS打印机调用的简单实现,Socket链接小 下一篇:没有了
猜你喜欢
热门排行
精彩图文