Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法连接多个字符串_C# - Fatal编程技术网

C# 无法连接多个字符串

C# 无法连接多个字符串,c#,C#,我试图建立一个字符串,它给出了错误,我正在分享我的代码,请指导我 public static bool Send(string Email, string ProcessID, string Name, string Part, string MFG, string TicketTotal) { StringBuilder sb = new StringBuilder(); string html = @"Dear, " + Name + "<br><br&g

我试图建立一个字符串,它给出了错误,我正在分享我的代码,请指导我

public static bool Send(string Email, string ProcessID, string Name, string Part, string MFG, string TicketTotal)
{
    StringBuilder sb = new StringBuilder();

    string html = @"Dear, " + Name + "<br><br>" + "We are sending the price for your reference" + "<br><br>"

    + @"<table><thead style='background-color: #006DCC;color: white;padding:10px;width:700px;'>
        <tr>
            <th style='background-color:#006DCC;color: white;'>
                Part No
            </th>
            <th>
                Manufacturer
            </th>
        </tr>
    </thead>" +

    @"<tbody style='background-color: #EEEEEE;'>" +
    for(int i=1;i<10;i++)
    {
        @"<tr>                                                
        <td style='background-color: #EEEEEE;'>" +
        @"<span>" + Part + "</span>" +
        "</td>" +

        @"<td>" + "<span>" + MFG + "</span>" + "</td>" +

        @"</tr>"
    } +

    @"</tbody></table>" +
    @"<br><br>" + "Total Amount: " + TicketTotal + "<br>" + "Process ID: " + ProcessID + "<br><br>"

    + @"For more inquiry please visit <a href='#' target='_blank'> Contact</a>"
    + @"<br><br>" + "Best Regards" + "<br><br>" + "Address: Address here" + "<br>"
    + @"Tel: Number" + "<br>" + "Fax: number" + "<br>" + "<a href='#' target='_blank'>abc</a>";                        
}

我想使用loop在表中添加十行,我已经搜索了internet,但找不到任何合适的解决方案。请引导我

正如其他人所说,这样做完全是错误的

public static bool Send(string Email, string ProcessID, string Name, string Part, string MFG, string TicketTotal)
{
    StringBuilder sb = new StringBuilder();

    string html = @"Dear, " + Name + "<br><br>" + "We are sending the price for your reference" + "<br><br>"

    + @"<table><thead style='background-color: #006DCC;color: white;padding:10px;width:700px;'>
        <tr>
            <th style='background-color:#006DCC;color: white;'>
                Part No
            </th>
            <th>
                Manufacturer
            </th>
        </tr>
    </thead>" +

    @"<tbody style='background-color: #EEEEEE;'>" +
    for(int i=1;i<10;i++)
    {
        @"<tr>                                                
        <td style='background-color: #EEEEEE;'>" +
        @"<span>" + Part + "</span>" +
        "</td>" +

        @"<td>" + "<span>" + MFG + "</span>" + "</td>" +

        @"</tr>"
    } +

    @"</tbody></table>" +
    @"<br><br>" + "Total Amount: " + TicketTotal + "<br>" + "Process ID: " + ProcessID + "<br><br>"

    + @"For more inquiry please visit <a href='#' target='_blank'> Contact</a>"
    + @"<br><br>" + "Best Regards" + "<br><br>" + "Address: Address here" + "<br>"
    + @"Tel: Number" + "<br>" + "Fax: number" + "<br>" + "<a href='#' target='_blank'>abc</a>";                        
}
但是,为了让你在正确的方向上前进,请解释为什么这不起作用,请参见下文

您正在尝试将循环连接到字符串。你不能这样做:

