简体   繁体   English

“输入字符串的格式不正确。”C#

[英]“Input string was not in a correct format.” C#

groupID is a number of checkboxes. groupID是一些复选框。 I am trying to create a comma separated list of UserIDs. 我正在尝试创建逗号分隔的UserID列表。 GroupIDs identify a group with a number of users as a member. GroupID标识具有多个用户的组作为成员。 So I have: 所以我有:

      string groupIds = Request.Form["groupID"];
        if (groupIds!=null){
            string[] arrayOfGroupIds = groupIds.Split(','); //possible error here if only one checkbox is ticked?
            foreach (string id in arrayOfGroupIds){
                var users = db.Query("SELECT guestID FROM userGroups WHERE id = @0", id);
                foreach(var user in users){
                    userIds += user.guestID + ",";
                }
            }
        }   
        userIds += Request.Form["userId"] + "," + yourID;

On my next page I then call : 在我的下一页,我然后打电话给:

string userIDs = Request.Form["userIDs"];
string[] userIdsArray = userIDs.Split(',');
foreach(string userID in userIdsArray){
if(Convert.ToInt32(userID) == loggedID) { //ERROR HERE
        db.Execute("INSERT INTO Membership(GroupId,UserId) VALUES (@0, @1)", groupID, userID);
    }

Which results in the error in the title. 这导致标题中的错误。

What could be causing this ? 可能是什么导致了这个?

Obviously the userID in the error line can't convert to an integer. 显然,错误行中的userID无法转换为整数。 You can add a break point, and see what value the variable actually is, and then modify your code. 您可以添加断点,查看变量的实际值,然后修改代码。

BTW, you can convert in a more secure way. 顺便说一句,您可以以更安全的方式进行转换。 Like 喜欢

int userIDInt;
if (int.TryParse(userID, out userIDInt))
{
    if (userIDInt == loggedID)
       // ...
}
else
    // handle the error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM