错误:无法将stringbuilder转换为字符串 [英] Error: failed to convert stringbuilder to string

查看:121
本文介绍了错误:无法将stringbuilder转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#将一些数据发送到数据库。填写的其中一个字段是复选框列表。如果使用选择多个选项,我希望所有选项都存储在数据库中。我试图使用stringbuilder将所有选项连接在一起,但我不断收到错误,无法将stringbuilder转换为字符串。



我看过已发布的类似问题,但我发现的任何内容都没有解决我的问题,所以我要发布我的内容并希望有人可以协助我弄清楚如何纠正问题。任何和所有建议/协助表示赞赏。



I am trying to send some data to a database using C#. One of the fields that gets filled out is a checkbox list. If the use selects multiple options, i would like all of them to be stored in the database. I am trying to use a stringbuilder to connect all of the options together, but I keep getting an error of not being able to convert stringbuilder to a string.

I have looked at similar questions that have been posted, but nothing I have found has corrected my problem yet so I am going to post what I have and hope someone can assist me in figuring out how to correct the problem. any and all suggestions/assistance is appreciated.

protected void btnSubmitRequest_Click(object sender, EventArgs e)
    {
        string employeeName = txtEmployeeName.Text;
        string employeeNumber = txtEmployeeNum.Text;
        string contact = txtContact.Text;
        string reason = rblReason.SelectedValue;
        Boolean quickCharge;
        if (rblReason.SelectedValue != "Lost")
        {
            quickCharge = false;
        }
        else
        {
            quickCharge = Convert.ToBoolean(rblQuickCharge.SelectedValue);
        }
        string notWorkingDetails = txtNotWorkingDetails.Text;
        string newTitle = txtNewTitle.Text;
        string newLocation = txtNewLocation.Text;
        string otherDesc = txtOtherDescription.Text;
        string specialInstructions = cblSpecialInstructions.Text;
        string deliverToLoc = txtDeliverToLoc.Text;
        string deliverToDept = txtDeliverToDept.Text;

        System.Text.StringBuilder sbSpecial = new System.Text.StringBuilder();
        foreach (var str in specialInstructions)
            sbSpecial.Append(str).Append(", ");

        #region //Send to Database
        try
        {
            using (SqlConnection connection = new SqlConnection("server=10.52.2.169\\sqlcluster,1206;uid=TannerAppsWriter;pwd=TannerAppsWriter;database=TannerInternal;"))
            {
                using (SqlCommand cmdInsertRequest = new SqlCommand("HR.BadgeReplacementRequest", connection))
                {
                    cmdInsertRequest.CommandType = CommandType.StoredProcedure;

                    #region //Set Parameters to Add to StoredProcedure
                    cmdInsertRequest.Parameters.Add("@employeeName", SqlDbType.VarChar, 150);
                    cmdInsertRequest.Parameters["@employeeName"].Value = employeeName;
                    cmdInsertRequest.Parameters.Add("@employeeNumber", SqlDbType.VarChar, 50);
                    cmdInsertRequest.Parameters["@employeeNumber"].Value = employeeNumber;
                    cmdInsertRequest.Parameters.Add("@contact", SqlDbType.VarChar, 150);
                    cmdInsertRequest.Parameters["@contact"].Value = contact;
                    cmdInsertRequest.Parameters.Add("@reason", SqlDbType.VarChar, 100);
                    cmdInsertRequest.Parameters["@reason"].Value = reason;
                    cmdInsertRequest.Parameters.Add("@quickCharge", SqlDbType.Bit);
                    cmdInsertRequest.Parameters["@quickCharge"].Value = quickCharge;
                    cmdInsertRequest.Parameters.Add("@notWorkingDetails", SqlDbType.VarChar, 250);
                    cmdInsertRequest.Parameters["@notWorkingDetails"].Value = notWorkingDetails;
                    cmdInsertRequest.Parameters.Add("@newTitle", SqlDbType.VarChar, 75);
                    cmdInsertRequest.Parameters["@newTitle"].Value = newTitle;
                    cmdInsertRequest.Parameters.Add("@newLocation", SqlDbType.VarChar, 150);
                    cmdInsertRequest.Parameters["@newLocation"].Value = newLocation;
                    cmdInsertRequest.Parameters.Add("@otherDesc", SqlDbType.VarChar, 250);
                    cmdInsertRequest.Parameters["@otherDesc"].Value = otherDesc;
                    cmdInsertRequest.Parameters.Add("@specialInstructions", SqlDbType.VarChar, 50);
                    cmdInsertRequest.Parameters["@specialInstructions"].Value = sbSpecial;
                    cmdInsertRequest.Parameters.Add("@deliverToLoc", SqlDbType.VarChar, 150);
                    cmdInsertRequest.Parameters["@deliverToLoc"].Value = deliverToLoc;
                    cmdInsertRequest.Parameters.Add("@deliverToDept", SqlDbType.VarChar, 150);
                    cmdInsertRequest.Parameters["@deliverToDept"].Value = deliverToDept;

                    #endregion

                    connection.Open();

                    cmdInsertRequest.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.ToString());
        }
        #endregion
}

推荐答案

cmdInsertRequest.Parameters["@specialInstructions"].Value = sbSpecial.ToString();





修复此行,它应该可以工作



Fix this line and it should work


我无法弄清楚如何将每个选择推送到数据库。它只是继续按1并用逗号分隔其中的字母,如前所示。我改变了我的代码并最终做到了这一点:



I couldn't figure out how to get it to push each selection to the database. It just kept pushing 1 and separating the letters in it by commas as showed previously. I changed up my code and ended up doing this:

CheckBoxList chkbxspecial = (CheckBoxList)form1.FindControl("cblSpecialInstructions");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < chkbxspecial.Items.Count; i++)
        {
            if (chkbxspecial.Items[i].Selected)
            {
                sb.Append(chkbxspecial.Items[i].Value + ", ");
            }
        }
        string InputString = sb.ToString().Substring(0, sb.ToString().Length - 1);





然后在存储过程区域执行此操作:





Then did this at the stored procedure area:

cmdInsertRequest.Parameters.Add("@specialInstructions", SqlDbType.VarChar, 50);
cmdInsertRequest.Parameters["@specialInstructions"].Value = InputString;





现在显示效果很棒!谢谢你带领我走向正确的方向。 @Allen Chong,我仍然接受你的解决方案,因为它回答了我提出的原始问题并删除了错误。由于出现了新的错误,我只需要改变一切。



Displays great now! thanks for leading me in the right direction guys. @Allen Chong, I am still accepting your solution since it answered the original question I asked and removed the error. I just had to change things around because of the new error that arose.


这篇关于错误:无法将stringbuilder转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