@"<tbody style='background-color: #EEEEEE;'>" + // this plus sign is the problem
          for(int i=1;i<10;i++)
         {
            @"<tr>      //etc.
您需要添加到循环内的字符串中:

string html = @"Dear, " + Name + "<br><br>" + "We are sending the price for your reference" + "<br><br>"

            + @"<table><thead style='background-color: #006DCC;color: white;padding:10px;width:700px;'>
                                            <tr>
                                                <th style='background-color:#006DCC;color: white;'>
                                                    Part No
                                                </th>
                                                <th>
                                                    Manufacturer
                                                </th>

                                            </tr>
                                        </thead>" +

              @"<tbody style='background-color: #EEEEEE;'>"; \\terminate the line. 

//start the loop
 for(int i=1;i<10;i++)
         {
           html += @"<tr>                                                
                    <td style='background-color: #EEEEEE;'>" +
                    @"<span>" + Part + "</span>" +
                "</td>" +

                 @"<td>" + "<span>" + MFG + "</span>" + "</td>" +

            @"</tr>"; // not plus
        }

html +=  @"</tbody></table>" +
     @"<br><br>" + "Total Amount: " + TicketTotal + "<br>" + "Process ID: " + ProcessID + "<br><br>"

     + @"For more inquiry please visit <a href='#' target='_blank'> Contact</a>"
     + @"<br><br>" + "Best Regards" + "<br><br>" + "Address: Address here" + "<br>"
     + @"Tel: Number" + "<br>" + "Fax: number" + "<br>" + "<a href='#' target='_blank'>abc</a>";

在你的评论中,你提到你正在发送基于html的电子邮件。我也这样做过。我发现的最简单的方法是将一个HTML文件作为您的资源,其中包含电子邮件中带有标记的特定区域。例如,我选择%USERNAME%作为用户名。然后,我可以将资源用作字符串,并对我拥有的标记执行string.Replace。这有两个目的。第一,准备起来很容易。第二,编辑和预览我的HTML很容易。下面是我的代码最后的样子

public override string GenerateEmailBody()
{
    var htmlBody = System.IO.File.ReadAllText("htmlEmail\\dliHTML.html");
    htmlBody = htmlBody.Replace("%AUTHOR%", Author);
    htmlBody = htmlBody.Replace("%REV%", Revision);
    htmlBody = htmlBody.Replace("%REPO%", Repo);
    htmlBody = htmlBody.Replace("%LOG%", Log);
    htmlBody = htmlBody.Replace("%DATE%", System.DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss"));
    htmlBody = htmlBody.Replace("%ADDED%",GetUList("Added",Added));
    htmlBody = htmlBody.Replace("%DELETED%",GetUList("Deleted",Deleted));
    htmlBody = htmlBody.Replace("%MODIFIED%",GetUList("Modified",Modified));
    return htmlBody;
}
你看,我确实做了一个乌拉主义者。也许有更好的方法,但这种方法并不可怕

private string GetUList(string header, string[] list)
{
    if (list==null) return string.Empty;
    if (list.Length==0) return string.Empty;

    var ulist = GetHeaderString(3, header);
    ulist += "<UL>";
    foreach (var element in list)
    {
        ulist += string.Format("<li>{0}</li>", element);
    }
    ulist += "</UL>";

    return ulist;
}
编辑

下面是我忘记添加的Get header String方法

private string GetHeaderString(int size, string message)
{
    return string.Format("<H{0}>{1}</H{0}>" ,size, message);
}

我可以推荐一个StringBuilder吗?或者是一个模板引擎吗?为什么不使用htmlTable类来代替字符串操作呢@RandRandom我看不到它被使用,至少没有被正确使用。这是一个不粗鲁的复习,我认为当我们看到这样的基本错误时,我们应该提供帮助,发布一些相关信息来纠正基本错误,而不是填鸭式地提供解决方案。谢谢你的评论,我该怎么做?@Usama查看此代码/编辑。这就告诉你怎么做了。@Usama-这看起来不是很漂亮很整洁吗?这是一个好得多的选择。虽然缺少一些东西,但这肯定是一个更好的选择approach@Steve我可以补充缺少的东西。我忘了添加什么?不重要,只是代码GetHeaderString@Steve只为你:添加它。。。