运算符'+'未定义为类型'ListItem'和字符串“,”

时间:2013-07-02 15:02:21

标签: asp.net vb.net

我正在尝试将文本放在下面的会话中,所以它基本上是Product1,Product2,Product3等等...另外如果会话包含文本“Select ....”将其替换为“”当我运行下面的代码我得到错误运算符'+'没有定义类型'ListItem'和字符串“,”。

NameofProduct = Session("Product1") + "," + Session("Product2").Replace("Select....", "") + ","  + Session("Product3") + "," + Session("Product4") + "," + Session("ProductManual")

4 个答案:

答案 0 :(得分:5)

您正在尝试将objects连接在一起,而是执行此操作:

NameofProduct = Session("Product1").ToString() + "," + Session("Product2").ToString().Replace("Select....", "") + ","  + Session("Product3").ToString() + "," + Session("Product4").ToString() + "," + Session("ProductManual").ToString()

答案 1 :(得分:2)

为了可维护性而使用StringBuilder:

Dim sb as new StringBuilder
sb.AppendFormat("{0},", Session("Product1"))
sb.AppendFormat("{0},", Session("Product2").Replace("Select....", ""))
sb.AppendFormat("{0},", Session("Product3"))
sb.AppendFormat("{0},", Session("Product4"))
sb.AppendFormat("{0}", Session("ProductManual")
NameofProduct = sb.ToString()

答案 2 :(得分:1)

好奇,你试过&运算符???

NameofProduct = Session("Product1") & "," & Session("Product2").Replace("Select....", "") & "," & Session("Product3") & "," & Session("Product4") & "," & Session("ProductManual")

试试

答案 3 :(得分:0)

IMO尝试Session(“Product1”)。ToString()在串联的所有部分;你会发现它们并非都是字符串......